概述
在Spring中处理外部值最简常用的方法就是外部创建name.properties
文件,并在其中声明变量值,供Java进行读取。比如数据源信息配置,Java固定属性位置等。读取的方式一般由三种:
- 通过Spring的Environment检索属性
- 通过占位符读取属性(Java和xml两种方式)
- 通过表达式装配(xml)
1. 通过Spring Envrionment检索属性
简单示例:
- 创建property.properties文件,并编写属性值:
name=this is properties
title=properties
num=100
- 创建目标Java类
PropertyImpl.java
,包含property.properties中属性对应的成员变量:
private String title;
private String name;
public PropertyImpl(String ti, String na) {
// TODO Auto-generated constructor stub
title = ti;
name = na;
}
@Override
public void show() {
// TODO Auto-generated method stub
System.out.println("title:" + title + "\n" + "name:" + name);
}
- 创建Java配置类,显示声明Bean,并声明包含properties中声明属性的Spring Bean工厂@Configuration等价于xml中的beans,@ComponentScan自动扫描该包和子包添加注释的Java类注入到Bean工厂,@PropertySource Spring解析指定的properties文件属性:
@Bean
public Proterty property(){
String[] activSsize = env.getActiveProfiles();
for(int i = 0; i < activSsize.length; i++){
System.out.println("i=" + i + " || profile:" + activSsize[i]);
}
String[] defaultSize = env.getActiveProfiles();
for(int i = 0; i < defaultSize.length; i++){
System.out.println("i=" + i + " || profile:" + defaultSize[i]);
}
Integer num = env.getProperty("num", Integer.class, 30);
System.out.println("num=" + num);
num = env.getProperty("num1", Integer.class, 30);
System.out.println("num1=" + num);
return new PropertyImpl(env.getProperty("title"), env.getProperty("name"));
}
}
- 通过Java配置Spring形式读取properties文件属性值基本结束,下面通过junit进行测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=JavaConfiguration.class)
public class Main {
@Autowired
private Proterty proterty;
@Test
public void test(){
proterty.show();
}
}
- 对于environment包含的其它方法介绍:
- T getProterty(String key, Class type, T defaultValue)如果不存在,设定默认值
- T getProterty(String key, Class type)将key属性值转化为固定类对象
- String getProterty(String key, String defaultValue)读取属性值不存在,设置为默认值
- getRequiredProperty(String key)如果对应属性不存在,跑出异常
- constainsProperty(String key)检查属性是否存在
- getPropertyAsClass(String key, name.class)将属性值解析为类
- getActiveProfiles()
- getdefaultProfiles()
- acceptsProfiles()
2. 通过占位符获取属性值
- 首先,创建properties属性文件
property.proterties
,property2.properties
(加载多propeerties文件):
name=this is properties
title=properties
num=100
url=localhost:8080
drive=c3p0
usename=chen
password=abcd1234
- 创建对应读取属性值的Java类
XmlProperties.java
public class XmlProperties {
private String url;
private String drive;
private String usename;
private String password;
private String title;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDrive() {
return drive;
}
public void setDrive(String drive) {
this.drive = drive;
}
public String getUsename() {
return usename;
}
public void setUsename(String usename) {
this.usename = usename;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public void show(){
System.out.println("url:" + url + "\nderive:" + drive + "\nusename:" + usename +
"\npassword:" + password + "\ntitle:" + title);
}
}
- xml配置文件:创建
PropertyPlaceholderConfigurer Bean
,并加载properties文件。
<bean id="propertyConfligurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/com/tidas/spring/proterties/property2.properties</value>
<value>/com/tidas/spring/proterties/property.proterties</value>
</list>
</property>
</bean>
- 通过占位符读取属性值,这种也是在web开发中配置数据源最常用的方法
<bean id="xmlProperties" class="com.tidas.spring.property.XmlProperties">
<property name="url" value="${url}"/>
<property name="drive" value="${drive}"/>
<property name="usename" value="${usename}"/>
<property name="password" value="${password}"/>
<property name="title" value="${title}"/>
</bean>
- 当然,如果是通过javaconfigration进行配置Bean工厂的,也可以使用如下方法读取属性:
@Component
public class PropertyImpl implements Proterty {
private String title;
private String name;
public PropertyImpl(@Value("${title}") String ti, @Value("${name}") String na) {
// TODO Auto-generated constructor stub
title = ti;
name = na;
}
...
这种创建方式在自动扫描的时候注入场景中使用最为合适