0
点赞
收藏
分享

微信扫一扫

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


前言

​​博主github​​

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

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

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、我们可以选中本地

[外链图片转存失败(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());
}
}

SpringBoot添加Profile文件夹同时作为Resource目录_取值_02


举报

相关推荐

0 条评论