2012-11-02 107 views
0

我有3種類型。彈簧依賴注入的NullPointerException

ServiceAffaire.java:

package ws.magicnew.sync; 

... 

@Transactional 
@Service("serviceAffaire") 
public class ServiceAffaire implements IServiceAffaire { 

    private static Log log = LogFactory.getLog(ServiceAffaire.class); 

    //private static SimpleDateFormat sdf = new SimpleDateFormat(MagicFacade.WS_DATE_FORMAT); 

    @Autowired 
    @Qualifier("manifestationService") 
    protected IManifestationService manifestationService; 

    @Autowired 
    @Qualifier("typeDeManifestationService") 
    protected ITypeDeManifestationService typeService; 

    @Autowired 
    @Qualifier("espaceManifestationService") 
    protected IEspaceManifestationService espaceManifService; 

    @Autowired 
    @Qualifier("siteService") 
    protected ISiteService siteService; 

    @Autowired 
    @Qualifier("natureService") 
    protected INatureService natureService; 

    @Autowired 
    @Qualifier("facadeGetAffaire") 
    protected MagicFacade facadeGetAffaire; 

    @Autowired 
    @Qualifier("compteurManifestation") 
    private Compteur compteurManifestation; 

    @Autowired 
    @Qualifier("compteurContenus") 
    protected Compteur compteurContenus; 

     public synchronized void synchronize(boolean setFlag) throws Exception { 
      ... 
     } 
} 

IServiceAffaire.java:

package ws.magicnew.sync; 

public interface IServiceAffaire { 

    public void synchronize(boolean setFlag) throws Exception; 

} 

和CacheAction.java:

package ws.magicnew.sync; 

... 

@Configurable 
@Transactional 
public class CacheAction extends DispatchAction { 

    private static Log log = LogFactory.getLog(CacheAction.class); 

    @Autowired 
    @Qualifier("serviceAffaire") 
    private IServiceAffaire serviceAffaire; 

    public ActionForward getAffaire(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 
     boolean setFlag = Boolean.parseBoolean(CmsProperties.SYNCRO_AFFAIRES_FLAG); 
     log.info("getAffaire debut " +setFlag); 
     serviceAffaire.synchronize(setFlag); // NullPointerException here: serviceAffaire is null 
     log.info("getAffaire fin " +setFlag); 
     request.setAttribute("message", "Le service get affaire a été lancé."); 

     return mapping.findForward("cache"); 
    } 
} 

佈線是在的applicationContext-scanpackages.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" xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:util="http://www.springframework.org/schema/util" 
    xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring" 
    xmlns:jaxws="http://cxf.apache.org/jaxws" 
    xmlns:cxf="http://cxf.apache.org/core" 
    xsi:schemaLocation=" 
      http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
      http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
      http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd 
      http://www.springframework.org/schema/jee 
       http://www.springframework.org/schema/jee/spring-jee-3.0.xsd 
      http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
      http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util-3.0.xsd 
      http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring 
       http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd 
      http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd 
      http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"> 
    <context:annotation-config /> 
    <context:component-scan base-package="ws.magicnew.sync" /> 
    <context:component-scan base-package="ws.magicnew.facade" /> 
</beans> 

這對我來說似乎沒問題,但當我調用CacheAction.getAffaire()方法時,我仍然會得到一個NullPointerException。我無法弄清楚爲什麼,這讓我發瘋。任何線索?

我以前有一些注入ServiceAffaire屬性的問題(我解決了這個問題),所以Spring實際上是自動裝配它們的。但由於某種原因,無法將ServiceAffaire注入到CacheAction中。

如代碼中的註釋所示,當調用ServiceAffaire.synchronize時,serviceAffaire屬性爲null,如我在調試模式下執行時所看到的。

+0

提示:青睞'@Resource(name = 「..」 )而不是'@ Qualifier/@ Autowired'組合。 –

+0

嗯..你不必給 smk

+0

@Sajit:你是對的。我忘了把它放在提問中。我會編輯它。 –

回答

2

你的問題可能是兩件事情:

首先,你需要添加「背景:春天配置」中的applicationContext-scanpackages.xml來激活AspectJ。

http://static.springsource.org/spring/docs/current/spring-framework-reference/html/aop.html#aop-atconfigurable

其次,你必須CACHEACTION爲@Configurable,我想,你這樣做,因爲你或一些框架創建CACHEACTION的情況下,採用了經典的「新CACHEACTION()」,而不是讓Spring來創建它。

如果是這種情況,如果您在Spring初始化IServiceAffaire bean之前通過代碼創建CacheAction實例,則NPE可能會發生。我的意思是,當你使用「new」創建一個CacheAction實例時,你必須確定Spring已經完成初始化CacheAction需要的所有bean。否則,Spring將無法注入依賴關係。

如果某些框架創建CacheAction的新實例並且無法控制何時創建這些實例,那可能會非常棘手。如果添加「依賴」之類的註釋,則無關緊要,因爲在完成創建需要注入的bean之前,Spring將無法保存該實例創建。

解決這個問題的一種方法是讓Spring初始化一些bean,它將啓動任何創建CacheAction的新實例的初始化,並在那裏添加一個「依賴」的IServiceAffaire bean。

無論如何,正確的解決方案取決於您的應用程序如何初始化。

+0

謝謝!這使我走上了正軌。 CacheAction的init在一個框架中完成,但它提供了另一個操作,DefaultAction,繼承DispatchAction,但處理自動裝配問題。所以我改變了繼承,現在它正在工作。 –

0

你可以嘗試改變了IServiceAffaire依賴注入註解@Resource,並檢查其是否正常工作

@Autowired 
@Qualifier("serviceAffaire") 
private IServiceAffaire serviceAffaire; 

更改爲

@Resource(name ="serviceAffaire") 
private IServiceAffaire serviceAffaire;