Skip to content

Instantly share code, notes, and snippets.

@max747
Created July 2, 2011 13:52
Show Gist options
  • Save max747/1060320 to your computer and use it in GitHub Desktop.
Save max747/1060320 to your computer and use it in GitHub Desktop.
use property placeholder in SpringFramework MessageSource
xday=2011/07/02
package sandbox.message;
import java.util.Locale;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;
@Service
public class Message {
@Autowired
MessageSource messageSource;
public String showHello() {
return messageSource.getMessage("hello", null, Locale.getDefault());
}
}
hello=こんにちは。Xデーは ${xday} です
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<context:component-scan base-package="sandbox.message" />
<bean id="placeholderProperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath:sandbox/message/env.properties" />
<property name="fileEncoding" value="utf-8" />
</bean>
<bean id="messageSource"
class="sandbox.message.PlaceHolderConfigurableReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:sandbox/message/messages" />
<property name="defaultEncoding" value="utf-8" />
<property name="placeholderProperties" ref="placeholderProperties" />
</bean>
</beans>
package sandbox.message;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class MessageTest {
@Autowired
Message message;
@Test
public void testShowHello() {
System.out.println(message.showHello());
}
}
--- ReloadableResourceBundleMessageSource.java 2011-07-02 16:34:17.421875000 +0900
+++ PlaceholderConfigurableReloadableResourceBundleMessageSource.java 2011-07-02 18:02:31.734375000 +0900
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package org.springframework.context.support;
+package sandbox.message;
import java.io.IOException;
import java.io.InputStream;
@@ -27,13 +27,17 @@
import java.util.Map;
import java.util.Properties;
+import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.ResourceLoaderAware;
+import org.springframework.context.support.AbstractMessageSource;
+import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.Assert;
import org.springframework.util.DefaultPropertiesPersister;
import org.springframework.util.PropertiesPersister;
+import org.springframework.util.PropertyPlaceholderHelper;
import org.springframework.util.StringUtils;
/**
@@ -88,7 +92,7 @@
* @see ResourceBundleMessageSource
* @see java.util.ResourceBundle
*/
-public class ReloadableResourceBundleMessageSource extends AbstractMessageSource
+public class PlaceHolderConfigurableReloadableResourceBundleMessageSource extends AbstractMessageSource
implements ResourceLoaderAware {
private static final String PROPERTIES_SUFFIX = ".properties";
@@ -571,8 +575,8 @@
*/
public void clearCacheIncludingAncestors() {
clearCache();
- if (getParentMessageSource() instanceof ReloadableResourceBundleMessageSource) {
- ((ReloadableResourceBundleMessageSource) getParentMessageSource()).clearCacheIncludingAncestors();
+ if (getParentMessageSource() instanceof PlaceHolderConfigurableReloadableResourceBundleMessageSource) {
+ ((PlaceHolderConfigurableReloadableResourceBundleMessageSource) getParentMessageSource()).clearCacheIncludingAncestors();
}
}
@@ -583,6 +587,17 @@
}
+ private PropertyPlaceholderHelper placeholderHelper = new PropertyPlaceholderHelper(
+ PropertyPlaceholderConfigurer.DEFAULT_PLACEHOLDER_PREFIX,
+ PropertyPlaceholderConfigurer.DEFAULT_PLACEHOLDER_SUFFIX,
+ PropertyPlaceholderConfigurer.DEFAULT_VALUE_SEPARATOR, true);
+
+ private Properties placeholderProperties;
+
+ public void setPlaceholderProperties(Properties placeholderProperties) {
+ this.placeholderProperties = placeholderProperties;
+ }
+
/**
* PropertiesHolder for caching.
* Stores the last-modified timestamp of the source file for efficient
@@ -629,7 +644,8 @@
if (this.properties == null) {
return null;
}
- return this.properties.getProperty(code);
+ String value = this.properties.getProperty(code);
+ return placeholderHelper.replacePlaceholders(value, placeholderProperties);
}
public MessageFormat getMessageFormat(String code, Locale locale) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment