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);
}
}
还没有评论,来说两句吧...