Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
老丢丢
SpringBoot2Demo
提交
d566de48
S
SpringBoot2Demo
项目概览
老丢丢
/
SpringBoot2Demo
通知
3
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
SpringBoot2Demo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
d566de48
编写于
11月 02, 2020
作者:
Q
qinxiaodong@pannk.com
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
profiles
上级
06102579
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
42 addition
and
5 deletion
+42
-5
common-config/src/main/java/com/pannk/demo/commonconfig/CommonConfigApplication.java
.../com/pannk/demo/commonconfig/CommonConfigApplication.java
+5
-3
common-config/src/main/java/com/pannk/demo/commonconfig/controller/IndexController.java
...m/pannk/demo/commonconfig/controller/IndexController.java
+6
-2
common-config/src/main/resources/application-dev.yml
common-config/src/main/resources/application-dev.yml
+2
-0
common-config/src/main/resources/application-prod.yml
common-config/src/main/resources/application-prod.yml
+2
-0
common-config/src/main/resources/application-test.yml
common-config/src/main/resources/application-test.yml
+2
-0
common-config/src/main/resources/application.yml
common-config/src/main/resources/application.yml
+6
-0
common-config/src/main/resources/note.txt
common-config/src/main/resources/note.txt
+19
-0
未找到文件。
common-config/src/main/java/com/pannk/demo/commonconfig/CommonConfigApplication.java
浏览文件 @
d566de48
...
...
@@ -25,6 +25,8 @@ package com.pannk.demo.commonconfig;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.context.ConfigurableApplicationContext
;
import
org.springframework.core.env.ConfigurableEnvironment
;
/**
* @author:wolf
...
...
@@ -34,10 +36,10 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
public
class
CommonConfigApplication
{
public
static
void
main
(
String
[]
args
)
{
//
SpringApplication springApplication = new SpringApplication(CommonConfigApplication.class);
SpringApplication
springApplication
=
new
SpringApplication
(
CommonConfigApplication
.
class
);
// //设置Banner模式为关闭,即不显示Banner内容
//// springApplication.setBannerMode(Banner.Mode.OFF);
// springApplication.run(args
);
SpringApplication
.
run
(
CommonConfigApplication
.
class
,
args
);
springApplication
.
setAdditionalProfiles
(
"dev"
);
//
SpringApplication.run(CommonConfigApplication.class, args);
}
}
common-config/src/main/java/com/pannk/demo/commonconfig/controller/IndexController.java
浏览文件 @
d566de48
package
com.pannk.demo.commonconfig.controller
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.RestController
;
...
...
@@ -10,8 +11,11 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
public
class
IndexController
{
@Value
(
"${test.data}"
)
private
String
testData
;
@GetMapping
(
"/hello/{name}"
)
public
String
hello
(
@PathVariable
String
name
){
return
String
.
format
(
"Hello,%s!
\n"
,
name
);
public
String
hello
(
@PathVariable
String
name
)
{
return
String
.
format
(
"Hello,%s!
Now we are in %s environment\n"
,
name
,
testData
);
}
}
common-config/src/main/resources/application-dev.yml
0 → 100644
浏览文件 @
d566de48
test
:
data
:
development
\ No newline at end of file
common-config/src/main/resources/application-prod.yml
0 → 100644
浏览文件 @
d566de48
test
:
data
:
product
\ No newline at end of file
common-config/src/main/resources/application-test.yml
0 → 100644
浏览文件 @
d566de48
test
:
data
:
test
\ No newline at end of file
common-config/src/main/resources/application.yml
浏览文件 @
d566de48
...
...
@@ -14,6 +14,12 @@ server:
min-spare-threads
:
100
# 最小线程数
connection-timeout
:
30000
# 超时时间
#spring:
# profiles:
# include: proddb prodmq
# active: prod
# - prodb
# - promq
#spring:
# application:
# name: commonConfig
# main:
...
...
common-config/src/main/resources/note.txt
浏览文件 @
d566de48
一、内容容器
1、SpringBoot支持Tomcat(默认)、Jetty、Undertow等Servlet容器
2、通过添加相关依赖来替换容器
二、profiles
1、什么是 Spring Profiles?
Spring Profiles 允许用户根据配置文件(dev,test,prod 等)来注册 bean。因此,当应用程序在开发中运行时,只有某些 bean 可以加载,而在 PRODUCTION中,某些其他 bean 可以加载。假设我们的要求是 Swagger 文档仅适用于 QA 环境,并且禁用所有其他文档。这可以使用配置文件来完成。Spring Boot 使得使用配置文件非常简单。
什么是Profiles?
Profile可以让 Spring 对不同的环境提供不同配置的功能,可以通过激活、指定参数等方式快速切换环境
在Spring Boot中多环境配置文件名需要使用application-{profile}.properties的格式,这里的**{profile}**对应的是你的环境标识。例如:
application-dev.properties — 这是开发环境
application-prod.properties — 这是生产环境
当前目录的 “/config”的子目录下
当前目录下
classpath根目录的“/config”包下
classpath的根目录下
2、@Component/@Configuration/@ConfigurationProperties可以通过添加@Profile来指定生效环境
2、激活指定profile
spring.profiles.active application-{profile}.properties
3、激活profile的方式
3.1、spring.profiles.active指定
3.2、命令指定 java -jar xxxx.jar --spring.profiles.active=prod
3.3、mvn spring-boot:run -Dspring-boot.run.profiles=dev
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录