如何配置并获取多个properties文件源信息

本文介绍了在Spring框架中如何配置并获取多个properties文件的内容。提供了两种方法:一是继承PropertyPlaceholderConfigurer,启动时加载;二是创建监听器,实现ServletContextListener接口,通过流方式读取并加载属性。每种方法都有其适用场景,可以根据需求选择。
摘要由CSDN通过智能技术生成

在搭配框架的时候,大多数时候都是需要配置多个properties文件, 那么这个时候,如何配置并获取文件里的内容呢?

第一种方法:写一个类,继承PropertyPlaceholderConfigurer ,然后启动的时候就加载:代码如下


package com.cmos.ngmttcontrol.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Decrypt the password to the properties file
 * 
 */
public final class PropertyConfigurer extends
		org.springframework.beans.factory.config.PropertyPlaceholderConfigurer {
	 private static final Pattern PATTERN = Pattern
	            .compile("\\$\\{([^\\}]+)\\}");
	 
	private static Properties props=null;
	private static Map<String, String> map=new HashMap<String, String>();
	
	//服务启动时,该方法就会执行。
	protected void loadProperties(Properties props) throws IOException {
		super.loadProperties(props);
		PropertyConfigurer.setProps(props);
		
		props.size();
		if(props.size()>0){
			String key = null;
            String value = null;
            for(Entry<Object, Object> entry: props.entrySet()){
            	key = (String) entry.getKey();
                value = (String) entry.getValue();
                if(key != null){
                	value = getdynamicValue(key);
                	if(value != null
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值