• spring boot——配置文件——配置绑定—— @PropertySource 注解


    @PropertySource 

    如果将所有的配置都集中到 application.properties 或 application.yml 中,那么这个配置文件会十分的臃肿且难以维护,

    因此我们通常会将与 Spring Boot 无关的配置(例如自定义配置)提取出来,写在一个单独的配置文件中,并在对应的 JavaBean 上使用 @PropertySource 注解指向该配置文件。

    1. 以 helloworld 为例,将与 person 相关的自定义配置移动到 src/main/resources 下的 person.properties 中(注意,必须把 application.properties 或 application.yml 中的相关配置删除),如下图。

    person.properties 的配置如下。

    person.last-name=李四
    person.age=12
    person.birth=2000/12/15
    person.boss=false
    person.maps.k1=v1
    person.maps.k2=14
    person.lists=a,b,c
    person.dog.name=dog
    person.dog.age=2
    

      

    2. 在 Person 使用 @PropertySource 注解指向 person.properties,代码如下。

    package net.biancheng.www.bean;
    
    import org.springframework.beans.factory.annotation.Value;
    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;
    
    @PropertySource(value = "classpath:person.properties")//指向对应的配置文件
    @Component
    @ConfigurationProperties(prefix = "person")
    public class Person {
    
        private String lastName;
    
        private Integer age;
    
        private Boolean boss;
    
        private Date birth;
    
        private Map<String, Object> maps;
    
        private List<Object> lists;
    
        private Dog dog;
    
        public Person() {
    
        }
    
        public String getLastName() {
            return lastName;
        }
    
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        public Boolean getBoss() {
            return boss;
        }
    
        public void setBoss(Boolean boss) {
            this.boss = boss;
        }
    
        public Date getBirth() {
            return birth;
        }
    
        public void setBirth(Date birth) {
            this.birth = birth;
        }
    
        public Map<String, Object> getMaps() {
            return maps;
        }
    
        public void setMaps(Map<String, Object> maps) {
            this.maps = maps;
        }
    
        public List<Object> getLists() {
            return lists;
        }
    
        public void setLists(List<Object> lists) {
            this.lists = lists;
        }
    
        public Dog getDog() {
            return dog;
        }
    
        public void setDog(Dog dog) {
            this.dog = dog;
        }
    
        public Person(String lastName, Integer age, Boolean boss, Date birth, Map<String, Object> maps, List<Object> lists, Dog dog) {
            this.lastName = lastName;
            this.age = age;
            this.boss = boss;
            this.birth = birth;
            this.maps = maps;
            this.lists = lists;
            this.dog = dog;
        }
    
        @Override
        public String toString() {
            return "Person{" +
                    "lastName='" + lastName + '\'' +
                    ", age=" + age +
                    ", boss=" + boss +
                    ", birth=" + birth +
                    ", maps=" + maps +
                    ", lists=" + lists +
                    ", dog=" + dog +
                    '}';
        }
    }
    

      

    3. 重启项目,使用浏览器访问 “http://localhost:8081/hello”,结果如下图。

     
                                                                                              Spring Boot 将 person.properties 的属性注入
  • 相关阅读:
    【LeetCode 104_二叉树_遍历】Maximum Depth of Binary Tree
    【LeetCode 110_二叉树_遍历】Balanced Binary Tree
    【LeetCode 111_二叉树_遍历】Minimum Depth of Binary Tree
    【剑指Offer】36两个链表的第一个公共结点
    【剑指Offer】34第一个只出现一次的字符
    【剑指Offer】33丑数
    【剑指Offer】32把数组排成最小的数
    xgboost的原理没你想像的那么难(转载)
    【剑指Offer】31整数中1出现的次数(从1到n整数中1出现的次数)
    【剑指Offer】28连续子数组的最大和
  • 原文地址:https://www.cnblogs.com/xiaobaibailongma/p/16343865.html
Copyright © 2020-2023  润新知