天天看點

SpringBoot添加Profile檔案夾同時作為Resource目錄

前言

​​部落客github​​

​​部落客個人部落格http://blog.healerjean.com​​

SpringBoot添加Profile檔案夾同時作為Resource目錄

1、maven

1.1、激活測試目錄local

<properties>
                <profiles.active>src/profiles/local</profiles.active>
            </properties>      
<profiles>
        <profile>
            <!-- 本地開發環境 -->
            <id>local</id>
            <properties>
                <profiles.active>src/profiles/local</profiles.active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <!-- 開發環境 -->
            <id>dev</id>
            <properties>
                <profiles.active>src/profiles/dev</profiles.active>
            </properties>
            <activation>
                <property>
                    <name>dev</name>
                    <value>true</value>
                </property>
            </activation>
        </profile>
        <profile>
            <!-- 生産環境 -->
            <id>product</id>
            <properties>
                <profiles.active>src/profiles/product</profiles.active>
            </properties>
            <activation>
                <property>
                    <name>product</name>
                    <value>true</value>
                </property>
            </activation>
        </profile>
    </profiles>      

1.2、添加profiles作為resource目錄

<build>
    <!-- 定義資源目錄 -->
    <resources>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
        <resource>
            <directory>${profiles.active}</directory>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
    </resources>      

2、我們可以選中本地

3、使用Properties測試

3.1、resource目錄下建立​

​resource.properties​

## properties 取值 ##
profile.name=resource.name
profile.age=resource.age      

3.2、激活的profile目錄下建立​

​profile.properties​

## properties 取值 ##
profile.name=profile.name
profile.age=profile.age      
/*
 * Copyright (C) 2018 dy_only, Inc. All Rights Reserved.
 */
package com.hlj.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertiesUtil {
  public static Properties properties = new Properties();

  public static String getProperty(String key) {
    return properties.getProperty(key) == null ? "" : properties.get(key).toString();
  }

  static {
      String profile = System.getProperty("spring.profiles.active");
        System.out.println(profile);

      String[]  props = new String[] {"profile.properties", "resource.properties" };
      for(String prop:props){
        InputStream inputStream = PropertiesUtil.class.getClassLoader().getResourceAsStream(prop);
        if (inputStream != null) {
          Properties propertiest = new Properties();
          try {
            propertiest.load(inputStream);
            properties.putAll(propertiest);

          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }
  }
}      

3.3、測試

@ApiOperation(value = "擷取properties",notes = "擷取properties",
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE,
            response = ResponseBean.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "參數", required =false,paramType = "query", dataType = "string")
    })
    @GetMapping("get")
    @ResponseBody
    public ResponseBean get(String name){
        try {
            System.out.println(PropertiesUtil.getProperty(name));
            return ResponseBean.buildSuccess(PropertiesUtil.getProperty(name));
        } catch (AppException e) {
            log.error(e.getMessage(),e);
            return ResponseBean.buildFailure(e.getCode(),e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return ResponseBean.buildFailure(e.getMessage());
        }
    }