spring注入,空指针异常,看了好多帖子忍找不到办法,求指点

J2EE 码拜 8年前 (2016-03-12) 1054次浏览
以下是本人本人做的例子:使用全注解的形式
DAO层:
package Impl.DAOImpl;
import javax.annotation.Resource;
import org.springframework.stereotype.Repository;
import DAO.RegionDAO;
import Entity.Region;
@Repository(value=”regionDAOimpl”)
public class RegionDAOImpl implements RegionDAO {
@Resource
private RegionDAO regionmapper;
public RegionDAO getRegionmapper() {
return regionmapper;
}
/*public void setRegionmapper(RegionDAO regionmapper) {
this.regionmapper = regionmapper;
}*/
public Region selectRegion(int code) {
return regionmapper.selectRegion(code);
}
public int updateRegion(int code) {
return regionmapper.updateRegion(code);
}
public void deleteRegion(int code) {
regionmapper.deleteRegion(code);
}
public int insertRegion(Region region) {
return regionmapper.insertRegion(region);
}
public void test2() {
System.out.println(“test2”);
}
}
Service层:
package Impl.ServiceImpl;
import java.net.URI;
import javax.annotation.Resource;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.xml.bind.JAXBElement;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import DAO.RegionDAO;
import Entity.Region;
import SERVICE.RegionService;
@Service
@Path(“/region”)
public class RegionServiceImpl implements RegionService {
@Context UriInfo uriInfo;
@Resource(name=”regionDAOimpl”)//不需要set方法了
private RegionDAO regionDAOimpl;
/*public void setRegionDAOimpl(RegionDAO regionDAOimpl) {
this.regionDAOimpl = regionDAOimpl;
}*/
public RegionDAO getRegionDAOimpl() {
return this.regionDAOimpl;
}
@Path(“/{id}”)
@GET
@Produces(MediaType.APPLICATION_XML)
public Region findRegion(@PathParam(“id”) String id) {
System.out.println(“10”);
int code = Integer.valueOf(id);
System.out.println(regionDAOimpl == null);
return this.regionDAOimpl.selectRegion(code);
}
@PUT
@Produces(“text/xml”)
@Consumes(MediaType.APPLICATION_XML)
@Transactional
public int update(JAXBElement<Region> jaxbRegion) {
Region region = jaxbRegion.getValue();
int id = region.getCode();
return this.regionDAOimpl.updateRegion(id);
}
@Path(“/{id}”)//子资源方法
@Transactional
@DELETE
@Consumes(“text/xml”)
public void delete(@PathParam(“id”)int id) {
this.regionDAOimpl.deleteRegion(id);
}
@Transactional
@POST
@Produces(“text/xml”)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)//接受表单输入
public int insert(@FormParam(“code”) String code,
@FormParam(“name”) String name) {
Region region = new Region();
int id = Integer.valueOf(code);
region.setCode(id);
region.setName(name);
URI uri = uriInfo.getAbsolutePathBuilder().path(code).build();
Response.created(uri).build();
return this.regionDAOimpl.insertRegion(region);
}
public void test() {
System.out.println(“ok”);
}
}
applicationContext.xml配置:
<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:aop=”http://www.springframework.org/schema/aop”
xmlns:tx=”http://www.springframework.org/schema/tx”
xmlns:context=”http://www.springframework.org/schema/context”
xmlns:mybatis=”http://www.mybatis.org/schema/mybatis”
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.mybatis.org/schema/mybatis
http://www.mybatis.org/schema/mybatis/mybatis-spring.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd”
default-autowire=”byName” ><!– default-autowire=”byName”  –>
<context:annotation-config />
<context:component-scan base-package=”Impl” />

<context:property-placeholder location=”classpath:postgre.properties” />

<bean id=”dataSource” class=”org.springframework.jdbc.datasource.DriverManagerDataSource”
>
<property name=”driverClassName” value=”${jdbc.driverClassName}” />
<property name=”url” value=”${jdbc.url}” />

</bean>
<bean id=”transactionManager” class=”org.springframework.jdbc.datasource.DataSourceTransactionManager”>
<property name=”dataSource” ref=”dataSource”  />
</bean>
<tx:annotation-driven transaction-manager=”transactionManager” proxy-target-class=”true” />

<bean id=”sqlSessionFactory” class=”org.mybatis.spring.SqlSessionFactoryBean”>
<property name=”dataSource” ref=”dataSource” />
<property name=”mapperLocations” value=”classpath:mapper/*.xml” />
</bean>
<bean id=”regionmapper” class=”org.mybatis.spring.mapper.MapperFactoryBean” >
<property name=”sqlSessionFactory” ref=”sqlSessionFactory”/>
<property name=”mapperInterface” value=”DAO.RegionDAO”/>
</bean>
<!– <bean id=”regionDAOimpl” class=”Impl.DAOImpl.RegionDAOImpl” >
<property name=”regionmapper” ref=”regionmapper”/>
</bean>

<bean id=”regionServiceimpl” class=”Impl.ServiceImpl.RegionServiceImpl” >
<property name=”regionDAOimpl” ref=”regionDAOimpl”/>
</bean> –>

</beans>
web.xml配置:
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>JerseyRestTest</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>Impl.ServiceImpl</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JerseyRestTest</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>
运行时报空指针异常service里调用dao的方法出现的这个错误,于是本人建立test测试:
ApplicationContext appContext = new
ClassPathXmlApplicationContext(“applicationContext.xml”);
//System.out.println(appContext.getType(“regionServiceimpl”));
RegionServiceImpl regionService = (RegionServiceImpl)appContext
.getBean(RegionServiceImpl.class);
regionService.test();
显示正常;
看了好多帖子,还是找不到解决问题的办法,求指点!

解决方案

10

你前面的定义:
@Repository(value=”regionDAOimpl”)
public class RegionDAOImpl implements RegionDAO {
@Resource
private RegionDAO regionmapper;
感觉应该是这个regionmapper的问题。
那个测试类调用test方法没用到增删改查,因此没报错。
你具体错误日志是啥呢?

30

空指针
at Impl.ServiceImpl.RegionServiceImpl.findRegion(RegionServiceImpl.java:51)
RegionServiceImpl 51行都做了什么操作呢?(你的代码没有排版,看不出行数)

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明spring注入,空指针异常,看了好多帖子忍找不到办法,求指点
喜欢 (0)
[1034331897@qq.com]
分享 (0)