import java.util.HashSet;
import java.util.Properties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
/**这里不用<context:property-placeholder/>
* 重写了PropertyPlaceholderConfigurer 类,在spring配置文件加载
* @author lyq
*
*/
public class MyPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
@SuppressWarnings("deprecation")
@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess,
Properties props) throws BeansException {
super.processProperties(beanFactoryToProcess, props);
// resolvedProps = new HashMap<String, String>();
for (Object key : props.keySet()) {
String keyStr = key.toString();
ConfigContainer.props.put(keyStr, parseStringValue(props.getProperty(keyStr), props,new HashSet()));
}
}
}
<bean id="configBean"
class="com.gainthink.prop.MyPropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath*:*.properties</value>
</property>
</bean>
import java.util.HashMap;
import java.util.Map;
public class ConfigContainer {
static Map<String,String> props = new HashMap<String, String>();
public static String getProperty(String key){
return props.get(key);
}
}
最后执行程序
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
System.out.println(ConfigContainer.getProperty("interval.time"));
import java.util.Properties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
/**这里不用<context:property-placeholder/>
* 重写了PropertyPlaceholderConfigurer 类,在spring配置文件加载
* @author lyq
*
*/
public class MyPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
@SuppressWarnings("deprecation")
@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess,
Properties props) throws BeansException {
super.processProperties(beanFactoryToProcess, props);
// resolvedProps = new HashMap<String, String>();
for (Object key : props.keySet()) {
String keyStr = key.toString();
ConfigContainer.props.put(keyStr, parseStringValue(props.getProperty(keyStr), props,new HashSet()));
}
}
}
<bean id="configBean"
class="com.gainthink.prop.MyPropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath*:*.properties</value>
</property>
</bean>
import java.util.HashMap;
import java.util.Map;
public class ConfigContainer {
static Map<String,String> props = new HashMap<String, String>();
public static String getProperty(String key){
return props.get(key);
}
}
最后执行程序
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
System.out.println(ConfigContainer.getProperty("interval.time"));