在Spring中,配置文件分为导入和装配两个步骤,而装配则分为通过Spring的Environment、解析属性占位符、Spring表达式语言SpEL三种方式。
一、Spring导入配置文件:
配置文件中导入非常简单,只需要
1、导入properties文件:
在src/main/resources下面新建一个File,命名为test.properties
2、在Config文件中,写入解析配置文件的注解:
@PropertySource("classpath:test.properties")
3、如果是多个配置文件的话,那么最好在.xml中加入扫描配置文件的注解:
<context:property-placeholder location="classpath*:**/*.properties"/>
那么Spring就会把classpath下面所有的.properties配置都导入到容器中。
二、通过Spring的Environment来装配属性:
Spring会自动解析配置文件,并将配置文件中得到的数据注入到Spring的Environment对象中,需要获取配置文件信息的类只需要注入Spring的Environment对象即可。
使用@Autowired即可自动注入一个Environment对象,此对象即有配置文件中的所有配置,即可以通过此对象来获取配置文件中的所有信息。
1.1、String getProperty(String key)
1.2、String getProperty(String key,String defaultValue)
1.3、T getProperty(String key, Class<T> type)
1.4、T getProperty(String key, Class<T> type,T defaultValue)
比如想获取配置文件中key为name的属性,可以这样:
env.getProperty("name")
再比如配置文件有connection.username = happyheng,那么可以这样获取:
env.getProperty("connection.username")
3与4即是获取非String类型的属性,比如获取Integer属性的参数:
int count = env.getProperty("connection.count", Integer.class, 30);
2、判断是否存在某配置:
boolean exists = env.containsProperty("disc.title");
举例:
需要解决的问题:
现在我需要将一个People的name和age都放在配置文件中,在程序运行时,动态的将name和age放置到People对象中:
解决方法:
1、新建一个test.properties,写入配置:
name=happyheng
age=18
2、在config文件中写入注解:
@Configuration
@ComponentScan(basePackages = "com.happyheng")
@PropertySource("classpath:test.properties")
public class MainConfig {
@Autowired
Environment env;
@Bean
public People people() {
People people = new People();
people.setName(env.getProperty("name"));
people.setAge(env.getProperty("age"));
return people;
}
}
3、其中People的实体类为:
public class People {
private String name;
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;
}
}
4、创建一个Test类进行测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MainConfig.class)
public class MainTest {
@Autowired
People people;
@Test
public void test() {
System.out.println("people的name为" + people.getName() + "age为" + people.getAge());
}
}
5、输出的结果为:
people的name为happyheng---age为18
三、使用解析属性占位符进行装配:
此为最简单的方法,当导入配置文件之后,我们可以直接通过
@Value("${xxx}")
来直接获取到配置文件中key对应的值:
比如,配置文件中有一个配置:
key=value
那么获取的方式为
@Value("${key}")
举例:
继续按照上述工程,现在需要直接通过配置文件中的数据去生成一个ValuePeople对象,写法为:
@Component
public class ValuePeople {
@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;
}
}
一、Spring导入配置文件:
配置文件中导入非常简单,只需要
1、导入properties文件:
在src/main/resources下面新建一个File,命名为test.properties
2、在Config文件中,写入解析配置文件的注解:
@PropertySource("classpath:test.properties")
3、如果是多个配置文件的话,那么最好在.xml中加入扫描配置文件的注解:
<context:property-placeholder location="classpath*:**/*.properties"/>
那么Spring就会把classpath下面所有的.properties配置都导入到容器中。
二、通过Spring的Environment来装配属性:
Spring会自动解析配置文件,并将配置文件中得到的数据注入到Spring的Environment对象中,需要获取配置文件信息的类只需要注入Spring的Environment对象即可。
使用@Autowired即可自动注入一个Environment对象,此对象即有配置文件中的所有配置,即可以通过此对象来获取配置文件中的所有信息。
Environment的使用:
1、获取配置信息的四种方法:1.1、String getProperty(String key)
1.2、String getProperty(String key,String defaultValue)
1.3、T getProperty(String key, Class<T> type)
1.4、T getProperty(String key, Class<T> type,T defaultValue)
比如想获取配置文件中key为name的属性,可以这样:
env.getProperty("name")
再比如配置文件有connection.username = happyheng,那么可以这样获取:
env.getProperty("connection.username")
3与4即是获取非String类型的属性,比如获取Integer属性的参数:
int count = env.getProperty("connection.count", Integer.class, 30);
2、判断是否存在某配置:
boolean exists = env.containsProperty("disc.title");
举例:
需要解决的问题:
现在我需要将一个People的name和age都放在配置文件中,在程序运行时,动态的将name和age放置到People对象中:
解决方法:
1、新建一个test.properties,写入配置:
name=happyheng
age=18
2、在config文件中写入注解:
@Configuration
@ComponentScan(basePackages = "com.happyheng")
@PropertySource("classpath:test.properties")
public class MainConfig {
@Autowired
Environment env;
@Bean
public People people() {
People people = new People();
people.setName(env.getProperty("name"));
people.setAge(env.getProperty("age"));
return people;
}
}
3、其中People的实体类为:
public class People {
private String name;
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;
}
}
4、创建一个Test类进行测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MainConfig.class)
public class MainTest {
@Autowired
People people;
@Test
public void test() {
System.out.println("people的name为" + people.getName() + "age为" + people.getAge());
}
}
5、输出的结果为:
people的name为happyheng---age为18
三、使用解析属性占位符进行装配:
此为最简单的方法,当导入配置文件之后,我们可以直接通过
@Value("${xxx}")
来直接获取到配置文件中key对应的值:
比如,配置文件中有一个配置:
key=value
那么获取的方式为
@Value("${key}")
举例:
继续按照上述工程,现在需要直接通过配置文件中的数据去生成一个ValuePeople对象,写法为:
@Component
public class ValuePeople {
@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;
}
}
则Spring在生成ValuePeople对象时会自动将name和age注入进去。
4、SpEL语言的使用请查看下面的链接:
5、此项目在github上的链接为:
https://github.com/happyheng/property-test