springmvc项目applicationContext.xml读取磁盘properties属性文件

项目框架:springmvc
开发工具:eclipse
jdk版本:1.7
目的:项目中的applicationContext.xml,bean对象引用磁盘中的properties属性文件,java类中可读取到properties中key对应value,xml可读取到properties中key对应value
示例如下:

applicationContext.xml中读取磁盘中的properties属性文件方法

<bean id="globalProperties"	class="com.test.utils.Config">
		<property name="order" value="1" />
		<property name="ignoreUnresolvablePlaceholders" value="true" />
		<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
		<property name="ignoreResourceNotFound" value="true" />
		<property name="locations" >
			<list>
				<!--引用classpath下的属性文件-->
				<value>classpath:test.properties</value>
				<!--引用磁盘中的属性文件,*表示读取此目录下以.properties的属性文件-->
				<value>file:/D:/test/*.properties</value>
				<!--linux引用磁盘中的属性文件-->
				<!--<value>file:/home/dev/config/*.properties</value>-->
			</list>	
		</property>
	</bean>

	<context:property-placeholder ignore-unresolvable="true" location="file:/D:/test/datasource.properties"/>

	<bean id="mysql_test" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> 
		    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
			<property name="url" value="${jdbc.test.url}"/>
			<property name="username" value="${jdbc.test.username}" />
			<property name="password" value="${jdbc.test.password}" />
			<!-- mysql其它配置略 -->
	</bean>
	

在我的D盘下有test目录,目录下放了几个属性文件
在这里插入图片描述
项目中读取properties中key对应value的方法

package com.test.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import data.test.utils.Config;


@Controller
@RequestMapping("/test")
public class TestController {

	//方法一: 使用注解方式,读取properties中key对应的value
	@Value("${jdbc.test.username}")
	private String username;
	
	@RequestMapping("/test")
	public @ResponseBody String test(HttpServletRequest request, 
			HttpServletResponse response){
		try {
			System.out.println("读取配置文件");
			//方法一: 使用springframework的PropertyPlaceholderConfigurer,读取properties中key对应的value
			String str = Config.getValue("aaaaaa");
			String str2 = Config.getValue("bbbbbb");
			String str3 = Config.getValue("cccccc");
			String str4 = Config.getValue("dddddd");
			System.out.println("项目外部配置文件读取到的变量1="+str);
			System.out.println("项目外部配置文件读取到的变量2="+str2);
			System.out.println("项目外部配置文件读取到的变量3="+str3);
			System.out.println("项目外部配置文件读取到的变量4="+str4);
			System.out.println("项目外部配置文件读取到的username="+username);
			//输出结果:
			//项目外部配置文件读取到的变量1=a111111111111111
			//项目外部配置文件读取到的变量2=b1111111111111111111111
			//项目外部配置文件读取到的变量3=c111111111111111111111111111111111
			//项目外部配置文件读取到的变量4=
			//项目外部配置文件读取到的username=zhangsan
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		
		return "test";
	}
}

Config.java

package com.test.utils;

import java.io.UnsupportedEncodingException;
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;

public class Config extends PropertyPlaceholderConfigurer {
	private static Map<String, String> globalCode = null;

	@Override
	protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
			throws BeansException {
		super.processProperties(beanFactoryToProcess, props);
		valuemap = new HashMap<String, String>();
		for (Object key : props.keySet()) {  
            String keyStr = key.toString();  
            String value = null;
			try {
				value = new String(props.getProperty(keyStr).getBytes("iso-8859-1"), "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}  
            valuemap.put(keyStr, value);  
        }
	}
	public static String getValue(String key) {  
        return valuemap.get(key)==null?"":valuemap.get(key);
    } 

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值