我们之前用到的@Component、@ConfigurationProperties他会默认到全局配置文件application.properties中获取,如果想要到指定的配置文件中获取可以通过@PropertySource注解进行配置。
组件
package com.itrucheng.springboot.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
@PropertySource(value = {"classpath:person.properties"}) //加上这个注解后,默认就从指定的配置文件中获取
@Component //下面的注解依赖于这个注解,必须要将对象放在spring容器中才能使用
@ConfigurationProperties(prefix = "person") //书写这个是从yml、properties文件中获取名为person的数据,这个也是从全局配置文件中获取
public class Person {
private String name;
private Integer age;
private Boolean boss;
private Map<String, Object> maps;
private List<Object> list;
private Dog dog;
public get/set/toString();