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


    @Value

    当我们只需要读取配置文件中的某一个配置时,可以通过 @Value 注解获取。

    1. 以 Spring Boot 项目 helloworld 为例,修改实体类 Person 中的代码,使用 @Value 注解进行配置绑定,代码如下。

    package net.biancheng.www.bean;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    
    import java.util.Date;
    import java.util.List;
    import java.util.Map;
    
    @Component
    public class Person {
        @Value("${person.lastName}")
        private String lastName;
        @Value("${person.age}")
        private Integer age;
        @Value("${person.boss}")
        private Boolean boss;
        @Value("${person.birth}")
        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 +
                    '}';
        }
    }

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

                                                                     Spring Boot @Value 注解读取配置文件值

  • 相关阅读:
    leetcode--91--递归与动态规划
    Grunt体验
    JSON标准中引号是双引号,不是单引号!
    npm install
    正则-手机号隐藏中间四位及tips备忘
    node tips
    技术栈
    slice与remove
    substr、substring、slice
    springboot 取消post数据大小限制
  • 原文地址:https://www.cnblogs.com/xiaobaibailongma/p/16343861.html
Copyright © 2020-2023  润新知