上一节:
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") // 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 应用留神
-
能够指定一个配置文件, 也能够指定多个:
@PropertySource(“classpath:/person.properties”), 也能够指定多个:
@PropertySource(value = {“classpath:/person.properties”}) // 1 指定自定义的 properties 配置文件
-
ConfigurationProperties 也扔须要配合应用;
@PropertySource(value = {"classpath:/person.properties"}) // 1 指定自定义的 properties 配置文件 @ConfigurationProperties(prefix = "person") // 2
//@PropertySource("classpath:/person.yml")
// 谬误! 3 yml 文件, 默认是不反对的!!