基于Spring 资源接口读取自定义*.properties,实现功能拓展
你猜
阅读:669
2021-03-31 17:01:39
评论:0
spring 读取*.properties 文件的三种方式:
第一种:注解标签:
<!--
用途1:Spring的xml配置文件中,可以通过${属性名}使用properties文件配置的值
用途2:可以使用@Value("${属性名}")注解读取properties文件配置的值,再给字段赋值
方法1:注解在字段上,给字段赋值
方法2:注解在字段的setter方法中赋值
-->
<context:property-placeholder location="classpath:jdbc.properties"/>
第二种:spring 容器注入Bean :
<!--
用途1:Spring的xml配置文件中,可以通过${属性名}使用properties文件配置的值
用途2:可以使用@Value("${属性名}")注解读取properties文件配置的值,再给字段赋值
方法1:注解在字段上,给字段赋值
方法2:注解在字段的setter方法中赋值
-->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/configs/sqlServer.properties</value>
</property>
</bean>
第三种方式:实现PropertyPlaceholderConfigurer类,覆写processProperties() 方法,新增自己的功能业务代码:
我的业务需求,读取指定*.propertise 文件,将读取的Properties的属性 值,转换为Map进行相关输出。
核心功能代码:
import java.util.Enumeration;
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;
/**
* properties配置文件读取类
*
* @author zzg
*
*/
public class PropertyConfigurer extends PropertyPlaceholderConfigurer {
private Properties props; // 存取properties配置文件key-value结果
@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
throws BeansException {
super.processProperties(beanFactoryToProcess, props);
this.props = props;
}
/**
* 获取指定key 值
* @param key
* @return
*/
public String getProperty(String key) {
return this.props.getProperty(key);
}
/**
* 获取指定key 值,不存在使用默认值
* @param key
* @param defaultValue
* @return
*/
public String getProperty(String key, String defaultValue) {
return this.props.getProperty(key, defaultValue);
}
/**
* 设置指定的key 和value 值
* @param key
* @param value
* @return
*/
public Object setProperty(String key, String value) {
return this.props.setProperty(key, value);
}
/**
* 将properties 转换为Map结构
* @throws ClassNotFoundException
*/
public Map converMap() throws ClassNotFoundException{
Map<String, Class<?>> map = new HashMap<String,Class<?>>();
Enumeration en=this.props.propertyNames();
while(en.hasMoreElements()){
String key = (String) en.nextElement();
Class<?> obj = Class.forName((String)this.props.get(key));
map.put(key, obj);
}
return map;
}
}
通过自定义配置注入,注入自定义的*properties 类。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import com.digipower.ucas.util.spring.FilterPropertyConfigurer;
import com.digipower.ucas.util.spring.PropertyConfigurer;
/**
* 自定义*.properties 读取工具类
* @author zzg
*
*/
@Configuration
public class SpringConfig {
@Bean
public PropertyConfigurer getPropertyConfigurer(){
PropertyConfigurer configurer = new PropertyConfigurer();
configurer.setIgnoreResourceNotFound(true);
configurer.setIgnoreUnresolvablePlaceholders(true);
// 设置自定义资源路径地址
ClassPathResource location = new ClassPathResource("settings/convert.properties") ;
configurer.setLocation(location);
return configurer;
}
}
相关功能补充调用。
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。