一、@PropertySource
@PropertySource:加载指定的配置文件,只使用于.properties格式的文件;
@ConfigurationProperties默认是从全局配置文件中获取值的,如果把所有的配置都写在全局配置文件中,就会显得主配置文件内容特别多。所以我们把一些和Spring Boot无关的配置单独放在一个配置文件下,这个时候就需要@PropertySource;
@PropertySource需要与@ConfigurationProperties或则@Value配合使用;
主配置文件application.yml或则application.properties优先级高于@PropertySource指定的配置文件;
- 将主配置文件application中person属性注释掉,如果不注释掉,主配置文件中的内容会覆盖掉指定配置文件中的内容;
- resource文件夹下新建指定配置文件person.properties
person.last-name=王五
person.age=25
person.boss=true
person.birth=201901/01/05
person.maps.k1=v51
person.maps.k2=v52
person.maps.k3=v53
person.lists=aa,bb,cc
person.dog.name=五花狗
person.dog.age=5
- Person类上添加@PropertySource
package cn.springboottest.springboot.bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.Email;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* 将配置文件中配置的每一个属性值,映射到这个组件中
* @ConfigurationProperties: 告诉Spring Boot将本类中的所有属性和配置文件中相关的配置进行绑定
* prefix ="person":配置文件中哪个下面的所有属性进行一一映射
*
* 只有这个组件是容器中的组件,才能使用容器提供的@ConfigurationProperties功能
*
* @ConfigurationProperties(prefix = "person")默认从全局配置文件中获取值;
*
*
*/
@PropertySource(value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {
/*
<bean class="cn.springboottest.springboot.bean.Person" >
<property key="lastName" value="字面量/${key}从环境变量,配置文件中获取值/#{SpEL}"></property>
</bean>
*/
//@Value("${person.last-name}")
// @Email
private String lastName;
//@Value("#{11*5}")
private Integer age;
//@Value("true")
private Boolean boss;
private Date birth;
private Map<String, Object> maps;
private List<Object> lists;
private Dog dog;
@Override
public String toString() {
return "Person{" +
"lastName='" + lastName + '\'' +
", age=" + age +
", boss=" + boss +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Boolean getBoss() {
return boss;
}
public void setBoss(Boolean boss) {
this.boss = boss;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
}
- 测试
package cn.springboottest.springboot;
import cn.springboottest.springboot.bean.Person;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Spring Boot单元测试
* 可以在测试期间很方便的类似编码一样进行自动注入等容器的功能
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot02Config01ApplicationTests {
@Autowired
Person person;
@Test
public void contextLoads() {
System.out.println(person);
}
}
测试结果
Person{lastName='王五', age=25, boss=true, birth=Sat Jan 05 00:00:00 CST 201901, maps={k1=v51, k3=v53, k2=v52}, lists=[aa, bb, cc], dog=Dog{name='五花狗', age=5}}
二、@ImprotResource
@ImprotResource:导入Spring的配置文件,让配置文件里面的内容生效 ;
Spring Boot里面没有Spring的配置文件,新建一个Spring的配置文件(beans.xml),Spring Boot也不能将Spring配置文件中的内容加载进来,想要让Spring配置文件中的内容生效,需要在主程序类上添加@ImportResource(locations = {“classpath:beans.xml”})
- 创建HelloService
package cn.springboottest.springboot.service;
public class HelloService {
}
- 创建Spring配置文件beans.xml
右键resources文件夹,New > XML Configuration File > Spring Config
添加bean对象
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="helloService" class="cn.springboottest.springboot.service.HelloService"></bean>
</beans>
- 导入Spring配置文件beans.xml让其生效
在主程序类上添加@ImportResource
package cn.springboottest.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@ImportResource(locations = {"classpath:beans.xml"})/*导入spring的配置文件让其生效*/
@SpringBootApplication
public class SpringBoot02Config01Application {
public static void main(String[] args) {
SpringApplication.run(SpringBoot02Config01Application.class, args);
}
}
- 测试
package cn.springboottest.springboot;
import cn.springboottest.springboot.bean.Person;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Spring Boot单元测试
* 可以在测试期间很方便的类似编码一样进行自动注入等容器的功能
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot02Config01ApplicationTests {
@Autowired
ApplicationContext ac;
@Autowired
Person person;
//测试@ImportSource
@Test
public void helloServiceTest(){
System.out.println(ac.containsBean("helloService"));
}
@Test
public void contextLoads() {
System.out.println(person);
}
}
使用配置类的全注解方式再演示一次
Spring Boot给容器中添加组件的方式推荐使用全注解的方式;如果使用这种方式就不用在主程序类上添加@ImportResource了。
- 创建配置类
package cn.springboottest.springboot.config;
import cn.springboottest.springboot.service.HelloService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Configuration:表示当前类是一个配置类,替代之前的spring配置文件
*
* 在配置文件中用<bean></bean>添加组件,这里用@Bean替代
*/
@Configuration
public class MyAppConfig {
//将方法的返回值添加到容器中,容器中这个组件默认的id就是方法名
@Bean
public HelloService helloService(){
System.out.println("配置类@Bean给容器中添加组件了……");
return new HelloService();
}
}
- 将主程序类上@ImportResource注释掉,使用上面的测试类再次测试
package cn.springboottest.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
//@ImportResource(locations = {"classpath:beans.xml"})/*导入spring的配置文件让其生效*/
@SpringBootApplication
public class SpringBoot02Config01Application {
public static void main(String[] args) {
SpringApplication.run(SpringBoot02Config01Application.class, args);
}
}