[英]NPE when using @Autowired property (but not when using ApplicationContext to set property)
我正在嘗試使用@Autowired批注在jax-rs restful服務上設置屬性,但是當引用該屬性時,我會得到一個空指針異常。 這是我第一次嘗試使用此批注。
package com.pallelli.mvcpract.rest;
import javax.servlet.ServletContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.pallelli.hibpract.policymodel.PolicyDao;
import com.pallelli.hibpract.policymodel.beans.Risk;
@Service("riskService")
@Path("risk")
@Component
public class RiskService {
//@Context
//private ServletContext context;
@Autowired
private PolicyDao policyDao;
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response storeRisk(Risk risk) {
//ApplicationContext ctx =
//WebApplicationContextUtils.getWebApplicationContext(context);
//policyDao = ctx.getBean(PolicyDao.class);
policyDao.addRisk(risk);
risk.setName(risk.getName()+" : processed");
return Response.ok(risk).status(200).build();
}
}
如果刪除注釋,則一切正常,以便使用應用程序上下文創建policyDao,因此,我認為spring已經意識到了bean。
我在mvc-dispatcher-servlet.xml中使用以下內容來獲取spring以查找bean。
<context:component-scan base-package="com.pallelli.mvcpract.rest" />
<context:component-scan base-package="com.pallelli.hibpract.policymodel" />
這是PolicyDao類(我知道這是“錯誤的”)
package com.pallelli.hibpract.policymodel;
import org.hibernate.Session;
import org.springframework.stereotype.Component;
import com.pallelli.hibpract.policymodel.beans.Risk;
@Component
public class PolicyDao {
public void addRisk(Risk risk) {
Session session = null;
try {
session = Main.getSessionFactory().openSession();
session.beginTransaction();
session.persist(risk);
session.getTransaction().commit();
}
finally {
if(session != null) session.close();
}
}
}
調試日志似乎表明自動裝配有效
20:10:41 DEBUG DefaultListableBeanFactory:220 - Creating shared instance of singleton bean 'riskService'
20:10:41 DEBUG DefaultListableBeanFactory:449 - Creating instance of bean 'riskService'
20:10:41 DEBUG InjectionMetadata:71 - Registered injected element on class [com.pallelli.mvcpract.rest.RiskService]: AutowiredFieldElement for private com.pallelli.hibpract.policymodel.PolicyDao com.pallelli.mvcpract.rest.RiskService.policyDao
20:10:41 DEBUG DefaultListableBeanFactory:523 - Eagerly caching bean 'riskService' to allow for resolving potential circular references
20:10:41 DEBUG InjectionMetadata:85 - Processing injected method of bean 'riskService': AutowiredFieldElement for private com.pallelli.hibpract.policymodel.PolicyDao com.pallelli.mvcpract.rest.RiskService.policyDao
20:10:41 DEBUG DefaultListableBeanFactory:220 - Creating shared instance of singleton bean 'policyDao'
20:10:41 DEBUG DefaultListableBeanFactory:449 - Creating instance of bean 'policyDao'
20:10:41 DEBUG DefaultListableBeanFactory:523 - Eagerly caching bean 'policyDao' to allow for resolving potential circular references
20:10:41 DEBUG DefaultListableBeanFactory:477 - Finished creating instance of bean 'policyDao'
20:10:41 DEBUG AutowiredAnnotationBeanPostProcessor:427 - Autowiring by type from bean name 'riskService' to bean named 'policyDao'
20:10:41 DEBUG DefaultListableBeanFactory:477 - Finished creating instance of bean 'riskService'
...
20:18:45 DEBUG DefaultListableBeanFactory:247 - Returning cached instance of singleton bean 'policyDao'
關於為何RiskService上的autowired屬性為null的任何想法?
看來jax-rs無法處理Spring注釋。
為了使jax-rs對象了解Spring Bean,還需要一些其他設置。 沒有正確的初始化,自動連線,事務性或任何其他Spring注釋都不會被處理。
假設您使用的是Jersey和Spring 3,則需要包括一個提供在Jersey和Spring之間的“橋梁”的庫:
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>2.22.1</version>
</dependency>
有一個單獨的庫為Spring 4提供支持。有關詳細信息,請參見Jersey 文檔和本示例 。
您設法獲得了ApplicationContext,因為它作為屬性存儲在ServletContext中,因此可以使用靜態方法調用從應用程序中的任何位置檢索它。 如您所見,bean已正確初始化,但是兩個框架都無法互相通信。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.