24. Spring Boot环境变量读取和属性对象的绑定【从零开始学Spring Boot】
转:http://blog.csdn.net/linxingliang/article/details/52069509
凡是被spring管理的类,实现接口EnvironmentAware 重写方法 setEnvironment 可以在工程启动时,获取到系统环境变量和application配置文件中的变量。
com.kfit.environment.MyEnvironmentAware :
package com.kfit.environment;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
/**
* 主要是@Configuration,实现接口:EnvironmentAware就能获取到系统环境信息;
*
* @authorAngel(QQ:412887952)
* @version v.0.1
*/
@Configuration
public class MyEnvironmentAware implements EnvironmentAware{
//注入application.properties的属性到指定变量中.
@Value("${spring.datasource.url}")
private String myUrl;
/**
*注意重写的方法 setEnvironment 是在系统启动的时候被执行。
*/
@Override
public voidsetEnvironment(Environment environment) {
//打印注入的属性信息.
System.out.println("myUrl="+myUrl);
//通过 environment 获取到系统属性.
System.out.println(environment.getProperty("JAVA_HOME"));
//通过 environment 同样能获取到application.properties配置的属性.
System.out.println(environment.getProperty("spring.datasource.url"));
//获取到前缀是"spring.datasource." 的属性列表值.
RelaxedPropertyResolverrelaxedPropertyResolver = new RelaxedPropertyResolver(environment,"spring.datasource.");
System.out.println("spring.datasource.url="+relaxedPropertyResolver.getProperty("url"));
System.out.println("spring.datasource.driverClassName="+relaxedPropertyResolver.getProperty("driverClassName"));
}
}
其中application.properties文件信息是:
########################################################
###datasource
########################################################
spring.datasource.url =jdbc:MySQL://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName =com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
@Controller@Service 等被Spring管理的类都支持,注意重写的方法setEnvironment 是在系统启动的时候被执行。
或者如下Controller:
@Controller
public class PageControllerimplementsEnvironmentAware{
@Override
public void setEnvironment(Environmentenvironment) {
String s= environment.getProperty("JAVA_HOME");
System.out.println(s);
}
}
我们还可以通过@ConfigurationProperties 读取application属性配置文件中的属性。
· @Configuration
· @ConditionalOnClass(Mongo.class)
· @EnableConfigurationProperties(MongoProperties.class)
· publicclassMongoAutoConfiguration {
·
· @Autowired
· private MongoProperties properties;
·
· }
· @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就不再创建。
@ConfigurationProperties(prefix = "spring.data.mongodb")
publicclass MongoProperties{
private Stringhost;
privateint port= DBPort.PORT;
private Stringuri = "mongodb://localhost/test";
private Stringdatabase;
// ... getters/ setters omitted
}
它就是以spring.data.mongodb作为前缀的属性,然后通过名字直接映射为对象的属性,同时还包含了一些默认值。如果不配置,那么mongo.uri就是mongodb://localhost/test。
以上这个配置需要加入依赖:
<!--spring-boot-configuration:springboot 配置处理器; -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
24. Spring Boot环境变量读取和属性对象的绑定【从零开始学Spring Boot】的更多相关文章
- Spring Boot 环境变量读取 和 属性对象的绑定
网上看到的一些方法,结合我看到的 和我们现在使用的.整理成此文: 第一种方法 参见catoop的博客之 Spring Boot 环境变量读取 和 属性对象的绑定(尊重原创) 第二种方法 class不用 ...
- (24)Spring Boot环境变量读取和属性对象的绑定【从零开始学Spring Boot】
凡是被Spring管理的类,实现接口 EnvironmentAware 重写方法 setEnvironment 可以在工程启动时,获取到系统环境变量和application配置文件中的变量. com. ...
- 十五、Spring Boot 环境变量读取 和 属性对象的绑定
凡是被spring管理的类,实现接口 EnvironmentAware 重写方法 setEnvironment 可以在工程启动时,获取到系统环境变量和application配置文件中的变量. 如: @ ...
- 43. Spring Boot动态数据源(多数据源自动切换)【从零开始学Spring Boot】
[视频&交流平台] àSpringBoot视频 http://study.163.com/course/introduction.htm?courseId=1004329008&utm ...
- 1.spring boot起步之Hello World【从零开始学Spring Boot】
[视频&交流平台] àSpringBoot视频 http://study.163.com/course/introduction.htm?courseId=1004329008&utm ...
- 0. 前言【从零开始学Spring Boot】
[视频&交流平台] àSpringBoot视频 http://study.163.com/course/introduction.htm?courseId=1004329008&utm ...
- 0. 资料官网【从零开始学Spring Boot】
[视频&交流平台] àSpringBoot视频 http://study.163.com/course/introduction.htm?courseId=1004329008&utm ...
- (43). Spring Boot动态数据源(多数据源自动切换)【从零开始学Spring Boot】
在上一篇我们介绍了多数据源,但是我们会发现在实际中我们很少直接获取数据源对象进行操作,我们常用的是jdbcTemplate或者是jpa进行操作数据库.那么这一节我们将要介绍怎么进行多数据源动态切换.添 ...
- (42)Spring Boot多数据源【从零开始学Spring Boot】
我们在开发过程中可能需要用到多个数据源,我们有一个项目(MySQL)就是和别的项目(SQL Server)混合使用了.其中SQL Server是别的公司开发的,有些基本数据需要从他们平台进行调取,那么 ...
随机推荐
- ci日志记录
log_message($level, $message) 参数: $level (string) -- Log level: 'error', 'debug' or 'info' $message ...
- loadrunner rtsp协议模拟
在核心网做过3年的sip消息模拟,所以rtsp消息模拟只要知道信令消息交互就非常顺利了 RTSP 实时流传输协议, 是TCP/IP协议体系中的一个应用层协议, 该协议定义了一对多应用程序如何有效地通过 ...
- [LOJ#2328]「清华集训 2017」避难所
[LOJ#2328]「清华集训 2017」避难所 试题描述 "B君啊,你当年的伙伴都不在北京了,为什么你还在北京呢?" "大概是因为出了一些事故吧,否则这道题就不叫避难所 ...
- tomcat 启动慢解决(/dev/random)
JRE默认使用 /dev/random作为随机数来源,当熵池大小不够的时候,random会很慢,造成随机数生成调用阻塞. 解决方案: 改用 /dev/urandom (1) tomcat的启动选项增加 ...
- 数组去重js方式
var selectmap = new Array(); /(\x0f[^\x0f]+)\x0f[\s\S]*\1/.test("\x0f"+selectmap.join(&quo ...
- 使用iview如何使左上的添加按钮和右上的搜索框和边框对齐
使用iview如何使左上的添加按钮和右上的搜索框和边框对齐呢? 效果如下: 使用iview自带的Grid 栅格进行布局,但是由于按钮和搜索框的大小不正好是一个栅格的宽度,所以不是很好跳转,且栅格也不支 ...
- js 清空div
document.getElementById('BIGDraw').innerHTML = ""; $('#BIGDraw').html(""); $('#B ...
- MySQL常用查询方法
SELECT TIME(NOW()); -- 15:23:07 SELECT CURTIME(NOW());-- 15:23:07 SELECT ABS(-4); -- 4 SELECT 5 MOD ...
- PNG图片透明 IE6 解决方法
原文发布时间为:2009-11-18 -- 来源于本人的百度文章 [由搬家工具导入] png透明解决办法 第1 种方法:定义一个样式,给某个div应用这个样式后,div的透明png背景图片自动透明了。 ...
- transfered jquery plugin practice!
jQuery插件开发入门 发表于 2014年05月23日 by 天涯孤雁 被浏览 20,897 次 ======2014-5-27 更新======= Require.js中使用jQuery 插件请查 ...