【问题标题】:MBeanExporter and conditional loading of a beanMBeanExporter 和 bean 的条件加载
【发布时间】:2015-10-20 18:06:58
【问题描述】:

我想使用 MBeanExporter 来公开一个 Mbean。但是,要导出的 bean 不一定总是存在。应该如何处理:

<bean id="mybean" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
    <property name="beans">
        <map>

            <entry key="bean:name=DatabaseDataSource" value="#{datasource.getPool().getJmxPool()}"/>

        </map>
    </property>
</bean>

可能是 getJmxPool() 有时在导出失败时返回 null。如果已知特定属性在设置时会导致 this 返回 null,如何使其成为有条件的(不暴露 mbean)。

【问题讨论】:

    标签: java spring jmx mbeans


    【解决方案1】:

    看看spring环境配置文件:

    http://gordondickens.com/wordpress/2012/06/12/spring-3-1-environment-profiles/

    如果特定环境或配置标志不应实例化 bean,只需使用 @Profile 使用特定配置文件标记 bean。如果 bean 与活动配置文件不匹配,Spring 不会尝试实例化 bean。

    【讨论】:

    • 是否可以使用通过propertyplaceholderconfigurer加载的属性来决定配置文件激活
    • 您可以使用getEnvironment().addActiveProfile("profile") 以编程方式设置活动配置文件。然后,您可以将其设为个人资料的名称或有条件地设置您选择的个人资料
    • 可以在propertyplaceholderconfigurer加载属性后激活profile吗?
    • 是的,可以使用我上面提到的方法。
    猜你喜欢
    • 1970-01-01
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-27
    • 1970-01-01
    相关资源
    最近更新 更多