1 Reply Latest reply on Mar 7, 2014 6:35 AM by hindsp

    @PostConstruct method not getting fired in Spring @Repository

    hindsp

      I am having trouble after updating an application that runs in WebSphere and Netweaver to run in JBoss6.2 EAP.


      I have found that a spring-managed @Repository (org.springframework.stereotype.Repository) with an init() method annotated with @PostConstruct (javax.annotation.PostConstruct) is does not have the init() method run when deployed in JBossEAP 6.2.0.


      The class looks something like the following:


      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;

          }

      }

       

      (sorry, I submitted before I had finished typing, continuing now)

      What I can see from the logging is that the logging in the init method is not getting logged, and the value of the testInitInvoked boolean stays as false when the class is used by the application (a good length of time after startup).

       

      The class above is in a jar bundled into the war/WEB-INF/lib.

       

      I can see from the spring logging that the UserRoleDao class is being autowired into the class where it is referenced with an @Autowired annotation.

       

      The spring jars are installed in JBoss at JBOSS_HOME\modules\com\company\thirdparty\main and are referenced by the module.xml file correctly (as most of the app is spring managed I know they are referenced correctly).

       

      The spring context uses class scanning as shown in the following excerpt from the spring context xml file:

       

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

       

      So, the strange thing is that spring is able to autowire the UserRoleDao class into the Service class that uses it, but the @PostConstruct seems to be ignored.

       

      I have tried moving the spring jars into the WEB-INF\lib directory (I found with previous issues around Hibernate that annotations weren't getting scanned if jars were referenced in the JBOSS_HOME\modules and moving them into the WEB-INF\lib directory fixed that).

       

      Has anybody else noticed a similar problem before? (and found a solution!)

       

      The @PostConstruct init method does get fired when deployed in WebSphere & Netweaver using the same spring version jars.

       

      Apologies if I have posted this in the wrong area, please let me know and I will move it.

       

      Thanks,

       

       

      Versions:

      JBoss: EAP 6.2.0.GA (built on AS 7.3.0)

      Spring: 3.1.1