2012-10-02 73 views
0

我已經定義了一個Spring bean的bean的方法:如何調用命令行

<bean id="myBean" class="package.MyBean"> 
    <property name="name1" ref="otherBean" /> 
    <property name="name2" vallue="2" /> 
</bean> 

而且我知道,它實現了一定的方法,例如MyBean.execute()

我可以從命令行啓動此方法嗎?怎麼樣? (喜歡的東西java -jar ... myBean.execute

回答

0

只需加載它的主要方法,查找這個bean並調用該方法是這樣的:

public class Main { 
    public static void main(String[] args) { 
     ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:yourcontext.xml"); 
     ctx.registerShutdownHook(); 
     MyBean myBean = ctx.getBean("myBean", MyBean.class); 
     myBean.execute(); 
    } 
}