zoukankan      html  css  js  c++  java
  • spring boot配置项profiles active

    结论:通用项配置在applicaton.yml,区别环境配置在application-{profile}.yml中

    一直不知道这个参数要不要配,配了有什么用,今天搭一个工程来检验

    此项作用:用来区分不同环境配置

    application-dev.yml 开发环境

    application-test.yml 测试环境

    application-prod.yml 生产环境

    applicaton.yml(通用项配置)用spring.profile.active=dev来决定启用上面的哪个环境配置文件(不同环境不同配置)

    1.搭个简单的spring boot工程

    此处省略过程,详细可参考。

    结果目录结构

    注:在服务器上时,yml文件与jar包同一级,若需指定路径时,启动脚本中,带参数--spring.config.location=/home/ap/testapp/myproject/config/

    2.文件内容

    application-dev.yml 

    application-test.yml 

    application-prod.yml 

     

    application.yml

    spring:
      application:
        name: profileactivetest-service
      profiles:
        active: dev
    server:
      port: 8707

    3.编写controller类ProfileTestController.java

    package com.example.profileactivetest;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @Title:
     * @Auther: test
     * @Date: 2019/7/3 8:54
     * @Version: 1.0
     * @Description:
     */
    @RestController
    public class ProfileTestController {
        @Value("${envUrl}")
        private String envUrl;
    
        @RequestMapping("/getEnvUrl")
        public String getParam(){
            return "My envUrl configed as :"+envUrl;
        }
    }

    3.启动测试

    当application.yml中spring.profile.active=prod时

    spring:
      application:
        name: profileactivetest-service
      profiles:
        active: prod
    server:
      port: 8707

    4.附

    pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <!--此工程没有加入聚合工程管理-->
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.6.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example</groupId>
        <artifactId>profileactivetest</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>profileactivetest</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
            <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring-cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>

    启动类

    package com.example.profileactivetest;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class ProfileactivetestApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ProfileactivetestApplication.class, args);
        }
    
    }
  • 相关阅读:
    Python 编码错误解决方案
    Slf4j 打日志的问题 Exception 没有堆栈信息
    朋友遇到过的t厂面试题
    php 加密解密算法
    mysql replace into用法详细说明
    python3 return print 之间的差异
    mac多版本python安装 pymysql
    thinkphp 在做搜索时的注意点
    get_object_vars()
    php中var关键字的作用和意义
  • 原文地址:https://www.cnblogs.com/pu20065226/p/11124445.html
Copyright © 2011-2022 走看看