Spring服务没有注入web servlet

时间:2016-02-07 15:00:56

标签: spring servlets dependency-injection

我正在使用Spring DI并尝试在我的servlet中注入Spring服务。但是,它没有被注入并保持null,导致NullPointerException

我的servlet:

@WebServlet(urlPatterns = {"/Register"}, displayName = "RegisterServlet")
public class RegisterServlet extends HttpServlet {

    @Autowired
    @Qualifier("registerServlet")
    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // ...
        customerService.save(customer); // Fail, because service is null.
        // ...
    }

}

我的spring-controller.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="registerServlet" class="com.fishingstore.controller.RegisterServlet">
        <property name="customerService" ref="customerService"/>
    </bean>

</beans>

我的客户DAO课程:

@Repository
@Transactional
public class CustomerDAOImpl implements CustomerDAO {

    private SessionFactory sessionFactory;

    @Autowired
    @Qualifier("sessionFactory")
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    // ...
}

我的客户服务类:

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    @Qualifier("customerService")
    private CustomerDAO customerDAO;

    // ...
}

我的spring-service.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="customerService" class="com.fishingstore.service.implementation.CustomerServiceImpl">
        <property name="customerDAO" ref="customerDAO"/>
    </bean>
</beans>

我的错误在哪里?

2 个答案:

答案 0 :(得分:2)

更改CustomerService限定符以匹配bean ID或更好地完全删除@Qualifier

@Autowired
@Qualifier("customerService")
public void setCustomerService(CustomerService customerService) {

customerDAO

执行相同的操作
@Autowired
@Qualifier("customerDAO")
private CustomerDAO customerDAO;

答案 1 :(得分:2)

Servlets不受Spring容器管理。显然,该类中的任何@Autowired注释都不会被处理。

Spring在类SpringBeanAutowiringSupport中提供了两种静态方法 可以在servlet类中使用它来启用Autowiring功能。

  1. processInjectionBasedOnCurrentContext(Object target)
  2. processInjectionBasedOnServletContext(Object target, ServletContext servletContext)
  3. 使用第一种方法的示例是here,第二种方法的示例是here

    这两种方法背后的想法是覆盖Servlet's init方法并启用bean的自动装配。

    例如 -

    @Override
        public void init(ServletConfig config) throws ServletException{
            super.init(config);
            SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
    
        }
    

    确保按照 @Reimeus 答案中的指示正确自动装配咖啡豆。