提交 d566de48 编写于 作者: Q qinxiaodong@pannk.com

profiles

上级 06102579
......@@ -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);
}
}
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);
}
}
test:
data: development
\ No newline at end of file
test:
data: product
\ No newline at end of file
test:
data: test
\ No newline at end of file
......@@ -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:
......
一、内容容器
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.
先完成此消息的编辑!
想要评论请 注册