Tun Lin Aung
Tun Lin Aung

Reputation: 349

How to manually reload properties file from Spring messageSource

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.

Upvotes: 2

Views: 2922

Answers (2)

Tun Lin Aung
Tun Lin Aung

Reputation: 349

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.

Upvotes: 1

Zaw Than oo
Zaw Than oo

Reputation: 9935

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

Upvotes: 0

Related Questions