基于Spring 资源接口读取自定义*.properties,实现功能拓展

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;
	}
	
}

相关功能补充调用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值