spring boot使用@Value,@PropertySource注解使用
来源:互联网 发布:我的世界java手机版 编辑:程序博客网 时间:2024/06/05 07:39
RandomValuePropertySource
系统中用到随机数的地方,例如:
- 1
- 2
- 3
- 4
- 5
random.int*
支持value
参数和,max
参数,当提供max
参数的时候,value
就是最小值。
应用配置文件(.properties或.yml)
在配置文件中直接写:
- 1
- 2
.yml
格式的配置文件如:
- 1
- 2
- 3
当有前缀的情况下,使用.yml
格式的配置文件更简单。关于.yml
配置文件用法请看这里
注意:使用.yml
时,属性名的值和冒号中间必须有空格,如name: Isea533
正确,name:Isea533
就是错的。
属性配置文件的位置
spring会从classpath下的/config
目录或者classpath的根目录查找application.properties
或application.yml
。
/config
优先于classpath根目录
@PropertySource
这个注解可以指定具体的属性配置文件,优先级比较低。
SpringApplication.setDefaultProperties
例如:
- 1
- 2
- 3
- 4
- 5
- 6
应用(使用)属性
@Value(“${xxx}”)
这种方式是最简单的,通过@Value
注解可以将属性值注入进来。
@ConfigurationProperties
Spring Boot 可以方便的将属性注入到一个配置对象中。例如:
- 1
- 2
- 3
- 4
对应对象:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
Spring Boot 会自动将prefix="my"
前缀为my
的属性注入进来。
Spring Boot 会自动转换类型,当使用List
的时候需要注意在配置中对List
进行初始化!
Spring Boot 还支持嵌套属性注入,例如:
- 1
- 2
- 3
- 4
对应的配置类:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
jdbc
开头的属性都会注入到Jdbc
对象中。
在@Bean方法上使用@ConfigurationProperties
例如:
- 1
- 2
- 3
- 4
- 5
Spring Boot 会将foo
开头的属性按照名字匹配注入到FooComponent
对象中。
属性占位符
例如:
- 1
- 2
可以在配置文件中引用前面配置过的属性(优先级前面配置过的这里都能用)。
通过如${app.name:默认名称}
方法还可以设置默认值,当找不到引用的属性时,会使用默认的属性。
由于${}
方式会被Maven处理。如果你pom继承的spring-boot-starter-parent
,Spring Boot 已经将maven-resources-plugins
默认的${}
方式改为了@ @
方式,例如@name@
。
如果你是引入的Spring Boot,你可以修改使用其他的分隔符
通过属性占位符还能缩短命令参数
例如修改web默认端口需要使用--server.port=9090
方式,如果在配置中写上:
- 1
那么就可以使用更短的--port=9090
,当不提供该参数的时候使用默认值8080
。
属性名匹配规则
例如有如下配置对象:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
firstName
可以使用的属性名如下:
person.firstName
,标准的驼峰式命名person.first-name
,虚线(-
)分割方式,推荐在.properties
和.yml
配置文件中使用PERSON_FIRST_NAME
,大写下划线形式,建议在系统环境变量中使用
- spring boot使用@Value,@PropertySource注解使用
- (15)spring boot使用@Value,@PropertySource注解使用
- spring boot中使用@Value注解
- Spring的@PropertySource和@Value注解例子
- Spring 4 @PropertySource和@Value注解示例
- Spring的@PropertySource和@Value注解例子
- Spring 4 @PropertySource和@Value注解示例
- Spring的@PropertySource和@Value注解例子
- Springboot中PropertySource注解的使用
- Spring @Value注解的使用
- Spring 从文件系统读取配置文件, 使用@PropertySource
- Spring 使用@PropertySource读取properties文件
- Spring 使用@PropertySource+Emvironment读取properties文件
- spring注解 @PropertySource 配置数据源
- Spring中Value注解的使用
- Spring中Value注解的使用
- 使用spring 的注解 @value使用方法
- Spring @Value注解使用${}进行注入
- Ubuntu下的静态IP配置
- 不平衡数据的数据处理方法(F值)
- 使用CocoaPods给swift项目配置第三方swift框架
- Ubuntu 14.04 下安装ncurses-devel
- 让sublim text3支持vue语法高亮的直接解决办法
- spring boot使用@Value,@PropertySource注解使用
- linux下安装MySQLdb
- 第3章 树莓派的网络配置
- python的py、pyc、pyo、pyd文件区别
- 设计模式-MVC,MVP 和 MVVM
- ROC介绍及AUC计算方法
- Mac Onenote2016 插入链接方法
- javaHashcode与equals
- jenkins的安装和使用