SpringBoot构建微服务实战 之 Configuration(一)

来源:互联网 发布:楚天手动编程g代码结尾 编辑:程序博客网 时间:2024/05/09 22:59

SpringBoot构建微服务实战 之 Configuration(一)

从本节开始我们将学习一下SpingBoot 中的一些“约定”。


在SpringBoot 启动时会默认加载配置文件信息,SpringBopt加载配置文件的两种主类方法:

  • 加载项目内配置文件(默认配置文件名字为:application.properties 或者 application.yml)

    • 默认为 classpath: 根目录下

    • 默认为classpath/confi: 目录下

  • 加载磁盘配置文件

    • 指定外部文件路径 file:/

    • 指定外部文件配置 file:/conf


下面我们将通过两个实例来演示SpringBoot 是如何读取配置文件信息的。

  • 加载项目内配置文件

    • 在classpath 或者 classpath:/conf 目录下新建两个 配置文件 application.properties 和 application.yml
      这里写图片描述

      如果你使用的IDE 时 STS 的话,这两个文件将会被特殊标记。

    • 在两个配置文件中配置一些信息

      • application.properties
      local.ip=192.168.100.19local.port=9053local.area=${local.En}local.En=ASPS
      • application.yml(该类配置文件以一定的格式限制)
      Market:  Domain: Na
    • 在 App.java 文件中读取以上配置文件信息

      ConfigurableApplicationContext context = SpringApplication.run(App.class, args);// 静态读取配置信息context.getBean(UnitConfiguration.class).show();// 两大类读取配置信息context.getBean(IntegrationConfiguration.class).integrationConfiguration();context.getBean(UATConfiguration.class).uatConfiguration();// 获取配置属性的方式System.out.println(context.getEnvironment().getProperty("Market.Domain"));System.out.println(context.getBean(ListExmaple.class));
    • 结果
      这里写图片描述


  • 加载磁盘配置文件

    • F:/Support 盘下 新建一个properies 文件:IntegrationSupport.properties

      IntegrationSupport.type =file
    • 在classpath下新建一个配置文件:application-Integration.properties

      Integration.domain=NAIntegration.proxy=BrokerIntegration.port=9015
    • IntegrationConfiguration.java

      package com.springBoot.entrance.configuration.statics;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.PropertySource;import org.springframework.context.annotation.PropertySources;import org.springframework.stereotype.Component;@Component/** *SpringBopt加载配置文件的两种主类方法 *1.默认为 classpath: 根目录下 *2.默认为classpath/confi: 目录下 *3.指定外部文件路径 file:/ *4.指定外部文件配置 file:/conf */@PropertySources({    @PropertySource("file:/F:/Support/IntegrationSupport.properties"),@PropertySource("classpath:application-Integration.properties")}) //,public class IntegrationConfiguration {    @Value("${Integration.domain}")    private String integrationDomain;    @Value("${Integration.proxy}")    private String integrationProxy;    @Value("${Integration.port}")    private String integrationPortn;    @Value("Conctract")    private String type;    @Value("${IntegrationSupport.type}")    private String  supportType;    public void integrationConfiguration() throws Exception{        System.out.println("Integration.doamin=" + integrationDomain);        System.out.println("Integration.proxy=" + integrationProxy);        System.out.println("Integration.port=" + integrationPortn);        System.out.println("Integration.type="+ type);        System.out.println("IntegrationSupport.type="+ supportType);    }}

      代码解读:
      @PropertySource:读取指定路径指定文件名的配置文件信息

      @PropertySources:读取指定路径指定文件名的配置文件信息 @PropertySource的集合

      @Value: 将配置文件中指定的配置项 赋给指定的属性 同时还可以直接给某一个 属性赋值

    • App.java

      ConfigurableApplicationContext context = SpringApplication.run(App.class, args);// 静态读取配置信息//  context.getBean(UnitConfiguration.class).show();// 两大类读取配置信息context.getBean(IntegrationConfiguration.class).integrationConfiguration();
    • 结果
      这里写图片描述


小结

  • Spring Boot 读取配置文件有四种默认的方法:

    • 默认为 classpath: application.properties/application.yml

    • 默认为 classpath: /conf/application.properties/application.yml

    • 指定外部文件路径 file:/F:/Support/IntegrationSupport.properties

    • 指定外部文件配置 file:/conf/Support/IntegrationSupport.properties

  • SpringBoot 可以通过 @PropertySource/@PropertySources 来读取指定的配置文件信息(自定义配置文件)