SpringBoot开发笔记-(4)-属性文件读取2-@PropertySource

  1. SpringBoot开发笔记-(4) 属性文件读取2- @PropertySource
  2. 4.1 PropertySource使用示例
  3. 4.2 PropertySource使用注意

SpringBoot开发笔记-(4) 属性文件读取2- @PropertySource

上一节用到了@ConfigurationProperties会加载配置文件application.yml或application.properties配置中的属性; 但是如果我们需要自己单独定义的属性文件的时候, 就要用到 自己指定属性源了: @PropertySource

4.1 PropertySource使用示例

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

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

测试:

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));
    }

}

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文件,默认是不支持的!!


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 hi@niewj.com

×

喜欢就点赞,疼爱就打赏