2012-08-07 6 views
5

コマンドラインパラメータとして渡されるプロパティファイルで初期化されたスプリングコマンドラインプログラムを作成したいと思います。それはどうやってできますか?コマンドラインからスプリングコンテキストのプロパティをロード

開始クラス:

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(); 
} 

applicationContext.xmlを:私は正しいをロードできるように、私は、実際の春のコンテキストに私のメインメソッドから設定ファイル名を乗り越えるにはどうすればよい

<context:property-placeholder location="CONFIGFILENAME" ignore-unresolvable="true"/> 

環境依存のプロパティ?あなたのケースでは

+2

代わりにJVM引数を使用できます。 http://stackoverflow.com/questions/5598217/how-do-i-read-jvm-arguments-in-the-spring-applicationcontext-xml – DB5

答えて

6

は、あなたがより良いapplicationContext.xmlをして、これはあなたの問題を解決します

<context:property-placeholder location="${location}" ignore-unresolvable="true"/> 

希望を提出

System.getProperties().setProperty("location", args[0]); 

[プロパティファイルの場所のためのシステムプロパティを設定することができます。

関連する問題