春天@PostConstruct不在JBoss7中触发 [英] spring @PostConstruct not firing in JBoss7

查看:61
本文介绍了春天@PostConstruct不在JBoss7中触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将在WebSphere和Netweaver中运行的应用程序更新为在JBoss6.2 EAP中运行之后,我遇到了麻烦.

我发现,带有init()方法并带有@PostConstruct(javax.annotation.PostConstruct)注释的spring-Managed @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布尔值的值保持为false(启动后很长一段时间)

上面的类在一个捆绑在war/WEB-INF/lib中的jar中.

从春季的日志中可以看到,UserRoleDao类正在自动装配到使用@Autowired批注引用的类中.

spring jars安装在JBoss的JBOSS_HOME \ modules \ com \ company \ thirdparty \ main中,并由module.xml文件正确引用(由于大多数应用程序是受Spring管理的,所以我知道它们已正确引用). /p>

spring上下文使用类扫描,如以下来自spring上下文xml文件的摘录所示:

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

所以,奇怪的是spring能够将UserRoleDao类自动连接到使用它的Service类中,但是@PostConstruct似乎被忽略了.

我尝试将弹簧罐子移动到WEB-INF \ lib目录中(我发现以前在Hibernate上存在的问题是,如果在JBOSS_HOME \ modules中引用了罐子,则注释不会被扫描并将其移动到WEB-INF中\ lib目录修复了该问题.)

其他任何人以前都注意到过类似的问题吗? (并找到了解决方案!)

@PostConstruct初始化方法在部署到WebSphere&中时会被触发. Netweaver使用相同的Spring版本jars.

很抱歉,如果我在错误的地方张贴了此内容,请告诉我,我将其移走.

谢谢

版本:

JBoss:EAP 6.2.0.GA(基于AS 7.3.0构建)

春季:3.1.1

解决方案

感谢M. Deinum的建议,我得以解决此问题.我必须将弹簧罐子放在我的WEB-INF/lib目录中,并将其从modules目录中删除.

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

解决方案

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.

这篇关于春天@PostConstruct不在JBoss7中触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆