Spring部署到Tomcat的简单应用程序

时间:2012-09-05 09:25:38

标签: java spring tomcat jmx

我有一个带spring的简单独立应用程序(Main class + bean class)。它创建了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;
}

cont.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上运行相同的示例? 谢谢!

1 个答案:

答案 0 :(得分: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中配置的所有bean。

相关问题