都是读取配置文件属性

1.  @ConfigurationProperties(prefix = "person")读取多个属性

@Component
@ConfigurationProperties(prefix = "person")
public class Person {
String LastName;
int age;
boolean boss;
Date birth;
Map<String, String> maps;
List<String> list;
Dog dog;
prefix = "person" 读取前缀为person的属性的值.
yaml文件对应的属性值:
person:
LastName: 飞
age: 12
boss: true
birth: 2019/6/15
maps: {key1:value1,key2:value2}
list: [1,2,3,4,5]
dog:
name: 旺财
age: ${random.int[20]}

1.  @value读取单个属性

@Component
public class Person1 {
@Value("${person.last-name}")
String LastName;
//@Value("${person.age}")
   @Value("#{12*2}")
int age;
@Value("${person.boss}")
boolean boss;
@Value("${person.birth}")
Date birth; Map<String, String> maps;
@Value("${person.list}")
List<String> list; Dog dog;

注意事项:maps 和dog 不能使用@value注解,@Value不支持复杂类型

     

application.properties文件配置

person.last-name=飞
person.age=12
person.boss=true
person.birth=2019/6/15
person.maps.key1=value1
person.maps.key2=value2
person.list=dog,cat
person.dog.name=${person.last-name}_你好
person.dog.age=${random.int(14)}

两者获取值的比较(都能从properties和yaml获值)

  @ConfigurationProperties @value
功能 批量注入配置文件的属性 一个个指定
松散绑定(松散语法) 支持 不支持
SPEL 不支持 支持(计算,如上age的值所示)
JSR303数据校验 支持 (邮箱验证) 不支持
复杂类型封装 支持 不支持

显然,前者支持松绑定的特性更强大,所以在实际开发中建议使用@ConfigurationProperties来读取自定义属性。

@ConfigurationProperties注解和@Value注解的区别的更多相关文章

  1. Spring中@Autowired注解、@Resource注解的区别 (zz)

    Spring中@Autowired注解.@Resource注解的区别 Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@ ...

  2. SpringBoot 中常用注解@Controller/@RestController/@RequestMapping的区别

    SpringBoot中常用注解@Controller/@RestController/@RequestMapping的区别 @Controller 处理http请求 @Controller //@Re ...

  3. 【lombok】lombok---帮你简化生成必要但臃肿的java代码工具 【映射注解和lombok注解同时使用 以及 映射注解放在属性和get方法上的区别】

    官方地址:https://projectlombok.org/ GitHub:https://github.com/rzwitserloot/lombok 指导说明文档:http://jnb.ociw ...

  4. [转帖]java注解与注释注解区别

    https://baijiahao.baidu.com/s?id=1615942718081024481&wfr=spider&for=pc 还需要仔细看一下书的 书里面都有. jav ...

  5. Springboot注解--@Controller和@RestController的区别

    1.使用@Controller 注解,在对应的方法上,视图解析器可以解析return 的jsp,html页面,并且跳转到相应页面:若返回json等内容到页面,则需要加@ResponseBody注解 2 ...

  6. JavaEE开发之Spring中的条件注解组合注解与元注解

    上篇博客我们详细的聊了<JavaEE开发之Spring中的多线程编程以及任务定时器详解>,本篇博客我们就来聊聊条件注解@Conditional以及组合条件.条件注解说简单点就是根据特定的条 ...

  7. JavaEE开发之Spring中的条件注解、组合注解与元注解

    上篇博客我们详细的聊了<JavaEE开发之Spring中的多线程编程以及任务定时器详解>,本篇博客我们就来聊聊条件注解@Conditional以及组合条件.条件注解说简单点就是根据特定的条 ...

  8. Spring 注解编程之注解属性别名与覆盖

    前两篇文章咱聊了深入了解了 Spring 注解编程一些原理,这篇文章我们关注注解属性方法,聊聊 Spring 为注解的带来的功能,属性别名与覆盖. 注解属性方法 在进入了解 Spring 注解属性功能 ...

  9. 【Java8新特性】重复注解与类型注解,你真的学会了吗?

    写在前面 在Java8之前,在某个类或者方法,字段或者参数上标注注解时,同一个注解只能标注一次.但是在Java8中,新增了重复注解和类型注解,也就是说,从Java8开始,支持在某个类或者方法,字段或者 ...

  10. Springboot--元注解及自定义注解(表单验证)

    本文简单说明一下元注解,然后对元注解中的@Retention做深入的讨论,在文章最后使用元注解写一个自定义注解来结尾. 一.结论: @Target:注解的作用目标 @Target(ElementTyp ...

随机推荐

  1. Spark资源调度及任务调度

    1.  资源分配 通过SparkSubmit进行提交应用后,首先会创建Client将应用程序(字节码文件.class)包装成Driver,并将其注册到Master.Master收到Client的注册请 ...

  2. Could not get JDBC Connection; nested exception is java.sql.SQLException: ${jdbc.driver}

    在一个SSM分布式项目中一个服务报错: ### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnec ...

  3. Jmeter获取 json字符的另外一种写法

    在jmeter使用过程中,我们经常会看到接口返回数据类型为application/json,也就时我们常说的json格式. 而在功能测试时,我们经常会要对它的结果进行断言,确认结果是否与预期一致,有时 ...

  4. PAT 甲级 1074 Reversing Linked List (25 分)(链表部分逆置,结合使用双端队列和栈,其实使用vector更简单呐)

    1074 Reversing Linked List (25 分)   Given a constant K and a singly linked list L, you are supposed ...

  5. LeetCode_447. Number of Boomerangs

    447. Number of Boomerangs Easy Given n points in the plane that are all pairwise distinct, a "b ...

  6. 【tensorflow基础】TensorFlow查看GPU信息

    re 1. TensorFlow查看GPU信息; end

  7. [LeetCode] 200. Number of Islands 岛屿的数量

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  8. [LeetCode] 229. Majority Element II 多数元素 II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The a ...

  9. PDF阅读器关闭“使用手型工具阅读文章”功能

    1.问题描述 某些PDF文件打开时,光标显示的手型工具里面有个箭头,一点击鼠标左键,就跳转到下一页了.给阅读带来很多不便. 2.原因 因为这类PDF文档中带有"文章"(articl ...

  10. nuxt/eapress 安装报错Module build failed: ValidationError: PostCSS Loader Invalid OptionsModule build failed: ValidationError: PostCSS Loader Invalid Options options['useConfigFile'] is an invalid additi

    错误信息: Module build failed: ValidationError: PostCSS Loader Invalid Options options['useConfigFile'] ...