• 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配置

    好了,玩的开心

  • 相关阅读:
    05.九个内置对象
    04.线程面试题-01
    03.反射杂谈
    02.Java动态代理实现与原理分析之静态代理
    01.JDBC技术摘要
    异步请求二
    表单验证(添加数据)
    异步请求(删除json数据)
    异步请求(解析json数据)
    异步请求(获取json数据)
  • 原文地址:https://www.cnblogs.com/zhenghengbin/p/11103998.html
Copyright © 2020-2023  润新知