2015-10-18 58 views
0

我想使用@Autowired註釋來設置jax-rs restful服務的屬性,但我得到一個空指針異常時屬性被引用。這是我第一次嘗試使用這個註釋。NPE當使用@Autowired屬性(但不是當使用ApplicationContext設置屬性時)

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,因此我認爲,春天正在意識到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上的自動裝配屬性爲空?

+0

更多信息。出於某種原因,我得到了兩個創建的風險服務對象。自動裝配是爲第一個而不是第二個工作的,並且是在調用其餘服務時使用的第二個。 –

回答

0

看來jax-rs無法處理Spring批註。

需要一些額外的設置才能使jax-rs對象知道Spring Beans。如果沒有正確的初始化,Autowired,Transactional或其他Spring註解都不會被處理。

假設你使用的是新澤西州和Spring 3你需要包括庫,提供了新澤西州和春天之間的「橋樑」:

<dependency> 
    <groupId>org.glassfish.jersey.ext</groupId> 
    <artifactId>jersey-spring3</artifactId> 
    <version>2.22.1</version> 
</dependency> 

沒有提供對Spring 4.支持細節一個單獨的庫見澤西documentation和這example

您設法獲得ApplicationContext,因爲它作爲ServletContext中的一個屬性存儲,因此可以使用靜態方法調用從應用程序中的任何位置檢索它。正如你注意到bean被正確初始化的,但是兩個框架都不會彼此交談。

+0

自發布我的原始問題後,我確實找到了使用jersey-spring3的參考,但在我的pom中包括這一點並沒有幫助。 –

相關問題