spring @PostConstruct not firing in JBoss7
Asked Answered
V

2

7

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;
            }
        }

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

Valency answered 28/2, 2014 at 19:32 Comment(2)
Could you post which jars you have in your WEB-INF/lib? Make sure there isn't an API jar including the @PostConstruct annotation in there. JBoss can be quite picky about classes and class loading.Bacteria
Hi M. Deinum, sorry for the delay in replying, I didn't get a notification that somebody had replied. I have found that if I place my spring jars in my WEB-INF/lib directory, and remove them from the modules then the @PostConstruct annotation works.Valency
V
2

Thanks to suggestion from M. Deinum I was able to fix this issue. I had to place the spring jars in my WEB-INF/lib directory and remove them from the modules directory.

Valency answered 7/3, 2014 at 11:27 Comment(0)
C
5

Adding the dependency

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

to the spring module works for me.

Cath answered 8/1, 2015 at 3:22 Comment(2)
Worked for me (JBoss 6.4 and Spring Boot 1.2.7.RELEASE). I was having the same issue.Bouleversement
In my case, we have bbb.war bundled in aaa.ear file. In META-INF/jboss-deployment-structure.xml of aaa.ear, there is module dependency com.demo.library. After we added <<module name="javax.annotation.api"/> to library's module.xml, problem got solved.Lemonade
V
2

Thanks to suggestion from M. Deinum I was able to fix this issue. I had to place the spring jars in my WEB-INF/lib directory and remove them from the modules directory.

Valency answered 7/3, 2014 at 11:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.