2016-04-08 10 views
4

動的コンフィグレーションの変更後に以前に@Autowiredスプリングオブジェクトをリフレッシュするにはどうすればよいですか? myApplicationContextAware.applicationContext.getBean(MyClass.class)新しい設定を行った後、どのようにすべてのスプリングオブジェクトを更新できますか?

// Here is my updateConfig method 

GenericApplicationContext context = new GenericApplicationContext(); 
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(context); 
reader.loadBeanDefinitions(new ClassPathResource("applicationContext.xml")); 
context.refresh(); 

myApplicationContextAware.setApplicationContext(context); 

私は新しい設定で新しいインスタンスを取得することができますが、すべての@Autowiredのオブジェクトがまだ古い値

が含まれている春のオブジェクトをリフレッシュするために任意の解決策はありますか?

答えて

2

これにはAbstractRefreshableApplicationContextを使用できます。実行時にBean構成を再ロードするメソッドを提供します。

スプリングブートを使用している場合はspring actuator@RefreshScope注釈を使用できます。スプリングアクチュエータエンドポイント/refreshは@RefreshScopeで注釈が付けられた豆を再ロードします。

+0

春のブーツは完璧に動作します、ありがとうございます! そうでなければ、AbstractRefreshableApplicationContextは私のためには機能しませんが、新しい設定をロードしますが、最初のautowired beanには古い値が含まれています。 – oedural

+0

確認していただき、ありがとうございます。 – 11thdimension

関連する問題