Java中如何获取spring中配置的properties文件内容
2017-06-06 16:20
1051 查看
有2种方式:
一。
1、通过spring配置properties文件
[java]
<bean id="propertyConfigurer"
class="com.tjsoft.base.util.CustomizedPropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>/WEB-INF/config/jdbc.properties</value>
<value>/WEB-INF/config/mail.properties</value>
<value>/WEB-INF/config/system.properties</value>
</list>
</property>
</bean>
其中class为自己定义的类
2、自定义类CustomizedPropertyPlaceholderConfigurer
[java]
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
/**
* 自定义PropertyPlaceholderConfigurer返回properties内容
*
* @author LHY 2012-02-24
*
*/
public class CustomizedPropertyPlaceholderConfigurer extends
PropertyPlaceholderConfigurer {
private static Map<String, Object> ctxPropertiesMap;
@Override
protected void processProperties(
ConfigurableListableBeanFactory beanFactoryToProcess,
Properties props) throws BeansException {
super.processProperties(beanFactoryToProcess, props);
ctxPropertiesMap = new HashMap<String, Object>();
for (Object key : props.keySet()) {
String keyStr = key.toString();
String value = props.getProperty(keyStr);
ctxPropertiesMap.put(keyStr, value);
}
}
public static Object getContextProperty(String name) {
return ctxPropertiesMap.get(name);
}
}
这样就可以通过CustomizedPropertyPlaceholderConfigurer类来获取properties属性文件中的内容了
3、如何获取属性文件的内容
String host = (String) CustomizedPropertyPlaceholderConfigurer.getContextProperty("mail.smtp.host");
一。
1、通过spring配置properties文件
[java]
<bean id="propertyConfigurer"
class="com.tjsoft.base.util.CustomizedPropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>/WEB-INF/config/jdbc.properties</value>
<value>/WEB-INF/config/mail.properties</value>
<value>/WEB-INF/config/system.properties</value>
</list>
</property>
</bean>
其中class为自己定义的类
2、自定义类CustomizedPropertyPlaceholderConfigurer
[java]
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
/**
* 自定义PropertyPlaceholderConfigurer返回properties内容
*
* @author LHY 2012-02-24
*
*/
public class CustomizedPropertyPlaceholderConfigurer extends
PropertyPlaceholderConfigurer {
private static Map<String, Object> ctxPropertiesMap;
@Override
protected void processProperties(
ConfigurableListableBeanFactory beanFactoryToProcess,
Properties props) throws BeansException {
super.processProperties(beanFactoryToProcess, props);
ctxPropertiesMap = new HashMap<String, Object>();
for (Object key : props.keySet()) {
String keyStr = key.toString();
String value = props.getProperty(keyStr);
ctxPropertiesMap.put(keyStr, value);
}
}
public static Object getContextProperty(String name) {
return ctxPropertiesMap.get(name);
}
}
这样就可以通过CustomizedPropertyPlaceholderConfigurer类来获取properties属性文件中的内容了
3、如何获取属性文件的内容
String host = (String) CustomizedPropertyPlaceholderConfigurer.getContextProperty("mail.smtp.host");
相关文章推荐
- Java中如何获取spring中配置的properties属性文件内容
- Java中如何获取spring中配置的properties属性文件内容
- Java中如何获取spring中配置的properties属性文件内容
- Java中如何获取spring中配置的properties属性文件内容
- SpringBoot学习:获取yml和properties配置文件的内容
- java 获取properties配置文件内容
- Java中获取包含变量的配置文件config.properties内容
- Java 获取*.properties配置文件中的内容 ,常见的两种方法
- SpringBoot学习:获取yml和properties配置文件的内容
- SpringBoot学习:获取yml和properties配置文件的内容(转)
- java 如何从配置文件(.properties)中读取内容
- Java代码中获取配置文件(config.properties)中内容的两种方法
- SpringBoot获取yml和properties配置文件的内容
- SpringBoot学习:获取yml和properties配置文件的内容(转)
- 如何在java类中读取Properties配置文件
- java工程如何读取配置文件并获取配置文件中的属性值
- java获取当前路径及加载配置文件(报错:java.lang.NullPointerException java.util.Properties$LineReader.readLine(Prop)
- 关于java读取和写入properties配置文件的内容
- Java中如何按顺序输出配置文件中的内容
- java.util.Properties读取中文内容的配置文件,发生中文乱码的现象有解决方案