@ConfigurationProperties和@Value的区别

表格:

@ConfigurationProperties

@Value

示例

实现功能

批量注入配置文件的属性值

一个一个指定

松散绑定(松散语法)

支持

不支持

last_name == lastName last-name == lastName

SpEL

不支持

支持

#{10*2}

复杂类型封装

支持

不支持

${emp.map}

JSR303数据校验

支持

不支持

如下3.6

  JSR303数据校验_配置文件注入的值

   校验邮箱:

    emp类的上面添加 @Validated数据校验注解

    在 lastName 属性上添加 @Email 注解

代码案例:

package com.cc.springboot01init.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.Email;
import java.util.Date;
import java.util.List;
import java.util.Map;
//校验
@Validated
//必须将当前组件作为 spring boot中的一个组件来使用,这样才会纳入容器中管理
@Component
//告诉spring boot将配置文件中的对应属性值,映射到这个组件类中,进行一一绑定
//配置文件中的前缀名
//@ConfigurationProperties(prefix = "emp")
public class Emp {

   @Value("${emp.last-name}")
    @Email
    private String lastName;
//   @Value("#{10*2}")
    private Integer age;
//   @Value("10000")
    private Double salary;
    private Boolean boos;
    private Date birthday;

     private Map map;
     private List list;

     private Forte forte;

    @Override
    public String toString() {
        return "Emp{" +
                "lastName='" + lastName + '\'' +
                ", age=" + age +
                ", salary=" + salary +
                ", boos=" + boos +
                ", birthday=" + birthday +
                ", map=" + map +
                ", list=" + list +
                ", forte=" + forte +
                '}';
    }

    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 Double getSalary() {
        return salary;
    }

    public void setSalary(Double salary) {
        this.salary = salary;
    }

    public Boolean getBoos() {
        return boos;
    }

    public void setBoos(Boolean boos) {
        this.boos = boos;
    }

    public Date getBirthday() {
        return birthday;
    }

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

    public Map getMap() {
        return map;
    }

    public void setMap(Map map) {
        this.map = map;
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    public Forte getForte() {
        return forte;
    }

    public void setForte(Forte forte) {
        this.forte = forte;
    }
}

 使用场景:

 如果只是在某个业务逻辑中需要获取配置文件中的某个属性值,就使用 @Value

 代码:

package com.cc.springboot01init.controller;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

//请求类
@Controller
public class InitController {

    @Value("${emp.last-name}")
    private String name;

    @ResponseBody
    @RequestMapping("/say")
    public String init(){
        return "hello "+name;
    }
}

如果专门使用javaBean和配置文件进行映射,就使用@ConfigurationProperties

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值