0

I am using Spring in conjunction with its MBeanExporter. I have a prototype bean definition which should only be instantiated when a call to ApplicationContext.getBean() is made. However, the MBeanExporter is (incorrectly) instantiating an instance of the prototype bean when bootstrapping the container.

I found this bug report from ages ago, with no notable response.

This seems to me like it must be a common scenario, so I feel like I must be missing something. It's important that my prototype not be instantiated ahead of time, and that I can use MBeanExporter to make my JMX integration simpler. Can anyone explain what I'm doing wrong?

For reference, my spring config looks like this:

<bean id="foo" class="MyPrototypeClassName" scope="prototype"/>

<bean id="namingStrategy" class="org.springframework.jmx.export.naming.IdentityNamingStrategy"/>

<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
    <property name="namingStrategy" ref="namingStrategy"/>
    <property name="autodetect" value="true"/>
</bean>
4

2 回答 2

0

您可以使用自定义标记接口,例如MyJmxAutodetectExclude并扩展 Spring MetadataMBeanInfoAssembler。这样你就不需要在每次重构代码时调整你的 context.xml

public class MyMBeanInfoAssembler extends MetadataMBeanInfoAssembler {

    @Override
    public boolean includeBean(final Class<?> beanClass, final String beanName) {
        if (super.includeBean(beanClass, beanName)) {
            List<Class<?>> list = Arrays.asList(beanClass.getInterfaces());
            if (list.contains(MyJmxAutodetectExclude.class)) {
                return false;
            }
            return true;
        }
        return false;
    }
}
于 2014-07-03T06:00:02.590 回答
0

我找到了一些解决方法,但它很笨拙,所以我怀疑这不是最佳实践。我只是将我的导出器配置更改为:

<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
    <property name="namingStrategy" ref="namingStrategy"/>
    <property name="autodetect" value="true"/>
    <property name="excludedBeans">
        <list>
            <value>foo</value>
        </list>
    </property>
</bean>

这样,自动导出器就会忽略我的原型 bean。因此,我的原型 bean 现在可以引用MBeanExporter(以前这会导致无法解决的依赖循环)。所以现在我的原型可以在适当的时候在 JMX 中注册和注销自己。

如果有人可以权衡这是否是一种好/坏的方法,我将不胜感激。

于 2012-10-05T14:24:38.037 回答