Spring Boot 环境变量读取 和 属性对象的绑定
来源:互联网 发布:mac 源码安装php7 编辑:程序博客网 时间:2024/06/08 19:18
http://blog.csdn.net/catoop/article/details/50548009
凡是被spring管理的类,实现接口 EnvironmentAware 重写方法 setEnvironment 可以在工程启动时,获取到系统环境变量和application配置文件中的变量。
如:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
@Controller @Service 等被Spring管理的类都支持,注意重写的方法 setEnvironment 是在系统启动的时候被执行。
或者如下Controller:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
我们还可以通过@ConfigurationProperties 读取application属性配置文件中的属性。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- @ConditionOnClass表明该@Configuration仅仅在一定条件下才会被加载,这里的条件是Mongo.class位于类路径上
- @EnableConfigurationProperties将Spring Boot的配置文件(application.properties)中的spring.data.mongodb.*属性映射为MongoProperties并注入到MongoAutoConfiguration中。
- @ConditionalOnMissingBean说明Spring Boot仅仅在当前上下文中不存在Mongo对象时,才会实例化一个Bean。这个逻辑也体现了Spring Boot的另外一个特性——自定义的Bean优先于框架的默认配置,我们如果显式的在业务代码中定义了一个Mongo对象,那么Spring Boot就不再创建。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
它就是以spring.data.MongoDB作为前缀的属性,然后通过名字直接映射为对象的属性,同时还包含了一些默认值。如果不配置,那么mongo.uri就是mongodb://localhost/test。
阅读全文
0 0
- Spring Boot 环境变量读取 和 属性对象的绑定
- Spring Boot 环境变量读取 和 属性对象的绑定
- Spring Boot 环境变量读取 和 属性对象的绑定
- Spring Boot 环境变量读取 和 属性对象的绑定
- Spring Boot环境变量读取和属性对象的绑定
- Spring Boot 环境变量读取 和 属性对象的绑定
- Spring Boot 环境变量读取 和 属性对象的绑定
- Spring Boot 环境变量读取 和 属性对象的绑定 RelaxedPropertyResolver
- Spring Boot 环境变量读取 和 属性对象的绑定
- 24. Spring Boot环境变量读取和属性对象的绑定【从零开始学Spring Boot】
- 24. Spring Boot环境变量读取和属性对象的绑定【从零开始学Spring Boot】
- (24)Spring Boot环境变量读取和属性对象的绑定【从零开始学Spring Boot】
- spring boot environmentaware接口实现环境变量读取和属性对象的绑定
- SpringBoot19 之环境变量读取和属性对象的绑定
- Spring boot 十三 读取 环境变量
- spring boot默认配置文件application.properties的属性读取
- Spring boot 过滤返回对象属性遇到的各种坑
- spring boot pojo 对象日期属性问题
- 学生信息管理系统(使用数据库)
- JAVA企业面试题精选 MyBatis 1-4
- 【Android 开源系列】之图片加载框架
- JAVA的静态变量、静态方法、静态类
- svn总结学习教程文件(二)
- Spring Boot 环境变量读取 和 属性对象的绑定
- HYSBZ
- JAVA代理模式
- Spin Control (上下控件)
- bat route 循环
- shell脚本
- 昆石VOS3000_2.1.2.0在CentOS6.x上安装教程
- laravel session 获取不到
- git初学者容易出现的错误