2016-02-07 24 views
0

我正在使用Spring DI,並試圖在我的servlet中注入Spring服務。但是,它沒有被注入並保持null,導致NullPointerException沒有注入到web servlet中的Spring服務

我的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. 
     // ... 
    } 

} 

我的彈簧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中:

<?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> 

哪裏是我的錯?

+0

您正在使用'registerServlet'限定符在您的servlet中注入'CustomerService' bean。我懷疑這是否正確。 –

+0

我在不使用@Qualifier的情況下在我的servlet中注入bean,它不起作用 –

+0

不要問爲什麼得到'NullPointerException'。 [每個Java開發人員都知道這個答案](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it)。相反,問爲什麼給定的變量是'空'。我從這個問題中減少了不相關的代碼,使它更好地集中。 – BalusC

回答

1

Servlets不受Spring容器管理。顯然,該類中的任何@Autowired註釋都不會被處理。

Spring提供,其中可以在該Servlet類用於啓用Autowiring特徵類SpringBeanAutowiringSupport
兩個靜態方法。

  1. processInjectionBasedOnCurrentContext(Object target)
  2. processInjectionBasedOnServletContext(Object target, ServletContext servletContext)

使用第一種方法的一個例子是here和用於第二方法是here

兩種方法背後的想法是重寫Servlet'sinit方法和啓用bean的自動裝配。

例如 -

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

    } 

確保正確配置爲自動連接豆子@Reimeus回答指出。

+0

爲我工作。謝謝 –

+0

謝謝你讓我知道。 –

2

更改CustomerService預選賽匹配的bean ID或更好的去除@Qualifier完全

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

執行相同的customerDAO

@Autowired 
@Qualifier("customerDAO") 
private CustomerDAO customerDAO; 
+0

這是行不通的,它會拋出同樣的異常。 –

+0

查看更新.......... – Reimeus

+0

不工作... –