2012-10-02 160 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(); 
    } 
}