凡是被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就能获取到系统环境信息;

*

* @author Angel(QQ:412887952)

* @version v.0.1

*/

@Configuration

public class MyEnvironmentAware implements EnvironmentAware{

//注入application.properties的属性到指定变量中.

@Value("${spring.datasource.url}")

private String myUrl;

/**

*注意重写的方法 setEnvironment 是在系统启动的时候被执行。

*/

@Override

public void setEnvironment(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." 的属性列表值.

RelaxedPropertyResolver relaxedPropertyResolver = 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

publicclassPageControllerimplementsEnvironmentAware{

@Override

publicvoid setEnvironment(Environment environment) {

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 String host;

privateint port = DBPort.PORT;

private String uri = "mongodb://localhost/test";

private String database;

// ... getters/ setters omitted

}

它就是以spring.data.mongodb作为前缀的属性,然后通过名字直接映射为对象的属性,同时还包含了一些默认值。如果不配置,那么mongo.uri就是mongodb://localhost/test。

以上这个配置需要加入依赖:

<!--spring-boot-configuration:spring boot 配置处理器; -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-configuration-processor</artifactId>

<optional>true</optional>

</dependency>

Spring Boot 系列博客】

)前言【从零开始学Spring Boot】 :

http://412887952-qq-com.iteye.com/blog/2291496

)spring boot起步之Hello World【从零开始学Spring Boot】:

http://412887952-qq-com.iteye.com/blog/2291500

)Spring Boot返回json数据【从零开始学Spring Boot】

http://412887952-qq-com.iteye.com/blog/2291508

)Spring Boot使用Druid(编程注入)【从零开始学Spring Boot】

http://412887952-qq-com.iteye.com/blogs/2292376

)Spring Boot普通类调用bean【从零开始学Spring Boot】:

http://412887952-qq-com.iteye.com/blog/2292388

更多查看博客:http://412887952-qq-com.iteye.com/blog

(24)Spring Boot环境变量读取和属性对象的绑定【从零开始学Spring Boot】的更多相关文章

  1. Spring Boot 环境变量读取 和 属性对象的绑定

    网上看到的一些方法,结合我看到的 和我们现在使用的.整理成此文: 第一种方法 参见catoop的博客之 Spring Boot 环境变量读取 和 属性对象的绑定(尊重原创) 第二种方法 class不用 ...

  2. 24. Spring Boot环境变量读取和属性对象的绑定【从零开始学Spring Boot】

    转:http://blog.csdn.net/linxingliang/article/details/52069509 凡是被spring管理的类,实现接口EnvironmentAware 重写方法 ...

  3. 十五、Spring Boot 环境变量读取 和 属性对象的绑定

    凡是被spring管理的类,实现接口 EnvironmentAware 重写方法 setEnvironment 可以在工程启动时,获取到系统环境变量和application配置文件中的变量. 如: @ ...

  4. 43. Spring Boot动态数据源(多数据源自动切换)【从零开始学Spring Boot】

    [视频&交流平台] àSpringBoot视频 http://study.163.com/course/introduction.htm?courseId=1004329008&utm ...

  5. 1.spring boot起步之Hello World【从零开始学Spring Boot】

    [视频&交流平台] àSpringBoot视频 http://study.163.com/course/introduction.htm?courseId=1004329008&utm ...

  6. 0. 前言【从零开始学Spring Boot】

    [视频&交流平台] àSpringBoot视频 http://study.163.com/course/introduction.htm?courseId=1004329008&utm ...

  7. 0. 资料官网【从零开始学Spring Boot】

    [视频&交流平台] àSpringBoot视频 http://study.163.com/course/introduction.htm?courseId=1004329008&utm ...

  8. (43). Spring Boot动态数据源(多数据源自动切换)【从零开始学Spring Boot】

    在上一篇我们介绍了多数据源,但是我们会发现在实际中我们很少直接获取数据源对象进行操作,我们常用的是jdbcTemplate或者是jpa进行操作数据库.那么这一节我们将要介绍怎么进行多数据源动态切换.添 ...

  9. (42)Spring Boot多数据源【从零开始学Spring Boot】

    我们在开发过程中可能需要用到多个数据源,我们有一个项目(MySQL)就是和别的项目(SQL Server)混合使用了.其中SQL Server是别的公司开发的,有些基本数据需要从他们平台进行调取,那么 ...

随机推荐

  1. ios添加麦克风访问权限

    不然程序崩溃: This app has crashed because it attempted to access privacy-sensitive data without a usage d ...

  2. SpringMVC中的 --- 异常处理

    系统异常处理器SimpleMappingExceptionResolver 处理器方法执行过程中,可能会发生异常,不想看到错误黄页,想看到一个友好的错误提示页. 自定义异常处理器 使用异常处理注解

  3. Ubuntu17.10 下配置caffe 仅CPU i386可以直接apt install caffe-cpu,但是怎么运行mnist代码我懵逼了

    Ubuntu16.04下配置caffe(仅CPU)  参考:http://blog.csdn.net/zt_1995/article/details/56283249   第二次配置caffe环境,依 ...

  4. B1060 [ZJOI2007]时态同步 dfs

    两遍dfs,第一遍有点像找重链,第二遍维护答案,每个点维护一个当前深度,然后就没啥了. ps:memset(lst,-1,sizeof(lst));这一句多余的话让我debug半天... 题干: De ...

  5. Win7系统专栏

    1.去掉Win7快捷方式小箭头的方法如下: 使用普通方法会使系统出现异常,比如开始菜单程序无法删除.收藏夹无法展开等,网上流传使用透明图标的方法会在快捷方式上留下一块黑痣,下面的方法是小君研究出来的, ...

  6. (Go)07.Go语言中strings和strconv包示例代码详解02

    1.strings使用 统计字符串出现次数 strings.Count(s string, substr string) int Count 用于计算字符串 substr 在字符串 s 中出现的非重叠 ...

  7. Windows phone开发数据绑定系列(1)--了解数据绑定

    (部分内容参考MSDN文档) 数据绑定是在应用程序UI与业务逻辑之间建立连接的过程.通过数据绑定的方式实现了后台数据和前台UI元素的关联, 为用户提供了更好地交互体验. 数据绑定一般有以下几种体现方式 ...

  8. 单个句子<code> 多行代码显示<pre> 键盘输入<kbd>

    1.用来显示单个句子或者单个单词:<code>……</code> 2.用来显示多行代码:<pre>……</pre> 当行数高度大于340px,自动出现y ...

  9. Centos6.6 安装Memcached

    一.环境介绍 1)Centos6.4 2) memcached-1.4.24 二.部署安装 计划具体部署步骤: 步骤1:安装 步骤2:配置 步骤3:运行 步骤4:检查 现在开始: 1)安装 $ yum ...

  10. IIS7部署网站的一些细节问题。

    1.不能在此路径中使用此配置节.如果在父级别上锁定了该节,便会出现这种情况. 这个错误的原因是在 IIS 7中 采用了更安全的 web.config 管理机制,默认情况下会锁住配置项.要取消锁定可以以 ...