Java:spring Value注解用法详解
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
从上往下覆盖,也就是说,最下的优先级是最低的
Devtools global settings properties in the
$HOME/.config/spring-boot
folder when devtools is active.@TestPropertySource
annotations on your tests.properties
attribute on your tests. Available on@SpringBootTest
and the test annotations for testing a particular slice of your application.Command line arguments.
Properties from
SPRING_APPLICATION_JSON
(inline JSON embedded in an environment variable or system property).ServletConfig
init parameters.ServletContext
init parameters.JNDI attributes from
java:comp/env
.Java System properties (
System.getProperties()
).OS environment variables.
A
RandomValuePropertySource
that has properties only inrandom.*
.Profile-specific application properties outside of your packaged jar (
application-{profile}.properties
and YAML variants).Profile-specific application properties packaged inside your jar (
application-{profile}.properties
and YAML variants).Application properties outside of your packaged jar (
application.properties
and YAML variants).Application properties packaged inside your jar (
application.properties
and YAML variants).@PropertySource
annotations on your@Configuration
classes. Please note that such property sources are not added to theEnvironment
until the application context is being refreshed. This is too late to configure certain properties such aslogging.*
andspring.main.*
which are read before refresh begins.Default properties (specified by setting
SpringApplication.setDefaultProperties
).
Java:spring Value注解用法详解相关推荐
- java spring框架 注解_详解Java的Spring框架中的注解的用法
1. 使用Spring注解来注入属性 1.1. 使用注解以前我们是怎样注入属性的类的实现: class UserManagerImpl implements UserManager { private ...
- Java 注解用法详解——@SuppressWarnings
转自: https://www.cnblogs.com/fsjohnhuang/p/4040785.html Java魔法堂:注解用法详解--@SuppressWarnings 一.前言 编码时我们总 ...
- springboot @Valid 注解用法详解
@Valid 注解通常用于对象属性字段的规则检测,具体啥意思,下面让我娓娓道来: 下面我们以新增一个员工为功能切入点,以常规写法为背景,慢慢烘托出 @Valid 注解用法详解. 那么,首先,我们会有一 ...
- @Valid 和 @Validated 注解用法详解
目录 案例引入 @Valid 详解 @Validated 详解 @Valid 和 @Validated 比较 案例引入 下面我们以新增一个员工为功能切入点,以常规写法为背景,慢慢烘托出 @Valid ...
- eval 函数 java_[Java教程]eval()函数用法详解
[Java教程]eval()函数用法详解 0 2015-12-14 11:00:26 eval()函数用法详解: 此函数可能使用的频率并不是太高,但是在某些情况下具有很大的作用,下面就介绍一下eval ...
- Java语言Socket接口用法详解
Socket接口用法详解 在Java中,基于TCP协议实现网络通信的类有两个,在客户端的Socket类和在服务器端的ServerSocket类,ServerSocket类的功能是建立一个Serve ...
- java中String的用法详解
在这里先分别对String.StringBuffer.StringBuilder进行解析,在比较三者异同 String String类:代表字符串.Java程序中所有的字符串字面值(如"fd ...
- java spring 是什么_Spring详解(一)------概述
本系列教程我们将对 Spring 进行详解的介绍,相信你在看完后一定能够有所收获. 1.什么是 Spring ? Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开 ...
- Java动态数组的用法详解
Java动态数组是一种可以任意伸缩数组长度的对象,在Java中比较常用的是ArrayList,ArrayList是javaAPI中自带的java.util.ArrayList.下面介绍一下ArrayL ...
- Java魔法堂:注解用法详解——@SuppressWarnings
一.前言 编码时我们总会发现如下变量未被使用的警告提示: 上述代码编译通过且可以运行,但每行前面的"感叹号"就严重阻碍了我们判断该行是否设置的断点了.这时我们可以在方法前添加 @S ...
最新文章
- hdu 1254(dfs+bfs+优先队列)
- 《UNIX环境高级编程》目录
- oracle之控制用户权限和练习
- 如何实现动态水球图 --》 echars结合echarts-liquidfill实现
- 【动态规划】多重背包
- css圆在中心根据宽度缩放_根据CSS中的容器宽度重新缩放字体
- Jquery对元素应用disabled和readonly
- Python实现快乐的数字
- 电池供电设备增加高精度库仑计
- 大数据软件在高校领域的应用探索—智慧高校解决方案分享
- 用cJSON.h/cJSON.c写JSON
- 1024技术论坛 | C#与.NET技术新发展
- 谷歌浏览器打开特殊端口号地址方法
- 简历||STAR法则
- 【数据分析与挖掘】基于Apriori算法的中医证型的关联规则挖掘(有数据集和代码)
- 关于国密HTTPS的那些事(三)
- 让生活不吃亏的技巧—《谈判》|每周一本书(7)
- 解决Win11连接群晖上的L2TP类型无响应的问题
- ModuleNotFoundError: No module named ‘pip‘;ERROR: Could not find a version that satisfies the requir
- 碳基生命与硅基生命之间的一场思维碰撞