一、@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);
}
}