zoukankan      html  css  js  c++  java
  • SpringBoot/Spring使用@Value进行属性绑定(尚硅谷)

    接上篇:springboot使用ConfigurationProperties注解读取自定义属性(传智播客代码)

    部分代码参考上篇,这里用@Value读取值

    1、@Value读取数据

    @Value支持字面量、Spring EL表达式(#),美元符号($),相比于@ConfigurationProperties,他用$取值时需要完全路径

    package com.atguigu.bean;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    import java.util.Date;
    import java.util.List;
    import java.util.Map;
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Component
    public class Person {
    
        /***
         * 美元表达式(从配置文件、系统环境变量读取)
         */
        @Value("${person.lastName}")
        private String lastName;
    
        /***
         * Spring EL表达式
         */
        @Value("#{11*2}")
        private Integer age;
    
        /***
         * 字面量
         */ 
        @Value("true")
        private Boolean boss;
        private Date birth;
    
        private Map<String,Object> maps;
        private List<Object> lists;
        private Dog dog;
    }

     2、ConfigurationProperties注解JSR303数据校验

    package com.atguigu.bean;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    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;
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Component
    @ConfigurationProperties(prefix = "person")
    @Validated
    public class Person {
    
        @Email
        private String lastName;
        private Integer age;
        private Boolean boss;
        private Date birth;
    
        private Map<String,Object> maps;
        private List<Object> lists;
        private Dog dog;
    }

    由于lastName不是有效的邮箱,启动时即报错

    Binding to target org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'person' to com.atguigu.bean.Person failed:
    
        Property: person.lastName
        Value: zhangsan
        Origin: class path resource [application.properties]:2:17
        Reason: 不是一个合法的电子邮件地址

    3、@Value不支持复合数据类型绑定

    测试用例

    package com.atguigu;
    
    import com.atguigu.bean.Person;
    import lombok.extern.slf4j.Slf4j;
    import org.junit.jupiter.api.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import java.util.List;
    import java.util.Map;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    @Slf4j
    class DemoApplicationTests {
    
        @Autowired
        Person person;
    
        @Test
        void customPropertiesTest()
        {
            log.info("{}",person);
        }
    }

    3.1、 $(美元符号读取map直接报错)

    package com.atguigu.bean;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    import org.springframework.validation.annotation.Validated;
    
    import java.util.Date;
    import java.util.List;
    import java.util.Map;
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Component
    public class Person {
    
        /***
         * 美元表达式(从配置文件、系统环境变量读取)
         */
        @Value("${person.lastName}")
        private String lastName;
    
        /***
         * Spring EL表达式
         */
        @Value("#{11*2}")
        private Integer age;
    
        /***
         * 字面量
         */ 
        @Value("true")
        private Boolean boss;
        private Date birth;
    
        @Value("${person.maps}")
        private Map<String,Object> maps;
        private List<Object> lists;
        private Dog dog;
    }

    3、2使用spring el表达式获取不到值

    package com.atguigu.bean;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    import org.springframework.validation.annotation.Validated;
    
    import java.util.Date;
    import java.util.List;
    import java.util.Map;
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Component
    public class Person {
    
        /***
         * 美元表达式(从配置文件、系统环境变量读取)
         */
        @Value("${person.lastName}")
        private String lastName;
    
        /***
         * Spring EL表达式
         */
        @Value("#{11*2}")
        private Integer age;
    
        /***
         * 字面量
         */ 
        @Value("true")
        private Boolean boss;
        private Date birth;
    
        @Value("#{person.maps}")
        private Map<String,Object> maps;
        private List<Object> lists;
        private Dog dog;
    }

    4、@ConfigurationProperties和@Value使用场景区别

  • 相关阅读:
    js如何引入本地json文件
    python学习笔记(八)———— IO编程
    python学习笔记(七)———— 错误、调试和测试
    接口测试
    cookie和token都存放在header中,为什么不会劫持token?
    占位
    MongoDB和MySql的区别(详细)且会持续补充
    【转】五分钟让你彻底了解TDD、ATDD、BDD&RBE
    python学习笔记(六)————面向对象高级编程
    Fiddler模拟接口数据(mock)(四)
  • 原文地址:https://www.cnblogs.com/passedbylove/p/12639020.html
Copyright © 2011-2022 走看看