Spring服务未注入Web servlet [英] Spring service not injected in web servlet
问题描述
我正在使用Spring DI并试图在自己的servlet中注入Spring服务。但是,它没有注入并且保持 null
,从而导致 NullPointerException
。
I am using Spring DI and trying to inject a Spring service in my servlet. However, it isn't injected and stays null
, causing 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:
My 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;
// ...
}
我的春天- service.xml:
My 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>
我的错误在哪里?
推荐答案
Servlet
不是由 Spring
容器管理的。因此,显然该类中的任何 @Autowired
批注都不会被处理。
Servlets
are not managed by Spring
Container. So apparently any @Autowired
annotations in that class will not be processed.
Spring在该类中提供了两个静态方法 SpringBeanAutowiringSupport
,可在servlet类中使用以启用自动装配
功能。
Spring provides two static methods in the class SpringBeanAutowiringSupport
which can be used in the the servlet classes to enable Autowiring
feature.
- processInjectionBasedOnCurrentContext(对象目标)
- processInjection BasedOnServletContext(Object target,
ServletContext servletContext)
- processInjectionBasedOnCurrentContext(Object target)
- processInjectionBasedOnServletContext(Object target, ServletContext servletContext)
An example using the first method is here and for the second method is here
两种方法是重写 Servlet的
init
方法并启用bean的自动装配。
The idea behind the two approaches is to override the Servlet's
init
method and enable the autowiring of beans.
例如-
@Override
public void init(ServletConfig config) throws ServletException{
super.init(config);
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}
请确保您按照<强> @Reimeus 答案。
这篇关于Spring服务未注入Web servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!