SpringBoot总结(四)——@Value和@ConfigurationProperties的区别

@ConfigurationProperties@Value二者的区别:

@ConfigurationProperties@Value
功能批量注入配置文件的属性一个一个的指定
松散绑定支持不支持
SPEL不支持支持
JSR303数据校验支持不支持
复杂类型封装支持不支持

一、值注入

1、@ConfigurationProperties的使用
首先导入依赖:

  <!--导入配置文件处理器,配置文件进行绑定就会有提示-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

定义一个School类:

School.java

@Component
@ConfigurationProperties(prefix = "school")
public class School {
    private String lastName;
    private String address;
    private Date birth;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Student student;

    public String getLastName() {
        return lastName;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    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 Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    @Override
    public String toString() {
        return "School{" +
                "lastName='" + lastName + '\'' +
                ", address='" + address + '\'' +
                ", birth=" + birth +
                ", maps=" + maps +
                ", lists=" + lists +
                ", student=" + student +
                '}';
    }

Student.java

public class Student {
    private String name;
    private Integer age;
    private String score;

    public Student() {
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

    public String getScore() {
        return score;
    }

    public void setScore(String score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score='" + score + '\'' +
                '}';
    }

配置文件:

application.yml

school:
  lastName: xx学校
  address: 江苏
  birth: 2020/01/01
  maps: {k1:v1,k2:v2}
  lists:
    - list1
    - list2
  student:
    name: 张三
    age: 17

测试类:

@SpringBootTest
class Springboot02ApplicationTests {
    @Autowired
    School  school;
    @Test
   public void contextLoads() {
        System.out.println(school);
    }
}

运行结果:
在这里插入图片描述
2、@Value的使用
@Value 也能从配置文件中获取属性的值,需要一个属性一个属性的进行绑定。

School.java

@Component
//@ConfigurationProperties(prefix = "school")
    public class School {
        @Value("XXX高级中学")
        private String lastName;

        @Value("${school.address}") 
        private String address;


        private Date birth;
        private Map<String,Object> maps;
        private List<Object> lists;

        private Student student;

        public String getLastName() {
            return lastName;
        }

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

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        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 Student getStudent() {
            return student;
        }

        public void setStudent(Student student) {
            this.student = student;
        }

        @Override
        public String toString() {
            return "School{" +
                    "lastName='" + lastName + '\'' +
                    ", address='" + address + '\'' +
                    ", birth=" + birth +
                    ", maps=" + maps +
                    ", lists=" + lists +
                    ", student=" + student +
                    '}';
        }

测试结果:
在这里插入图片描述

二、松散绑定

在JavaBean里写的是lastName(驼峰写法),配置文件里可以写成lastName,也可以写成last-name。
在这里插入图片描述
测试结果:
在这里插入图片描述
注意:@ConfigurationProperties 是支持这种命名的;而@Value 是不支持!

在这里插入图片描述

三、SPEL

SpEL即Spring表达式语言(Spring Expression Language)。SpEL表达式的默认格式为: #{expression}。SpEL表达式以“#”开头,表达式主体包围在花括号中。

@Component
public class Person {
    private  String name;

    @Value("#{2*6}")
    private  Integer age;

测试结果:
在这里插入图片描述
@ConfigurationProperties 不支持 SPEL不再演示。

四、JSR303校验

注:使用之前需要添加jar包:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

示例代码:

@Component
@Validated
@ConfigurationProperties(prefix = "school")
    public class School {

      // @Value("${school.last_name}") 
        @Length(min=5, max=20, message="用户名长度必须在5-20之间")
        private String lastName;

application.yml

school:
  last_name: 学校

运行效果:
在这里插入图片描述
注:@ConfigurationProperties支持 JSR303 数据校验。

         @Value("${school.last_name}") 
        @Length(min=5, max=20, message="用户名长度必须在5-20之间")
        private String lastName;

在这里插入图片描述
可以看出:@Value是不支持 JSR303 数据校验的。

五、复杂类型封装

  @Value("${school.student}")
        private Student student;

这样程序运行是直接报错的。@Value不支持复杂类型封装。在上面的例子中已经证明@ConfigurationProperties是支持复杂类型封装。

若文章中有错误的地方欢迎大家反馈或者留言,十分感谢!!!

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小Java开发者

“是一种鼓励,你懂的”

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值