上一節:SpringBoot開發筆記-(3) 屬性文件讀取1: @ConfigurationProperties讀取yml配置文件java
上一節用到了@ConfigurationProperties會加載配置文件application.yml或application.properties配置中的屬性; 可是若是咱們須要本身單獨定義的屬性文件的時候, 就要用到 本身指定屬性源
了: @PropertySource
web
maven:spring
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> <relativePath/> </parent> <groupId>com.niewj</groupId> <artifactId>springboot-01-helloworld</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-01-helloworld</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.6</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- @RunWith都是來自它 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.6</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Person.java:apache
package com.niewj.springboot.model; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; import java.util.Date; import java.util.List; import java.util.Map; /** * 注意: * 1. 使用 @PropertySource時, @ConfigurationProperties(prefix="person") 亦不能省略! * 2. 指定 yml文件是不支持的! * * Created by niewj on 2020/8/5 0:01 */ @Data @Component //@PropertySource("classpath:/person.yml") // 3 yml文件是不支持的!! @PropertySource("classpath:/person.properties") // 1 指定自定義的 properties 配置文件 @ConfigurationProperties(prefix = "person") // 2 public class Person { private String lastName; private Integer age; private Boolean male; private Date birth; private Map<String, Object> maps; private List<Object> lists; private Dog dog; }
person.properties:segmentfault
person.lastName=李四 person.age=33 person.male=true person.birth=1985/03/03 person.maps.k1=v1 person.maps.k2=20 person.maps.k3=true person.lists=lisi,wangwu person.dog.name=小黃 person.dog.age=3
測試:springboot
package com.niewj.springboot; import com.google.gson.Gson; import com.niewj.springboot.model.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.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class SpringbootTest { @Autowired private Person person; @Test public void printPerson(){ System.out.println(new Gson().toJson(person)); } }
能夠指定一個配置文件, 也能夠指定多個:app
@PropertySource("classpath:/person.properties"), 也能夠指定多個:@PropertySource(value = {"classpath:/person.properties"}) // 1 指定自定義的 properties 配置文件maven
ConfigurationProperties也扔須要配合使用;spring-boot
@PropertySource(value = {"classpath:/person.properties"}) // 1 指定自定義的 properties 配置文件 @ConfigurationProperties(prefix = "person") // 2
//@PropertySource("classpath:/person.yml")
// 錯誤! 3 yml文件,默認是不支持的!!