2014-02-28 37 views
6

我在更新在WebSphere和Netweaver中運行的應用程序以在JBoss6.2 EAP中運行後遇到問題。Spring @PostConstruct在JBoss7中沒有觸發

我發現用@PostConstruct(javax.annotation.PostConstruct)註解的init()方法的spring管理的@Repository(org.springframework.stereotype.Repository)沒有init()方法運行當部署在JBossEAP 6.2.0中時。

的類看起來像下面這樣:

 package com.company.productname.api.dao.impl; 


     // ... imports removed .... 

     @Repository 
     public class UserRoleDao extends AbstractBaseDao { 

      private static final Log LOG = LogFactory.getLog(UserRoleDao.class); 

      private boolean testInitInvoked = false; 


      // .... some code removed .... 

      /** 
      * Prepare the caches used to lookup market roles 
      */ 
      @PostConstruct 
      protected void init() { 
      testInitInvoked = true; 
      if (LOG.isDebugEnabled())LOG.debug("UserRoleDao.init() method called"); 

         // .. . . . some code removed ...... 
       } 


      @Override 
      public Mask getMask(final String aMaskName) { 
       LOG.debug("getRoleMask entered, testInitInvoked = [" + testInitInvoked + "]- aMaskName = " + aMaskName); 

       Mask myMask = masksByName.map().get(aMaskName); 

       if (myMask != null) { 
        myMask.setMembers(this.getMembersForMaskId(myMask.getId())); 
       } 

       LOG.debug("getRoleMask returning - myMask = " + myMask); 

       return myMask; 
      } 
     } 

我可以從日誌看到的是,在init方法記錄是沒有得到記錄,以及testInitInvoked布爾值保持在假該類由應用程序使用(啓動後很長時間)。

上面的類位於一個捆綁到war/WEB-INF/lib中的jar中。

我可以從Spring日誌中看到,UserRoleDao類正在被自動裝入類中,它被@Autowired註解引用。

Spring JBars被安裝在JBoss的JBOSS_HOME \ modules \ com \ company \ thirdparty \ main中,並被module.xml文件正確引用(因爲大多數應用程序都是spring管理的,我知道它們被正確引用)。

Spring上下文中使用類掃描如從Spring上下文xml文件摘錄如下:

<context:component-scan base-package="com.company.productname.api" /> 

因此,奇怪的是,彈簧能自動裝配的UserRoleDao類爲服務類使用它,但@PostConstruct似乎被忽略。

我試過將Spring Jars移動到WEB-INF \ lib目錄中(我在Hibernate的前幾個問題中發現,如果在JBOSS_HOME \ modules中引用jar並將它們移動到WEB- INF \ lib目錄固定的那個)。

有沒有人注意過類似的問題? (並找到解決方案!)

@PostConstruct init方法在部署在WebSphere中時被觸發& Netweaver使用相同的彈簧版本jar。如果我在錯誤的地方發佈了這個消息,請道歉,我會將其移動。

感謝,

版本:

的JBoss:EAP 6.2.0.GA(建立在AS 7.3.0)

春:3.1.1

+2

你可以發佈你的WEB-INF/lib中有哪些罐子嗎?確保那裏沒有包含'@ PostConstruct'註解的API jar。 JBoss對於類和類的加載可能相當挑剔。 –

+0

Hi M. Deinum,對於延遲迴復感到抱歉,我沒有收到有人回覆的通知。 我發現如果將我的彈簧罐放在我的WEB-INF/lib目錄中,並將它們從模塊中移除,那麼@PostConstruct註釋就可以工作。 –

回答

2

由於從M個建議Deinum我能解決這個問題。我必須將彈簧罐放在我的WEB-INF/lib目錄中,並將它們從modules目錄中刪除。

2

添加依賴

<module name="javax.annotation.api" export="true"/>

彈簧模塊爲我工作。

+0

爲我工作(JBoss 6.4和Spring Boot 1.2.7.RELEASE)。我遇到了同樣的問題。 – Cleankod