Value注解是字段或方法/构造函数参数级别的注释,用于分配参数的默认值表达式。我们可以使用@Value注释读取spring环境变量和系统变量。

语法:

@Value("Default DBConfiguration")
private String defaultName;//Method
@Value("Test")
public void printValues(String s, String v){} //both 's' and 'v' values will be 'Test' @Value("Test")
public void printValues(String s, @Value("Data") String v){}
// s=Test, v=Data
用法 例子 说明
默认值(Default Value) @Value("Default DBConfiguration") 仅接受字符串的默认值,但是会根据参数的类型进行转换,比如"true"或者"10"
Spring环境变量(Spring Environment Property)

@Value("${APP_NAME_NOT_FOUND}")

@Value("${APP_NAME_NOT_FOUND:Default}")

获取spring的properties设置的值,如果没有设置,则值为"${APP_NAME_NOT_FOUND}"
系统环境变量(System Environment) @Value("${HOME}") 当Spring环境被填充时,它读取所有系统环境变量并将其存储为属性。所以我们也可以用@Value指定系统变量
SpEL表达式 @Value("#{systemProperties['java.home']}") 请参考SqEL表达式定义
方法 参考用法 当在方法上找到@Value注释时,Spring上下文将在加载所有Spring配置和bean时调用它。如果方法有多个参数,则每个参数值都将从方法批注映射。如果我们需要不同参数的不同值,那么可以直接将@Value注释与参数一起使用。

覆盖顺序

参考文档:https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config

从上往下覆盖,也就是说,最下的优先级是最低的

  1. Devtools global settings properties in the $HOME/.config/spring-boot folder when devtools is active.

  2. @TestPropertySource annotations on your tests.

  3. properties attribute on your tests. Available on @SpringBootTest and the test annotations for testing a particular slice of your application.

  4. Command line arguments.

  5. Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).

  6. ServletConfig init parameters.

  7. ServletContext init parameters.

  8. JNDI attributes from java:comp/env.

  9. Java System properties (System.getProperties()).

  10. OS environment variables.

  11. RandomValuePropertySource that has properties only in random.*.

  12. Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants).

  13. Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants).

  14. Application properties outside of your packaged jar (application.properties and YAML variants).

  15. Application properties packaged inside your jar (application.properties and YAML variants).

  16. @PropertySource annotations on your @Configuration classes. Please note that such property sources are not added to the Environment until the application context is being refreshed. This is too late to configure certain properties such as logging.* and spring.main.* which are read before refresh begins.

  17. Default properties (specified by setting SpringApplication.setDefaultProperties).

Java:spring Value注解用法详解相关推荐

  1. java spring框架 注解_详解Java的Spring框架中的注解的用法

    1. 使用Spring注解来注入属性 1.1. 使用注解以前我们是怎样注入属性的类的实现: class UserManagerImpl implements UserManager { private ...

  2. Java 注解用法详解——@SuppressWarnings

    转自: https://www.cnblogs.com/fsjohnhuang/p/4040785.html Java魔法堂:注解用法详解--@SuppressWarnings 一.前言 编码时我们总 ...

  3. springboot @Valid 注解用法详解

    @Valid 注解通常用于对象属性字段的规则检测,具体啥意思,下面让我娓娓道来: 下面我们以新增一个员工为功能切入点,以常规写法为背景,慢慢烘托出 @Valid 注解用法详解. 那么,首先,我们会有一 ...

  4. @Valid 和 @Validated 注解用法详解

    目录 案例引入 @Valid 详解 @Validated 详解 @Valid 和 @Validated 比较 案例引入 下面我们以新增一个员工为功能切入点,以常规写法为背景,慢慢烘托出 @Valid ...

  5. eval 函数 java_[Java教程]eval()函数用法详解

    [Java教程]eval()函数用法详解 0 2015-12-14 11:00:26 eval()函数用法详解: 此函数可能使用的频率并不是太高,但是在某些情况下具有很大的作用,下面就介绍一下eval ...

  6. Java语言Socket接口用法详解

    Socket接口用法详解   在Java中,基于TCP协议实现网络通信的类有两个,在客户端的Socket类和在服务器端的ServerSocket类,ServerSocket类的功能是建立一个Serve ...

  7. java中String的用法详解

    在这里先分别对String.StringBuffer.StringBuilder进行解析,在比较三者异同 String String类:代表字符串.Java程序中所有的字符串字面值(如"fd ...

  8. java spring 是什么_Spring详解(一)------概述

    本系列教程我们将对 Spring 进行详解的介绍,相信你在看完后一定能够有所收获. 1.什么是 Spring ? Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开 ...

  9. Java动态数组的用法详解

    Java动态数组是一种可以任意伸缩数组长度的对象,在Java中比较常用的是ArrayList,ArrayList是javaAPI中自带的java.util.ArrayList.下面介绍一下ArrayL ...

  10. Java魔法堂:注解用法详解——@SuppressWarnings

    一.前言 编码时我们总会发现如下变量未被使用的警告提示: 上述代码编译通过且可以运行,但每行前面的"感叹号"就严重阻碍了我们判断该行是否设置的断点了.这时我们可以在方法前添加 @S ...

最新文章

  1. hdu 1254(dfs+bfs+优先队列)
  2. 《UNIX环境高级编程》目录
  3. oracle之控制用户权限和练习
  4. 如何实现动态水球图 --》 echars结合echarts-liquidfill实现
  5. 【动态规划】多重背包
  6. css圆在中心根据宽度缩放_根据CSS中的容器宽度重新缩放字体
  7. Jquery对元素应用disabled和readonly
  8. Python实现快乐的数字
  9. 电池供电设备增加高精度库仑计
  10. 大数据软件在高校领域的应用探索—智慧高校解决方案分享
  11. 用cJSON.h/cJSON.c写JSON
  12. 1024技术论坛 | C#与.NET技术新发展
  13. 谷歌浏览器打开特殊端口号地址方法
  14. 简历||STAR法则
  15. 【数据分析与挖掘】基于Apriori算法的中医证型的关联规则挖掘(有数据集和代码)
  16. 关于国密HTTPS的那些事(三)
  17. 让生活不吃亏的技巧—《谈判》|每周一本书(7)
  18. 解决Win11连接群晖上的L2TP类型无响应的问题
  19. ModuleNotFoundError: No module named ‘pip‘;ERROR: Could not find a version that satisfies the requir
  20. 碳基生命与硅基生命之间的一场思维碰撞

热门文章

  1. 公链、私链、联盟链优缺点
  2. VMware 搭建私有云
  3. java 数独算法_Java:递归数独解法计数算法
  4. matlab中正弦信号的功率计算,范数norm
  5. HTML——W3C简介(万维网联盟)
  6. cadence打开license manager无法开启,显示并行配置不正确
  7. LINUX中安装gotop检测页面
  8. WordPress清理和优化数据库,推荐几个优秀插件
  9. tomcat中的过滤器
  10. xman 2018夏solo题