Udo*_*eld 5 java spring parameter-passing command-line-arguments
我想编写一个spring命令行程序,该程序使用属性文件进行初始化,该属性文件作为命令行参数传递.怎么办?
上课:
public static void main (String [] args) {
String configFilename = args[0];
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"classpath:/context/applicationContext.xml");
MyBean bean = ctx.getBean(MyBean.class);
bean.getStarted();
}
Run Code Online (Sandbox Code Playgroud)
applicationContext.xml中:
<context:property-placeholder location="CONFIGFILENAME" ignore-unresolvable="true"/>
Run Code Online (Sandbox Code Playgroud)
如何从我的main方法获取配置文件名到实际的spring上下文,以便我可以加载正确的依赖于环境的属性?
在您的情况下,您可以更好地为属性文件位置设置系统属性
System.getProperties().setProperty("location", args[0]);
Run Code Online (Sandbox Code Playgroud)
然后在applicationContext.xml文件中
<context:property-placeholder location="${location}" ignore-unresolvable="true"/>
Run Code Online (Sandbox Code Playgroud)
希望这能解决你的问题.
| 归档时间: |
|
| 查看次数: |
7236 次 |
| 最近记录: |