SpringBoot添加Profile文件夹同时作为Resource目录

前言

博主github

博主个人博客http://blog.healerjean.com

SpringBoot添加Profile文件夹同时作为Resource目录_第1张图片

1、maven

1.1、激活测试目录local

      <properties>
                <profiles.active>src/profiles/localprofiles.active>
            properties>
<profiles>
        <profile>
            
            <id>localid>
            <properties>
                <profiles.active>src/profiles/localprofiles.active>
            properties>
            <activation>
                <activeByDefault>trueactiveByDefault>
            activation>
        profile>
        <profile>
            
            <id>devid>
            <properties>
                <profiles.active>src/profiles/devprofiles.active>
            properties>
            <activation>
                <property>
                    <name>devname>
                    <value>truevalue>
                property>
            activation>
        profile>
        <profile>
            
            <id>productid>
            <properties>
                <profiles.active>src/profiles/productprofiles.active>
            properties>
            <activation>
                <property>
                    <name>productname>
                    <value>truevalue>
                property>
            activation>
        profile>
    profiles>



1.2、添加profiles作为resource目录

<build>
    
    <resources>
        <resource>
            <directory>src/main/resourcesdirectory>
        resource>
        <resource>
            <directory>${profiles.active}directory>
        resource>
        <resource>
            <directory>src/main/javadirectory>
            <includes>
                <include>**/*.propertiesinclude>
                <include>**/*.xmlinclude>
            includes>
            <filtering>falsefiltering>
        resource>
    resources>

2、我们可以选中本地

[外链图片转存失败(img-Q4VvOTj9-1566553204168)(https://raw.githubusercontent.com/HealerJean/HealerJean.github.io/master/blogImages/1559295712114.png)]

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());
        }
    }

ContactAuthor

你可能感兴趣的:(Maven,Maven)