0

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

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

我有一个类,该类具有我通过 JMX 控制台公开的方法。该方法正在通过 bean 调用方法。

我正在初始化的那个bean。奇怪的是,在我通过 jmx 控制台调用该方法后,bean 实例变为空。

这就是暴露的 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();
}

应用程序上下文.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(虽然它之前已经被初始化)

任何想法?

谢谢,雷。

4

0 回答 0