zoukankan      html  css  js  c++  java
  • Spring Boot

    Profile是什么

    Profile我也找不出合适的中文来定义,简单来说,Profile就是Spring Boot可以对不同环境或者指令来读取不同的配置文件。

    Profile使用

    假如有开发、测试、生产三个不同的环境,需要定义三个不同环境下的配置。

    基于properties文件类型

    你可以另外建立3个环境下的配置文件:

    applcation.properties
    application-dev.properties
    application-test.properties
    application-prod.properties

    然后在applcation.properties文件中指定当前的环境:
    spring.profiles.active=test
    这时候读取的就是application-test.properties文件。

    基于yml文件类型

    只需要一个applcation.yml文件就能搞定,推荐此方式。

    spring:
      profiles: 
        active: prod
    
    ---
    spring: 
      profiles: dev  
    
    server: 
      port: 8080  
    
    ---
    spring: 
      profiles: test  
    
    server: 
      port: 8081    
    
    ---
    spring.profiles: prod
    spring.profiles.include:
      - proddb
      - prodmq
    
    server: 
      port: 8082      
    
    ---
    spring: 
      profiles: proddb  
    
    db:
      name: mysql   
    
    ---
    spring: 
      profiles: prodmq   
    
    mq: 
      address: localhost

    此时读取的就是prod的配置,prod包含proddb,prodmq,此时可以读取proddb,prodmq下的配置。

    也可以同时激活三个配置。

    spring.profiles.active: prod,proddb,prodmq

    基于Java代码

    在JAVA配置代码中也可以加不同Profile下定义不同的配置文件,@Profile注解只能组合使用@Configuration和@Component注解。

    @Configuration
    @Profile("prod")
    public class ProductionConfiguration {
    
        // ...
    
    }

    指定Profile

    main方法启动方式:

    // 在Eclipse Arguments里面添加
    --spring.profiles.active=prod

    插件启动方式:

    spring-boot:run -Drun.profiles=prod

    jar运行方式:

    java -jar xx.jar --spring.profiles.active=prod

    除了在配置文件和命令行中指定Profile,还可以在启动类中写死指定,通过SpringApplication.setAdditionalProfiles方法。

    SpringApplication.class

    public void setAdditionalProfiles(String... profiles) {
        this.additionalProfiles = new LinkedHashSet<String>(Arrays.asList(profiles));
    }

    推荐阅读

    干货:2TB架构师四阶段视频教程

    面经:史上最全Java多线程面试题及答案

    面经:史上最全阿里高级Java面试题

    面经:史上最全Spring面试题

    教程:最全Spring Boot全套视频教程

    书籍:进阶Java架构师必看的15本书

    工具:推荐一款在线创作流程图、思维导图软件

    分享Java干货,高并发编程,热门技术教程,微服务及分布式技术,架构设计,区块链技术,人工智能,大数据,Java面试题,以及前沿热门资讯等。

  • 相关阅读:
    小程序中自定义组件
    rem是如何实现自适应布局的?
    基于vue前端状态管理模式
    vue项目使用keep-alive的作用
    JS移动元素的方法
    es6 promise then对异常处理的方法
    async/await 中await接收的promise的问题
    angularjs ngRoute demo
    angularjs $watch demo
    Html5 Geolocation demo
  • 原文地址:https://www.cnblogs.com/java-stack/p/11952549.html
Copyright © 2011-2022 走看看