0

我有一个简单的带有 spring 的独立应用程序(主类 + bean 类)。它创建 MBean (JMX)。

它只是启动了我的 bean。

主类:

public class Main {
public static void main(final String[] args) {
    ApplicationContext ac = new ClassPathXmlApplicationContext("cont.xml");
    try {
        Thread.sleep(1000 * 60 * 5);
    } catch (final Throwable t) {}
}

}

public class Test {
private String val = "";
public String getVal() {
    return val;
}
public void setVal(String v) {
    val = v;
}

续.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd"
    default-lazy-init="true">
    <bean id="test" class="test.Test" />
    <bean class="org.springframework.jmx.support.MBeanServerFactoryBean">
        <property name="locateExistingServerIfPossible" value="true" />
    </bean>
    <bean class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
        <property name="assembler">
            <bean class="org.springframework.jmx.export.assembler.MethodNameBasedMBeanInfoAssembler"            >
                <property name="managedMethods">
                    <list>
                        <value>getVal</value>
                        <value>setVal</value>
                    </list>
                </property>
            </bean>
        </property>
        <property name="beans">
            <map>
                <entry key="bean:name=Test" value-ref="test"/>
            </map>
        </property>
    </bean>
</beans>

如何在 tomcat 上运行相同的示例?谢谢!

4

1 回答 1

2

利用

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:cont.xml</param-value>
</context-param>

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

在你的web.xml. 这将实例化所有配置在cont.xml.

于 2012-09-05T09:29:45.723 回答