2016-10-27 6 views
2

私はspring-bootアプリケーションを持っています。Springのapplication.propertiesから@Valueプロパティをリロードする方法は?

dir/config/application.properties

アプリケーションが起動すると、それはファイルからの値を使用してにそれらを注入:

@Value("${my.property}") 
private String prop; 

質問:どのようにすることができ、実行フォルダの下に、追加の設定ファイルがありますそれらの@Valueプロパティのリロードをトリガーしますか? 実行時にapplication.propertiesの設定を変更し、@Valueフィールドを更新したい(アプリ内の/reloadサーブレットを呼び出すことによってその更新をトリガーすることができます)。

どのようにですか?

+0

あなたはここで見ているのhttp代わりに@valueを使用するのではなく、あなたが最新のプロパティを望んでいたたびにあなたが使用する

@EnableScheduling @PropertySource("classpath:/config.properties") public class HelloWorldConfig { } 

@Component public class PropertyLoader { @Autowired private StandardEnvironment environment; @Scheduled(fixedRate=1000) public void reload() throws IOException { MutablePropertySources propertySources = environment.getPropertySources(); PropertySource<?> resourcePropertySource = propertySources.get("class path resource [config.properties]"); Properties properties = new Properties(); InputStream inputStream = getClass().getResourceAsStream("/config.properties"); properties.load(inputStream); inputStream.close(); propertySources.replace("class path resource [config.properties]", new PropertiesPropertySource("class path resource [config.properties]", properties)); } } 

あなたの主な設定は次のようになります://stackoverflow.com/questions/27919270/set-override-spring-spring-boot-properties-at-runtime? – Mitchapp

+0

'spring-cloud'を' @Service'に '@RefreshScope'と、プロパティーを変更して' localhost:8080/my-app/refresh'でPOSTリクエストを実行して解決しました。 – membersound

答えて

2

config.propertiesを1秒ごとに再読み込みするには、以下のBeanを使用します。

environment.get("my.property"); 
関連する問題