2012-09-05 5 views
0

스프링 (Main 클래스 + Bean 클래스)이있는 간단한 독립형 애플리케이션이 있습니다. MBean (JMX)을 작성합니다.Tomcat에 Spring을 배포하는 간단한 애플리케이션

그냥 콩을 시작합니다.

메인 클래스 :

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> 

? 감사합니다.

+0

'cont.xml'의 관련 부분을 게시하십시오. Spring이 관리하는'ApplicationContext'와 빈을 어떻게 사용하는지 설명하십시오. 지금은 코드에서 JMX와 Spring 사용을 볼 수 없다. –

+0

완료, 감사합니다! :) –

답변

2

사용

당신의 web.xml
<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> 

. 그러면 cont.xml에 구성된 모든 bean이 인스턴스화됩니다.

+0

그리고 나는 메인 클래스를 제거해야합니까? 감사! –

관련 문제