開始時に Spring コンテキストにプロパティ値を注入したいと考えています。Spring 3.1 の新しい Environment および PropertySource 機能を使用してこれを実行しようとしています。
Spring コンテキストをロードするクラスで、独自の PropertySource クラスを次のように定義します。
private static class CustomPropertySource extends PropertySource<String> {
public CustomPropertySource() {super("custom");}
@Override
public String getProperty(String name) {
if (name.equals("region")) {
return "LONDON";
}
return null;
}
次に、このプロパティ ソースをアプリケーション コンテキストに追加します。
ClassPathXmlApplicationContext springIntegrationContext =
new ClassPathXmlApplicationContext("classpath:META-INF/spring/ds-spring-intg-context.xml");
context.getEnvironment().getPropertySources().addLast( new CustomPropertySource());
context.refresh();
context.start();
私の Bean の 1 つで、プロパティ値にアクセスしようとしました。
@Value("${region}")
public void setRegion(String v){
...
}
次のエラーが表示されます。
java.lang.IllegalArgumentException: 原因: java.lang.IllegalArgumentException: 文字列値 [${region}] のプレースホルダー 'region' を解決できませんでした
どんな助けでも大歓迎です