005-Spring Boot配置分析-配置文件application、EnvironmentPostProcessor、Profiles
一、配置文件application
springboot配置文件,默认配置文件application.propertie或者application.yml,可同时存在。
基础使用
application.propertie增加配置:local.ip=192.168.1.1
application.yml增加配置【使用缩进】:
jdbc:
name: lhx
默认位置:classpath、classpath:/config、file:/、file:config下
注意:application.properties==application-default.properties
1.1、读取方式
方式一、Environment方式读取
context.getEnvironment().getProperty("local.ip","默认值")
@SpringBootApplication
public class App {
@Bean
public Runnable createRunnable() {
return () -> {
System.out.println("spring boot is running");
};
} public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
context.getBean(Runnable.class).run();
System.out.println(context.getEnvironment().getProperty("local.ip"));
context.close();
}
}
其实相当于自定义注入Environment ,然后使用env.getProperty("local.ip")即可
@Component
public class UserConfig { @Autowired
private Environment env; public void show() {
System.out.println("local.ip=" + env.getProperty("local.ip"));
}
}
另一种设置默认值方式
HashMap<String, Object> defaultProperties = new HashMap<>();
defaultProperties.put("server.host", "127.0.0.1");
application.setDefaultProperties(defaultProperties);
方式二、使用@Value注解
@Value("${local.port}")
private String localPort;
默认必须有配置项,如果没有配置项可以增加默认值
@Value("${tomcat.port:9090}")
private String tomcatPort2;
这种的:@Value("tomcat.port") 也可以使用。具体没查
方式三、使用@ConfigurationProperties(prefix="ds")
在application配置文件中增加ds.url=jdbc:mysql://spring
增加读取类
@Component
@ConfigurationProperties(prefix="ds")
public class DataSourceProperties {
private String url;
public void show() {
System.out.println("url"+url);
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
} }
其他处使用即可。
注意:@ConfigurationProperties(prefix="ds")也支持指定具体路径文件配置@ConfigurationProperties(prefix="ds",localtions="classpath:/ds.properties")
注入数组、集合,在配置文件中增加
ds.host[]=192.168.1.1
ds.host[]=192.168.1.2
ds.host[]=192.168.1.3
代码中
private List<String> hosts = new ArrayList<>();
即可,注意增加getter,setter,
注:支持配置引用配置
name=springboot
aap.name=this is ${name}
1.2、指定具体名称配置
如不使用application.properties,改为app.proerties.
方式一、启动参数中修改:
--spring.config.name=app
或者 有路径的 --spring.config.location=classpath:cong/app.propertis
或者 多个用逗号隔开
或者 file目录 --spring.config.location=classpath:cong/app.propertis,file:E:/app.properties


方式二、文件注解@PropertySource
增加jdbc.properties配置文件
增加Config配置类
@Configuration
@PropertySource("classpath:jdbc.properties")
public class FileConfig { }
然后使用即可 PropertySource 可以列多个
或者多个可以使用 @PropertySources({@PropertySource("classpath:jdbc.properties")})
@PropertySource: 用于引入外部属性配置,和Environment 配合一起使用。其中ignoreResourceNotFound 表示没有找到文件是否会报错,默认为false,就是会报错,一般开发情况应该使用默认值,设置为true相当于生吞异常,增加排查问题的复杂性.
引入PropertySource,注入Environment,然后就能用environment 获取配置文件中的value值。
二、EnvironmentPostProcessor配置文件扩展
需要注册到META-INF/spring.factories文件
1.增加此文件,并增加内容
org.springframework.boot.env.EnvironmentPostProcessor=com.lhx.spring.springboot_config.MyEnvironmentPostProcessor
2.增加实现类文件MyEnvironmentPostProcessor
三、Profiles
增加两个配置文件

方式一、程序读取
在application-dev.properties中添加
jdbc.url=jdbc:mysql://127.0.0.1/spring_dev
在application-test.properties中添加
jdbc.url=jdbc:mysql://127.0.0.1/spring_test
程序使用
SpringApplication app = new SpringApplication(App3.class);
app.setAdditionalProfiles("test");//test 读取application-test.properties
ConfigurableApplicationContext context = app.run(args);
context.getBean(Runnable.class).run();
System.out.println(context.getEnvironment().getProperty("jdbc.url"));
context.close();
注:可在setAdditionalProfiles配置多个,会被覆盖
方式二、参数配置
启动参数增加,多个使用逗号分割,配置多个 多个同时生效
--spring.profiles.active=test
使用
执行java -jar xxx.jar,可以观察到服务端口被设置为8001,也就是默认的开发环境(dev)
执行java -jar xxx.jar --spring.profiles.active=test,可以观察到服务端口被设置为8002,也就是测试环境的配置(test)
执行java -jar xxx.jar --spring.profiles.active=prod,可以观察到服务端口被设置为8003,也就是生产环境的配置(prod)
总结多环境的配置思路:
application.properties中配置通用内容,并设置spring.profiles.active=dev,以开发环境为默认配置
application-{profile}.properties中配置各个环境不同的内容
通过命令行方式去激活不同环境的配置
方式三、@Profile注解
@SpringBootConfiguration
public class MyConfig {
@Bean
public Runnable createRunnable() {
System.out.println("--------1--------");
return ()->{};
} @Bean
@Profile("dev")
public Runnable createRunnable2() {
System.out.println("--------2--------");
return ()->{};
} @Bean
@Profile("test")
public Runnable createRunnable3() {
System.out.println("--------3--------");
return ()->{};
}
}
启动对应环境时候生效
005-Spring Boot配置分析-配置文件application、EnvironmentPostProcessor、Profiles的更多相关文章
- Spring boot 梳理 - 全局配置文件application.properties或者是application.yml,在resources目录下或者类路径下的/config下,一般我们放到resources下。
全局配置文件application.properties或者是application.yml,在resources目录下或者类路径下的/config下,一般我们放到resources下.
- Spring Boot中注入配置文件application.properties中的list 对象参数
例如要注入下列参数: dyn.spring.datasources[0].name=branchtadyn.spring.datasources[0].driverClassName=oracle.j ...
- Spring Boot配置,读取配置文件
Spring Boot配置,读取配置文件 一.配置Spring Boot 1.1 服务器配置 1.2 使用其他Web服务器 1.3 配置启动信息 1.4 配置浏览器显示ico 1.5 Yaml语法 1 ...
- Spring Boot 启动(四) EnvironmentPostProcessor
Spring Boot 启动(四) EnvironmentPostProcessor Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698. ...
- Spring boot配置MongoDB以及Morphia踩坑记录
pom 因为项目中采用Morphia(MongoDB的ODM框架,对象-文档映射(object-document mapper)),因此需要在pom文件中引入相应依赖: <dependency& ...
- Redis篇之操作、lettuce客户端、Spring集成以及Spring Boot配置
Redis篇之操作.lettuce客户端.Spring集成以及Spring Boot配置 目录 一.Redis简介 1.1 数据结构的操作 1.2 重要概念分析 二.Redis客户端 2.1 简介 2 ...
- Spring Boot -- 配置切换指南
一般在一个项目中,总是会有好多个环境.比如: 开发环境 -> 测试环境 -> 预发布环境 -> 生产环境 每个环境上的配置文件总是不一样的,甚至开发环境中每个开发者的环境可能也会有一 ...
- Spring Boot 配置优先级顺序
一般在一个项目中,总是会有好多个环境.比如: 开发环境 -> 测试环境 -> 预发布环境 -> 生产环境 每个环境上的配置文件总是不一样的,甚至开发环境中每个开发者的环境可能也会有一 ...
- 笔记:Spring Boot 配置详解
Spring Boot 针对常用的开发场景提供了一系列自动化配置来减少原本复杂而又几乎很少改动的模板配置内容,但是,我们还是需要了解如何在Spring Boot中修改这些自动化的配置,以应对一些特殊场 ...
随机推荐
- Manacher算法详解
问题 什么是回文串,如果一个字符串正着度读和反着读是一样的,这个字符串就被称为回文串. such as noon level aaa bbb 既然有了回文,那就要有关于回文的问题,于是就有了-- 最长 ...
- VScode中python环境配置
vscode中python环境配置 想要在vscode中运行python代码必须要告诉vscode使用哪个解释器才行 方法1. 打开命令面板(Ctrl+Shift+P)输入Python: Select ...
- 【LOJ565】【LibreOJ Round #10】mathematican 的二进制 DP 分治FFT
题目大意 有一个无限长的二进制串,初始时它的每一位都为 \(0\).现在有 \(m\) 个操作,其中第 \(i\) 个操作是将这个二进制串的数值加上 \(2^{a_i}\).我们称每次操作的代价是这次 ...
- GOOGLE RANKBRAIN 完整指南
[译]GOOGLE RANKBRAIN 完整指南 ( 2018 最新版 ) 2018.01.29 来源 http://www.zhidaow.com/post/google-rankbrain ...
- [基础]Android 应用的启动
Android 应用的启动模式分为两种,一种是通过启动器(Launcher)启动,另一种是通过Intent消息启动. 如果在通过Intent 消息启动前,希望判断欲启动的应用是否已经安装, 目前有两种 ...
- P1020 导弹拦截 (贪心+最长不降子序列)
题目描述 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某天,雷达捕捉到敌国的导弹 ...
- Linux 配置vim编辑器
最终效果 步骤1.下载NERDTree插件安装包(vim目录插件) https://www.vim.org/scripts/script.php?script_id=1658 步骤2.在家目录创建 . ...
- Mybatis的原理与JVM内存结构(面试题)
Mybatis的原理 1.Mapper 接口在初始SQL SessionFactory注册的 2.Mapper 接口注册在名为MapperRegistry类的 HasMap中 key=Mapper c ...
- Pandas系列(二)- DataFrame数据框
一.初识DataFrame dataFrame 是一个带有索引的二维数据结构,每列可以有自己的名字,并且可以有不同的数据类型.你可以把它想象成一个 excel 表格或者数据库中的一张表DataFram ...
- Hadoop记录-hadoop jmx配置
1.hadoop-env.sh添加export HADOOP_JMX_OPTS="-Dcom.sun.management.jmxremote.authenticate=false -Dco ...