how can I reload/refresh properties in spring at runtime without jvm restart?
I am looking for something elegant and it should be applicable in Prod. Therefore, JRebel is off the table I guess.
Currently, I have my properties in MYProject.properties
oms.url=abbc.com
checkin.enabled=true
and my java file is autowired to search and use these properties from applicationContext provided different property files:
<bean id="applicationProperties" class="myproject.config.CustomPropertyPlaceHolder">
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath:MyProject.properties</value>
<value>file:${CATALINA_BASE}/lib/MyProject.properties</value>
<!--<value>file:///appl/conf/outlet.properties</value>-->
<value>classpath:db.password</value>
<value>file:${CATALINA_BASE}/lib/db.password</value>
<!-- <ref bean="applPropertiesFromDb" /> -->
</list>
</property>
</bean>
and Java file as:
@Value("${checkin.enabled}")
private String checkinEnabled;
Use ReloadableResourceBundleMessageSource
.
But this will not be refreshed.
private String checkinEnabled;
You must reload checkinEnabled
variable.
Consider implementing MessageSource Spring bean like this:
1.Update applicationContext.xml
<bean id="messageSource" class="com.mycompany.service.DynamicMessageSource" />
2.Write your own MessageSource implementation
public class DynamicMessageSource extends AbstractMessageSource {
// Autowire propertiesService
@Override
protected MessageFormat resolveCode(String code, Locale locale) {
String result = propertiesService.getProperty(code);
return new MessageFormat(result, locale);
}
}
3.Implement your custom service to read properties from some place (DB, properties file, etc).
4.Enjoy dynamic updates of properties when updating the properties source.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.