zoukankan      html  css  js  c++  java
  • SpringBoot(十九)_spring.profiles.active=@profiles.active@ 的使用

    现在在的公司用spring.profiles.active=@profiles.active@ 当我看到这个的时候,一脸蒙蔽,这个@ 是啥意思。

    这里其实是配合 maven profile进行选择不同配置文件进行开发

    实战

    1.构建一个springboot 项目

    这里使用idea进行构建的,这个过程省略

    2.pom文件配置

    <profiles>
            <profile>
                <!-- 生产环境 -->
                <id>prod</id>
                <properties>
                    <profiles.active>prod</profiles.active>
                </properties>
            </profile>
            <profile>
                <!-- 本地开发环境 -->
                <id>dev</id>
                <properties>
                    <profiles.active>dev</profiles.active>
                </properties>
                <activation>
                    <activeByDefault>true</activeByDefault>
                </activation>
            </profile>
            <profile>
                <!-- 测试环境 -->
                <id>test</id>
                <properties>
                    <profiles.active>test</profiles.active>
                </properties>
            </profile>
        </profiles>
    
    • 这里默认dev配置

    3.配置多个配置文件

    application.properties

    注意这里的profiles.active 要和pom文件的对应上

    spring.profiles.active=@profiles.active@
    
    

    application-dev.properties

    name = "dev"
    

    application-prod.properties

    name = "prod"
    

    application-test.properties

    name = "test"
    

    4.编写个测试的controller

    
    /**
     * @author kevin
     * @date 2019/6/28 16:12
     */
    @RestController
    public class HelloController {
    
        @Value("${name}")
        private String name;
    
        @RequestMapping(value = {"/hello"},method = RequestMethod.GET)
        public String say(){
            return name;
        }
    }
    

    5.启动测试

    使用idea工具启动开发

    默认是dev,假如想要使用prod配置文件,如上图选择prod,注意下面的导入,重启项目

    D:dev_codeprofiles-demo	arget>curl http://localhost:8080/hello
    "prod"
    
    

    6 打包

    这里使用idea打包不再介绍,如果你使用命令

    mvn clean package -P dev
    
    

    则是使用dev配置

    好了,玩的开心

  • 相关阅读:
    备份
    Ibatis_dataMapper
    查询成绩都大于80分的学生
    删除文件的工具
    从运行中启动收索引擎
    数据库Northwind
    搭建Android开发环境
    数据库知识结构
    数据库MedicineMis_STD
    数据库work
  • 原文地址:https://www.cnblogs.com/zhenghengbin/p/11103998.html
Copyright © 2011-2022 走看看