美文网首页springboot
7-springboot多环境配置+如何获取配置文件的值

7-springboot多环境配置+如何获取配置文件的值

作者: Guoyubo | 来源:发表于2018-09-05 14:16 被阅读70次

    版本说明

    springboot:2.0
    maven:3.5
    jdk:1.8
    提示:如果你发现配置和我一样却不行,应该是你的maven或者jdk环境问题

    项目结构:
    图片.png
    1 创建文件

    application-dev.properties
    application-prod.properties
    这里面写不同环境的配置
    比如:


    图片.png

    在application.properties文件里加上这句

    spring.profiles.active=@activatedProperties@

    activatedProperties会自动读取pom文件里的activatedProperties值

    2 在pom文件里添加配置如下配置,和<parent>标签同级的
      <!-- 多环境配置 -->
        <profiles>
            <profile>
                <id>dev</id>
                <properties>
                    <!-- 环境标识,需要与配置文件的名称相对应 -->
                    <activatedProperties>dev</activatedProperties>
                </properties>
                <activation>
                    <!-- 默认环境 -->
                    <activeByDefault>true</activeByDefault>
                </activation>
            </profile>
            <profile>
                <id>prod</id>
                <properties>
                    <activatedProperties>prod</activatedProperties>
                </properties>
            </profile>
        </profiles>
    

    为了防止你们看不懂,贴下我的完整的pom文件如下:

    <?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>
    
        <groupId>com.beacool</groupId>
        <artifactId>weather</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>war</packaging>
    
        <name>weather</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.4.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <!-- 多环境配置 -->
        <profiles>
            <profile>
                <id>dev</id>
                <properties>
                    <!-- 环境标识,需要与配置文件的名称相对应 -->
                    <activatedProperties>dev</activatedProperties>
                </properties>
                <activation>
                    <!-- 默认环境 -->
                    <activeByDefault>true</activeByDefault>
                </activation>
            </profile>
            <profile>
                <id>prod</id>
                <properties>
                    <activatedProperties>prod</activatedProperties>
                </properties>
            </profile>
        </profiles>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <exclusions>
                    <!-- 想要配置log4j2,就要先去除logging包 -->
                    <exclusion>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-logging</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-log4j2</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>com.vaadin.external.google</groupId>
                <artifactId>android-json</artifactId>
                <version>RELEASE</version>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>com.google.code.gson</groupId>
                <artifactId>gson</artifactId>
                <version>2.8.5</version>
            </dependency>
            <!-- 热部署模块 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <optional>true</optional>
            </dependency>
        </dependencies>
    
        <build>
            <finalName>weather</finalName>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <fork>true</fork>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    
    
    3 maven打包命令

    例如打包dev环境的war包
    mvn clean install -DskipTests spring-boot:repackage -Pdev

    4代码里是如何获取不同配置文件里的值的

    maven在编译打包的时候会根据你给的命令加载不同的配置文件,默认加载application-dev.properties文件,
    代码里这么用就行了

        //获取配置文件的值
        @Value("${weather.WEATHER_API}")
        private String WEATHER_API;
     
    

    方法里直接用WEATHER_API的值就可以了

    public void test(){
     System.out.println("WEATHER_API= " + WEATHER_API);
    }
    

    如果要读取自定义的配置文件,那就需要配置下springboot扫描你的配置文件路径了,具体的可参考我的github
    https://github.com/gyb123456/MySpringBootFrame/blob/master/src/main/java/com/demo/gyb/config/IotProperties.java
    里面的代码如下:

    package com.demo.gyb.config;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.context.annotation.PropertySources;
    /**
     * 使得自定义配置文件.property的内容可被获取
     * 20180821 GYB
    */
    @Configuration
    //配置变量前缀
    @ConfigurationProperties(prefix="iot")
    //配置单个配置文件
    //@PropertySource("classpath:application-dev.properties")
    //配置多个配置文件
    @PropertySources({
            @PropertySource("classpath:application-dev.properties"),
            @PropertySource("classpath:application-prod.properties"),
            @PropertySource("classpath:application-preview.properties")
    })
    public class IotProperties {
        private String firmware_update_port;
    
    
        public String getFirmware_update_port() {
            return firmware_update_port;
        }
    
        public void setFirmware_update_port(String firmware_update_port) {
            this.firmware_update_port = firmware_update_port;
        }
    
    }
    

    你也可以参考这个
    https://blog.csdn.net/sz85850597/article/details/79133206

    相关文章

      网友评论

        本文标题:7-springboot多环境配置+如何获取配置文件的值

        本文链接:https://www.haomeiwen.com/subject/fijawftx.html