在applicationContext.xml中定义一个bean,如下:
<bean id="placeholderResolver" class="com.source.util.PlaceholderResolverImpl">
<property name="location" value="project.properties" />
</bean>
PlaceholderResolverImpl类负责将属性配置文件(project.properties)中配置的系统变量解析成相应的值,详细见附件1.
例如:table.applicationParameters=APPL_PARM
hibernate的映射文件中的table就可以通过这个类来解析,
<class name="ApplParm" table="${table.applicationParameters}" batch-size="50" mutable="false">
以上的代码在加载时被解析为:
<class name="ApplParm" table="APPL_PARM" batch-size="50" mutable="false">
APPL_PARM是数据库对应的一个表名。
附件1:PlaceholderResolverImpl
package pl.com.source.wwmd.util;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.Resource;
import org.springframework.util.PlaceholderResolver;
import pl.com.source.wwmd.exception.WwmdUnmappedPropertyException;
/**
* Implementation of PlaceHolderResolver, allowing to read properties from files
* with ${property.name} syntax
*
*/
public class PlaceholderResolverImpl implements PlaceholderResolver {
/** Prefix for system property placeholders: "${" */
public static final String PLACEHOLDER_PREFIX = "${";
/** Suffix for system property placeholders: "}" */
public static final String PLACEHOLDER_SUFFIX = "}";
private static final Log log = LogFactory.getLog(PlaceholderResolverImpl.class);
private Properties properties = new Properties();
/**
* Sets location attribute - place where the config file should exist
* @param resource
* @throws FileNotFoundException
* @throws IOException
*/
public void setLocation(Resource resource) throws FileNotFoundException, IOException {
properties.load(ConfigDirectoryPathHolder.getInputStream(resource));
}
/**
* Resolve ${...} placeholders in the given text,
* replacing them with corresponding system property values.
* @param text the String to resolve
* @return the resolved String
* @see #PLACEHOLDER_PREFIX
* @see #PLACEHOLDER_SUFFIX
*/
public String resolvePlaceholders(String text) {
StringBuffer buf = new StringBuffer(text);
// The following code does not use JDK 1.4's StringBuffer.indexOf(String)
// method to retain JDK 1.3 compatibility. The slight loss in performance
// is not really relevant, as this code will typically just run on startup.
int startIndex = text.indexOf(PLACEHOLDER_PREFIX);
while (startIndex != -1) {
int endIndex = buf.toString().indexOf(PLACEHOLDER_SUFFIX, startIndex + PLACEHOLDER_PREFIX.length());
if (endIndex != -1) {
String placeholder = buf.substring(startIndex + PLACEHOLDER_PREFIX.length(), endIndex);
int nextIndex = endIndex + PLACEHOLDER_SUFFIX.length();
String propVal = properties.getProperty(placeholder);
if (propVal != null) {
log.debug("Resolved placeholder " + placeholder + " into " + propVal);
buf.replace(startIndex, endIndex + PLACEHOLDER_SUFFIX.length(), propVal);
nextIndex = startIndex + propVal.length();
} else {
log.warn("Could not resolve placeholder '" + placeholder + "' in [" + text + "]");
}
startIndex = buf.toString().indexOf(PLACEHOLDER_PREFIX, nextIndex);
}
else {
startIndex = -1;
}
}
log.debug(buf.toString());
return buf.toString();
}
/* (non-Javadoc)
* @see org.springframework.util.PlaceholderResolver#resolvePlaceholder(java.lang.String)
*/
public String resolvePlaceholder(String placeholder) {
String value = properties.getProperty(placeholder);
if(value == null) {
throw new WwmdUnmappedPropertyException("Could not resolve placeholder " + placeholder);
} else {
return value;
}
}
}