前言
在上一篇文章《Spring基本使用(二)——依赖注入》中,还有一个点没讲到,就是关于properties文件。现在常用的springboot中,经常会把一些配置属性写在application.properties中。写在application.properties文件中的属性,后面会注入到容器对象的属性中。本文准备先展示一下引入properties的几种方法。然后在稍微看看如何注入到对象的属性中。
引入properties文件
引入properties有四种方式,前三种是xml方式,最后一种是注解方式。假设我们在resource目录下建立一个application.properties文件作为配置文件:
test.list=1,3,4,5,6,1,2,3
name=jack
第一种方式
在xml文件中增加一句:
<context:property-placeholder location="application.properties" />
如果有多个properties文件,可以用逗号进行分割。注意多个引入多个properties文件时,配置的变量不能有所重复,否则会报错。
第二种方式
在xml中配置
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="locations" value="application.properties"/>
</bean>
注意,如果存在多个properties的话,不能配置多个PropertySourcesPlaceholderConfigurer的bean,否则后面的会无效。应该修改locations的属性设置,例如:
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="locations">
<list>
<value>application.properties</value>
<value>application2.properties</value>
</list>
</property>
</bean>
第三种方式
在xml中配置
<bean id="props2"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<array>
<value>classpath:application.properties</value>
</array>
</property>
</bean>
第四种方式
这种是注解方式,可以在java配置方式的容器,即添加了注解@Configuration的类上,添加@PropertySource注解,例如:
@Configuration
@ComponentScan(basePackages="com.sadoshi.springjavaconf")
@PropertySource("classpath:application.properties")
public class MyConfig {
@Bean
public Student myStudent() {
Student student = new Student();
student.setName("mike");
return student;
}
}
属性注入的方式
配置properties文件的属性之后,重点便是如何注入到对象中。通常有两种方式,一种是通过xml,另一种是@Value注解方式。
我们首先创建一个实体类,假设我们需要注入的对象是PropertiesEntity:
public class PropertiesEntity {
private String name;
private List<String> list;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
}
xml方式
假设我们按照本文前面的方式配置好了application.properties,然后在xml中配置属性注入:
<bean id="propertiesEntity" class="com.sadoshi.springannotation.entity.PropertiesEntity">
<property name="name">
<value>${name}</value>
</property>
<property name="list">
<value>${list}</value>
</property>
</bean>
@Value注解方式
通过@Value注解也是一种方式,直接注解到类上:
public class PropertiesEntity {
@Value("${name}")
private String name;
@Value("#{'${list}'.split(',')}")
private List<String> list;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
}
关于@Value内使用的表达式,叫SpEL表达式。这里简单说一下:
使用#{...}里面包含的,代表执行SpEL表达式取值。例如#{'hello world'}取字符串"hello world"赋值给属性,例如@Value("#{'1,2,3,4,5'.split(',')}"),会执行split分割字符串。另外#{...}也用于取对象的某个属性值出来,例如#{datasource.url}。
使用${...}一般是取properties中设置的属性。
通常#{...}和${...}还会混合使用,例如上面代码@Value("#{'${list}'.split(',')}")。意思是取properties文件中的list属性,然后通过split进行字符串分割,注入到对象属性中。
小结
本文列出了多种方法配置properties文件,以及如果进行属性注入。了解这些方式,不单有助于自己编码使用,更重要的是容易看懂和理解spring和springboot相关的程序配置。