2

For spring framework, I want to manually reload data inside properties file. Actually, have to write reload servlet that will manually reload data when I manually run this servlet file.

I have already defined spring configuration for messageSource.

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
        p:basename="classpath:/message" />

But don't want to autoreload at certain amount of time for example can autoreload when setting:

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
            p:basename="classpath:/message"
            p:cacheSeconds="1" />

I tried before by clearCaches() but not autoreload.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Tun Lin Aung
  • 339
  • 1
  • 4
  • 17

2 Answers2

1

It is working now. Need to inject messageSource into servlet file and call clearCache(). It does clear previous properties data and reload updated properties file.

In ReloadServlet.java,

ReloadableResourceBundleMessageSource rs = Global.getBean("messageSource", ReloadableResourceBundleMessageSource.class);
rs.clearCache();

In Global.java,

private static ApplicationContext context;

public static <T> T getBean(String s, Class<T> type) {
        return context.getBean(s, type);
}

Thanks.

Tun Lin Aung
  • 339
  • 1
  • 4
  • 17
0

I don't know, what you mean manually reload properties file. Spring already provide to load properties file as below.

Configure your properties file in your spring configuration file. Eg. applicationContext.xml or spring-beans.xml

<util:properties id="MY_CONFIG" location="classpath:MY_CONFIG.properties"/>

In your sping bean, inject as below

@Resource(name = "MY_CONFIG")
private Properties properties;

Your servlet invoke that spring bean.

Update

If you would like to load the file from Servlet or other classes directly

Load properties file in Servlet/JSP

Community
  • 1
  • 1
Zaw Than oo
  • 9,651
  • 13
  • 83
  • 131
  • "Manually reload properties" mean that if you add new key=value pair in properties file, no need to redeploy this web application to update your changes in properties file. Instead of this, write new servlet file, inject message source and manually run this servlet to reload properties file that will update my changes without redeploying web application. – Tun Lin Aung Apr 05 '16 at 04:42
  • http://stackoverflow.com/questions/14117117/dynamically-loading-properties-file-using-spring – Zaw Than oo Apr 05 '16 at 06:27
  • I tried all, demo. But not working :). For cacheSeconds configuration, it autoreload every minutes we defined. It reduce production performance. That's why I am finding manual reload after I have changed my properties file. – Tun Lin Aung Apr 05 '16 at 10:29
  • You want to get up to date data. But, you don't want to reload every min/sec because of performance. Even if you use `FileChangeListener` of some API, this API have to check every min or sec itself. – Zaw Than oo Apr 05 '16 at 10:41