Spring,原型和MBeanExporter [英] Spring, prototypes, and MBeanExporter

查看:107
本文介绍了Spring,原型和MBeanExporter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Spring与它的MBeanExporter结合使用.我有一个原型bean定义,仅在调用ApplicationContext.getBean()时才实例化它.但是,MBeanExporter在引导容器时(错误地)实例化了原型bean的实例.

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.

在我看来,这似乎是一种常见的情况,所以我觉得我一定缺少一些东西.重要的是,不要提前实例化我的原型,并且可以使用MBeanExporter简化JMX集成.谁能解释我在做什么错?

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?

作为参考,我的spring配置看起来像这样:

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>

推荐答案

您可以使用MyJmxAutodetectExclude之类的自定义标记界面,并扩展Spring MetadataMBeanInfoAssembler.这样,您无需在每次重构代码时都对context.xml进行调整

You could use a custom marker interface like MyJmxAutodetectExclude and extend Spring MetadataMBeanInfoAssembler. This way you don't need to tweak your context.xml each time you refactor your code

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

这篇关于Spring,原型和MBeanExporter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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