@Autowire在jersey资源类中给出空指针异常

时间:2016-08-25 09:00:13

标签: spring spring-mvc jersey-1.0

我尝试使用我的mapper接口@autowire我的jersey资源类,并且它给出了空指针注释异常但是@autowire在我的调度程序控制器中工作正常。我是spring mvc的新手,我只想通过我的mapper界面找到@autowire我的jersey资源类。

控制器代码

@Controller
public class EmployController {
    @Autowired
    EmployMapper employMapper;

    @RequestMapping("test")
    public ModelAndView reviewspage()
    {
        employMapper.updateMytable("SpringController");
          ModelAndView modelAndView = new ModelAndView("test");
            return modelAndView;
    }
}

球衣资源类

@Path("/employees")
@Repository
public class MyResource {
    @Autowired
    EmployMapper employMapper;

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getEmployees(){
        employMapper.updateMytable("WebAPiController");
        return "Successfuly done";
    }
}

Mapper界面

public interface EmployMapper {
    public int finduser(@Param("MName") String MName,@Param("password") String password);
    public int updateMytable(@Param("name") String name);
}

Spring调度程序xml

<beans 
   <mvc:annotation-driven />
   <context:component-scan base-package="com.controller"/>
    <bean id = "ViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" 
      value="/WEB-INF/" />
      <property name="suffix"
       value=".jsp" />
   </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost/employtrackingsystem"/>
    <property name="username" value="adminXuMPZTn" />
    <property name="password" value="IMhzT-AJE47P" />
  </bean>


   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
     <property name="dataSource" ref="dataSource" />
     <property name="mapperLocations" value="classpath*:*mappers/EmployMapper.xml"/>

</bean>


  <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
      <constructor-arg index="0" ref="sqlSessionFactory" />
 </bean>

 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.mappers"/>
</bean>
</beans>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         metadata-complete="false">

         <servlet>
    <servlet-name>spring-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>spring-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>



           <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.resources</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/webapi/*</url-pattern>
    </servlet-mapping>
     <context-param>
    <param-name>resteasy.scan</param-name>
    <param-value>false</param-value>
</context-param>
<context-param>
    <param-name>resteasy.scan.providers</param-name>
    <param-value>false</param-value>
</context-param>
<context-param>
    <param-name>resteasy.scan.resources</param-name>
    <param-value>false</param-value>
</context-param>


</web-app>

1 个答案:

答案 0 :(得分:1)

我的猜测是你没有使用jersey-spring配置Jersey将对象生命周期委托给Spring。如果没有明确地完成,那么Jersey本身将控制生命周期,这意味着它将是Jersey实例化你的资源类。