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

转:http://412887952-qq-com.iteye.com/blog/2292727

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

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

@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
public class PageControllerimplementsEnvironmentAware{

    @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>


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值