一般properties文件,用于在代码块中读取,并給变量赋值。但是,Spring可以Bean XML方式定义(注册Bean)中,可以通过${属性名}使用properties文件配置的值。或者在代码中使用@Value注解读取properties的属性值。
所以用途有两种:
1. 用途
1.1. Spinrg的Bean XML定义中,可以通过${属性名}使用properties文件配置的值
首先,必须先加载properties配置文件,方式有两种,如下
方式一:
- <!--
- 用途1:Spring的xml配置文件中,可以通过${属性名}使用properties文件配置的值
- 用途2:可以使用@Value("${属性名}")注解读取properties文件配置的值,再给字段赋值
- 方法1:注解在字段上,给字段赋值
- 方法2:注解在字段的setter方法中赋值
- -->
- <context:property-placeholder location="classpath:jdbc.properties"/>
方式二:
- <!--
- 用途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>
可以清楚的看到,方式一,非常地简洁,但是如果要使用多个properties就可能实现不了,其实可以通过通配符实现,会有点麻烦。
接着,就可以在Bean的定义中,使用properties中的属性值,如下
- <bean id="dataSource"
- class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- <property name="driverClassName">
- <value>${jdbc.driver}</value>
- </property>
- <property name="url" >
- <value>${jdbc.url}</value>
- </property>
- <property name="username" >
- <value>${jdbc.user}</value>
- </property>
- <property name="password">
- <value>${jdbc.pwd}</value>
- </property>
- </bean>
1.2. 使用@Value注解读取properties文件配置的值,再给字段赋值
@Value注解是Spring 3.0 之后引入的新特性
@Value的值有三种类型,#{} 、${} 和 #{'${}'} ,其实是#{}和${}这两种类型,#{'${}'} 这种是前两种的嵌套使用,下面分别介绍
1) #{expression?:default value}
#{} 花括号里面的是SpEL表达式(即Spring Expression Language),?: 前面的是表达式,?: 后面的是默认值,这种方式非常地灵活,可以直接取bean对象的字段值!SpEL表达式的介绍,请看官方参考资料http://docs.spring.io/spring/docs/current/spring-framework-reference/html/expressions.html
但是,这种方式下,有个缺陷,那就是 properties配置文件中的属性名称不能带点,否则取不到值,会报错
如 file.uploadpath = E:\\360Downloads\\temp , 读取该属性值,就会报错,如下
- @Value("#{prop.file.uploadpath}")
- private String uploadPath;
- Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'file' cannot be found on object of type 'java.util.Properties' - maybe not public?
要使用这种类型的Value值,实现方式有两种,如下
方式一:
- <!--
- 用途:可以使用@Value("#{prop.属性名}")注解读取properties文件配置的值,再给字段赋值
- 方法1:注解在字段上,给字段赋值
- 方法2:注解在字段的setter方法中赋值
- 注意:@Value("#{prop.属性名}") 中的 prop 是 注册的PropertiesFactoryBean的 Bean ID
- -->
- <bean id="prop" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
- <property name="locations">
- <array>
- <value>classpath:fileupload.properties</value>
- </array>
- </property>
- </bean>
方式二:
- <!--
- 用途:可以使用@Value("#{prop.属性名}")注解读取properties文件配置的值,再给字段赋值
- 方法1:注解在字段上,给字段赋值
- 方法2:注解在字段的setter方法中赋值
- 注意:@Value("#{prop.属性名}") 中的 prop 是 注册的PropertiesFactoryBean的 Bean ID
- -->
- <util:properties id="prop" location="classpath:fileupload.properties"/>
可以清楚的看到,方式二,非常地简洁,但是如果要使用多个properties就可能实现不了,其实可以通过通配符实现,会有点麻烦。
接下来,看demo
fileupload.properties文件:
- name=zengyanhui
- age=12
Test.java:
- package edu.mvcdemo.service;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.context.annotation.Scope;
- import org.springframework.stereotype.Component;
- /**
- * @编写人: yh.zeng
- * @编写时间:2017-7-26 下午11:04:10
- * @文件描述: todo
- */
- @Component("test")
- @Scope("singleton")
- public class Test {
- @Value("#{prop.name}")
- private String name;
- @Value("#{prop.age}")
- private String age;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getAge() {
- return age;
- }
- public void setAge(String age) {
- this.age = age;
- }
- }
SpringBeanUtilsTest.java:
- package edu.mvcdemo.utils;
- import edu.mvcdemo.service.Test;
- import junit.framework.TestCase;
- /**
- * @编写人: yh.zeng
- * @编写时间:2017-7-26 下午11:09:38
- * @文件描述: todo
- */
- public class SpringBeanUtilsTest extends TestCase{
- public void test1(){
- SpringBeanUtils.setFilePath("src/springCfg/applicationContext-base.xml");
- Test test = (Test) SpringBeanUtils.getBean("test");
- System.out.println("name="+test.getName());
- System.out.println("age="+test.getAge());
- }
- }
程序运行结果:
- [INFO][2017-07-27 23:50:59][AbstractApplicationContext:583] - Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@b8d805: startup date [Thu Jul 27 23:50:59 CST 2017]; root of context hierarchy
- [INFO][2017-07-27 23:50:59][XmlBeanDefinitionReader:317] - Loading XML bean definitions from file [D:\EclipseWorkspace\MavenSpringMvcDemo\src\springCfg\applicationContext-base.xml]
- name=zengyanhui
- age=12
2)${property:default value}
${}这种值,只用来读取properties配置文件中的属性值, : 前面的是属性名称,: 后面的是默认值。这种类型的值,却可以读取带点的属性值,如 file.uploadpath = E:\\360Downloads\\temp,可以使用@Value("${file.uploadpath}")读取
要使用这种方式的Value,有两种实现方式,如下
方式一:
- <!--
- 用途1:Spring的xml配置文件中,可以通过${属性名}使用properties文件配置的值
- 用途2:可以使用@Value("${属性名}")注解读取properties文件配置的值,再给字段赋值
- 方法1:注解在字段上,给字段赋值
- 方法2:注解在字段的setter方法中赋值
- -->
- <bean id="propertyConfigurer"
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="location">
- <value>classpath:fileupload.properties</value>
- </property>
- </bean>
方式二:
- <!--
- 用途1:Spring的xml配置文件中,可以通过${属性名}使用properties文件配置的值
- 用途2:可以使用@Value("${属性名}")注解读取properties文件配置的值,再给字段赋值
- 方法1:注解在字段上,给字段赋值
- 方法2:注解在字段的setter方法中赋值
- -->
- <context:property-placeholder location="classpath:fileupload.properties"/>
可以清楚的看到,方式二,非常地简洁,但是如果要使用多个properties就可能实现不了,其实可以通过通配符实现,会有点麻烦。
下面看demo:
- package edu.mvcdemo.service;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.context.annotation.Scope;
- import org.springframework.stereotype.Component;
- /**
- * @编写人: yh.zeng
- * @编写时间:2017-7-26 下午11:04:10
- * @文件描述: todo
- */
- @Component("test")
- @Scope("singleton")
- public class Test {
- @Value("${name}")
- private String name;
- @Value("${age}")
- private String age;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getAge() {
- return age;
- }
- public void setAge(String age) {
- this.age = age;
- }
- }
程序运行结果:
- [INFO][2017-07-27 23:50:59][AbstractApplicationContext:583] - Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@b8d805: startup date [Thu Jul 27 23:50:59 CST 2017]; root of context hierarchy
- [INFO][2017-07-27 23:50:59][XmlBeanDefinitionReader:317] - Loading XML bean definitions from file [D:\EclipseWorkspace\MavenSpringMvcDemo\src\springCfg\applicationContext-base.xml]
- name=zengyanhui
- age=12
(3)#{'${}'}
这种类型的Value值,是#{}里面嵌套${}使用,所以必须按照上述的(1)(2)两种类型的实现方式,配置properties文件,才可以使用这种方式的值
- @Value("#{'${age}'}")
- private String age;
参考资料:
http://www.mkyong.com/spring3/spring-value-default-value/