@PropertySource 加载指定配置文件

需求背景

  • 通过《Spring Boot 全局配置文件》《@Value 取赋值详解与 @ConfigurationProperties 对比》已经知道使用“@Value”注解与“@ConfigurationProperties”可以从全局配置文件“application.properties”或者“application.yml”中取值然后为需要的属性赋值
  • 但是如果应用比较大的时候,如果所有的内容都当在一个文件中,如“application.properties”或者“application.yml”中时,就会显得比较臃肿,同时也不太好理解和维护
  • 可以将一个文件拆分为多个,此时使用@PropertySource即可解决问题
  • @PropertySource 用于加载指定的配置文件;


项目结构


待赋值的POJO 属性

package com.lct.wmx.damain;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import javax.validation.constraints.Email;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/7/11 0011.
 * 用户···实体
 *
 * @ConfigurationProperties 表示 告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定;
 * @ConfigurationProperties (prefix = "user") 默认从全局配置文件中获取值然后进行注入
 * prefix = "user" 表示 将配置文件中keyuser的下面所有的属性与本类属性进行一一映射注入值,如果配置文件中
 * 不存在"user"key,则不会为POJO注入值,属性值仍然为默认值
 * <p/>
 * @Component 将本来标识为一个Spring 组件,因为只有是容器中的组件,容器才会为@ConfigurationProperties提供此注入功能
 * @PropertySource (value = {"classpath:user.properties"}) 指明加载类路径下的哪个配置文件来注入值
 */
@PropertySource(value = {"classpath:user.properties"})
@Component
@ConfigurationProperties(prefix = "user")
public class User {
    private Integer id;
    private String lastName;
    private Integer age;
    private Date birthday;
    private List<String> colorList;
    private Map<String, String> cityMap;
    private Dog dog;

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Map<String, String> getCityMap() {
        return cityMap;
    }

    public void setCityMap(Map<String, String> cityMap) {
        this.cityMap = cityMap;
    }

    public List<String> getColorList() {
        return colorList;
    }

    public void setColorList(List<String> colorList) {
        this.colorList = colorList;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String name) {
        this.lastName = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "age=" + age +
                ", id=" + id +
                ", lastName='" + lastName + '\'' +
                ", birthday=" + birthday +
                ", colorList=" + colorList +
                ", cityMap=" + cityMap +
                ", dog=" + dog +
                '}';
    }
}

资源配置文件


user.id=111
user.lastName=张无忌
user.age=120
user.birthday=2018/07/11
user.colorList=red,yellow,green,blacnk
user.cityMap.mapK1=mapV1
user.cityMap.mapK2=mapV2
user.dog.id=7523
user.dog.name=狗不理
user.dog.age=3

测试运行

package com.lct.wmx;

import com.lct.wmx.damain.Dog;
import com.lct.wmx.damain.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

/**
 * SpringBoot单元测试
 * 可以在测试期间很方便的类似编码一样进行自动注入等容器的功能
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class HelloWorldQuickApplicationTests {

    /**
     * 因为POJO类使用了@Component注解,就是Spring一个组件,交由了Spring容器注入值
     * 所以使用@Autowired或者@ResourceDI注入在取值即可
     */
    @Resource
    private User user;

    @Test
    public void contextLoads() {
        System.out.println("------------------------------********************--------------------------------------------");
        System.out.println("·······················" + user);
    }
}



猜你喜欢

转载自blog.csdn.net/wangmx1993328/article/details/81005170