具有组件扫描功能的简单RestFul WebService

时间:2018-08-02 04:59:36

标签: java spring rest jax-rs

我试图在restFul Web服务中使用组件扫描自动连接对象。但是,当使用 userDao 对象时,会出现Nullpointer异常。

我不想使用Spring MVC或spring boot。只想使用组件扫描简单地注入依赖项。请帮忙!!!

UserDao.java

@Component
public class UserDao { 
public List<User> getAllUsers(){ 
    //return a list 
} 
}

UserService.java

@Component
@Path("/UserService") 
public class UserService {

    @Autowired
    private UserDao userDao;

    @PostConstruct
    public void initialize() {
        System.out.println("userDao object: " + userDao);
    }

    @GET 
    @Path("/users") 
    @Produces(MediaType.APPLICATION_JSON) 
    public List<User> getUsers(){ 
        return getUserDao().getAllUsers(); 
    }  

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
}

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:context = "http://www.springframework.org/schema/context"
   xmlns:aop = "http://www.springframework.org/schema/aop"
   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.springframework.org/schema/aop 
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

   <context:annotation-config/>
   <context:component-scan base-package="com.rest"/>
   <aop:aspectj-autoproxy/>

</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"  
   xmlns = "http://java.sun.com/xml/ns/javaee"  
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
   id = "WebApp_ID" version = "3.0"> 
   <display-name>User Management</display-name> 
   <servlet> 
      <servlet-name>Jersey RESTful Application</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>com.rest</param-value> 
      </init-param> 
      <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>      
   </servlet> 
   <servlet-mapping> 
      <servlet-name>Jersey RESTful Application</servlet-name> 
      <url-pattern>/rest/*</url-pattern> 
   </servlet-mapping> 

</web-app>

1 个答案:

答案 0 :(得分:0)

您将需要在applicationContext.xml文件中定义一个bean。启动应用程序时,Spring容器会初始化此bean。 例如。

<bean id="userDAO" class="your.package.name.UserDAO">
<!-- In case if you use hibernate -->
<property name="sessionFactory" ref="sessionFactory" />
</bean>

打个比方,您可以将其想象为春天正在为您做UserDAO userDAO = new UserDAO();