上一节:
SpringBoot开发笔记-(3) 属性文件读取1: @ConfigurationProperties读取yml配置文件


上一节用到了@ConfigurationProperties会加载配置文件application.yml或application.properties配置中的属性; 然而如果咱们须要本人独自定义的属性文件的时候, 就要用到 本人指定属性源了: @PropertySource

4.1 应用示例

maven:

<?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:

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") // 2public 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:

person.lastName=李四person.age=33person.male=trueperson.birth=1985/03/03person.maps.k1=v1person.maps.k2=20person.maps.k3=trueperson.lists=lisi,wangwuperson.dog.name=小黄person.dog.age=3

测试:

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)@SpringBootTestpublic class SpringbootTest {    @Autowired    private Person person;    @Test    public void printPerson(){        System.out.println(new Gson().toJson(person));    }}

4.2 PropertySource应用留神

  1. 能够指定一个配置文件, 也能够指定多个:

    @PropertySource("classpath:/person.properties"), 也能够指定多个:

    @PropertySource(value = {"classpath:/person.properties"}) // 1 指定自定义的 properties 配置文件

  2. ConfigurationProperties也扔须要配合应用;

    @PropertySource(value = {"classpath:/person.properties"}) // 1 指定自定义的 properties 配置文件@ConfigurationProperties(prefix = "person") // 2
  3. //@PropertySource("classpath:/person.yml") // 谬误! 3 yml文件,默认是不反对的!!