在JMX方法调用后,从ioc检索的对象变为null

时间:2012-07-02 10:28:09

标签: java spring ioc-container jmx

我使用Spring 3.1作为独立应用程序。

我有一些奇怪的情况,我想我错过了一些东西。

我的类有我通过JMX控制台公开的方法。 该方法是通过bean调用方法。

我正在初步推荐的bean。 在我通过jmx控制台调用方法之后,bean实例变为null。

这是暴露的JMX bean:

public class TriggerBean implements IJmxTriggerBean
{
    static Logger logger = Logger.getLogger(TriggerBean.class);

    FeedListenerBean fe = null;

    public void start()
    {
        try
        {
            // init();
            PropertyConfigurator.configure(FixGWConstants.LOG4J_PATH);
            ApplicationContext context = new ClassPathXmlApplicationContext(FixGWConstants.APPLICATION_CONTEXT_XML);
            fe = (FeedListenerBean) context.getBean("FeedListenerBean");
            doTheListen();

        }
        catch (Throwable t)
        {
            logger.error(t);
            System.out.println(t);
        }

    }

        //thats the method being exposed by jmx. pay attention that fe object has been   initialized before by the start()
    public void doTheListen()
    {
        fe.listen();
    }

    private void init()
    {
        Resource resource = new ClassPathResource(System.getProperty("user.dir") + "//config.properties");
        try
        {
            Properties props = PropertiesLoaderUtils.loadProperties(resource);
            String log4jPath = props.getProperty("LOG4J_PATH");
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

我是通过独立测试的:

public static void main(String[] args) throws Exception
    {
protected TriggerBean trigger = new Trigger();
trigger.start();
}

的applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"


    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <!-- Must for auto wiring 
    <context:annotation-config />
    -->

    <context:component-scan base-package="com.fixgw.beans">
    </context:component-scan>



    <!-- start a JMX Server -->
    <bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean" />

    <bean id="FeedListenerBean" class="com.fixgw.beans.FeedListenerBean">
    </bean>

    <bean id="TriggerBean" class="com.fixgw.test.TriggerBean">
    </bean>





    <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
        <property name="beans">
            <map>
                <entry key="Server:name=HttpAdaptor">
                    <bean class="mx4j.tools.adaptor.http.HttpAdaptor">
                        <property name="port" value="8000" />
                        <property name="host" value="0.0.0.0" />
                        <property name="processor">
                            <bean class="mx4j.tools.adaptor.http.XSLTProcessor" />
                        </property>
                    </bean>
                </entry>
                <entry key="bean:name=TriggerBean" value-ref="TriggerBean" />

            </map>
        </property>
        <property name="listeners">
            <list>
                <!--
                    let the HttpAdapter be started after it is registered in the
                    MBeanServer
                -->
                <bean class="com.fixgw.jmx.HttpAdaptorMgr">
                    <property name="mbeanServer" ref="mbeanServer" />
                </bean>
            </list>
        </property>
    </bean>

</beans>

当我第一次使用start()时,对象fe做得很好。 但是在调用doTheListen之后(通过JMX,对象fe提醒null(尽管之前已经初始化了)

任何想法?

感谢, 射线。

0 个答案:

没有答案