Spring Boot 2.x实战7 - Spring 5.x基础5 - Environment

2.7 应用环境 - Environment

Spring为我们提供了一个接口Environment用来代表当前运行应用的环境,这个环境包含两个部分:

  • Profile:指的是一组命名的、定义在一起的Bean。我们通常为不同的应用场景(生产、开发,测试等)定义。
  • Property:指的是配置属性,我们可以从properties文件、JVM系统属性、操作系统环境变量等外部来获得配置属性。
2.7.1 场景 - @Profile

我们可以通过@Profile注解指定当前的运行场景,@Profile可以和@Component等、@Configuration@Bean一起使用,当然也分别限制了@Profile起效的Bean的分组。

下面使用需要显示不同操作系统的列表命令的Bean:

public class CommandService {
    private String listCommand;

    public CommandService(String listCommand) {
        this.listCommand = listCommand;
    }

    public void list(){
        System.out.println("当前系统下列表命令是:" + listCommand);
    }
}

在开发环境Windows下的配置为:

@Configuration
@Profile("dev")
public class WindowsProfileConfig {
    @Bean
    CommandService commandService(){
        return new CommandService("dir");
    }
}

在生产环境Linux下的配置为:

@Configuration
@Profile("production")
public class LinuxProfileConfig {
    @Bean
    CommandService commandService(){
        return new CommandService("ls");
    }
}

当我们配置好了两种不同场景下的Profile,我们需要在应用中配置哪个是激活的Profile,手动配置应该是像下面这样:

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.getEnvironment().setActiveProfiles("production"); 
context.scan("top.wisely");
context.refresh();

但我们使用了Spring Boot,我们只需在application.properties文件配置:

spring.profiles.active=production

我们在JavaConfig里用CommandLineRunner分别运行将Profile置成production和dev:

@Bean
CommandLineRunner profileClr(CommandService commandService){
   return args -> commandService.list();
}

在这里插入图片描述

在这里插入图片描述

2.7.2 属性配置 - @PropertySource

Spring的Environment的属性是由PropertySource组成的,我们可以通过@PropertySource指定外部配置文件的路径,这些配置文件的属性都会以PropertySource的形式注册到Environment中,@PropertySource支持xmlproperties格式,不支持Spring Boot下的YAML格式。

如我们现在添加了2个外部配置文件:

  • author.properties

    author.name=wyf
    
  • book.properties

    book.name=spring boot in battle
    

我们可以用一个配置类来接受这两个文件的配置:

@Configuration
@PropertySources({
        @PropertySource("classpath:author.properties"),
        @PropertySource("classpath:book.properties")
}) //1
public class ExternalConfig {

    Environment env;

    public ExternalConfig(Environment env) { //2
        this.env = env;
    }

    @Value("${book.name}") //3
    private String bookName;

    public void showEnv(){
        System.out.println("作者名字是:" + env.getProperty("author.name")); //4
        System.out.println("书籍名称是:" + bookName);
    }
}
  1. 多个外部配置可以用@PropertySources,若只有一个可以只使用@PropertySource("classpath:book.properties")
  2. 注入Environment的Bean,因只有一个参数,可省略@Autowired
  3. 可以@Value注解获得Environment中的属性,@Value的使用在Spring EL一节有更详细的讲解;
  4. 外部配置的属性都已经在Environment注册,可以直接获取。

新书推荐:

我的新书《从企业级开发到云原生微服务:Spring Boot 实战》已出版,内容涵盖了丰富Spring Boot开发的相关知识
购买地址:https://item.jd.com/12760084.html

在这里插入图片描述

主要包含目录有:

第一章 初识Spring Boot(快速领略Spring Boot的美丽)
第二章 开发必备工具(对常用开发工具进行介绍:包含IntelliJ IDEA、Gradle、Lombok、Docker等)
第三章 函数式编程
第四章 Spring 5.x基础(以Spring 5.2.x为基础)
第五章 深入Spring Boot(以Spring Boot 2.2.x为基础)
第六章 Spring Web MVC
第七章 数据访问(包含Spring Data JPA、Spring Data Elasticsearch和数据缓存)
第八章 安全控制(包含Spring Security和OAuth2)
第九章 响应式编程(包含Project Reactor、Spring WebFlux、Reactive NoSQL、R2DBC、Reactive Spring Security)
第十章 事件驱动(包含JMS、RabbitMQ、Kafka、Websocket、RSocket)
第11章 系统集成和屁股里(包含Spring Integration和Spring Batch)
第12章 Spring Cloud与微服务
第13章 Kubernetes与微服务(包含Kubernetes、Helm、Jenkins、Istio)
多谢大家支持。

发布了116 篇原创文章 · 获赞 10 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/wiselyman/article/details/105733654