2012-09-05 79 views
0

我有一個簡單的獨立應用程序,使用spring(Main class + bean class)。它創建MBean(JMX)。將彈簧部署到Tomcat的簡單應用程序

它只是啓動我的豆。

主類:

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相同的例子嗎? 謝謝!

+0

請發佈'cont.xml'的相關部分。請描述你如何使用'ApplicationContext'和Spring管理的bean。現在我的代碼中沒有看到JMX和Spring的用法。 – 2012-09-05 09:46:44

+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。

+0

我應該擺脫主要類,是嗎?謝謝! –

相關問題