Springboot多属性文件配置
Springboot 多属性文件配置
配置文件后缀有两种: .properties和.yml
要完成多属性配置需要自定义PropertySourcesPlaceholderConfigurer 这个Bean
properties配置方法
/**
* 这里必须是static函数
* 如果不是 application.propertise 将读取不到
* application.properties 是默认加载的,这里配置自己的properties就好
* @return
*/
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
// properties 加载方式
Resource[] resources = {
//new ClassPathResource("application.properties"),
//ClassPathResource针对的是resource目录下的文件
new ClassPathResource("test.properties")
};
configurer.setLocations(resources);
return configurer;
}
注意这个Bean 的函数必须是static的,否则会加载不到application.properties中的内容
这里不需要将application.properties也加进来,因为application.properties是默认加进来的,这里只要写其他的属性文件就好了
yml配置方法
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
// yml 加载方式
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
//这里可以传入多个自定义属性文件
yaml.setResources(new ClassPathResource("test.yml"));
configurer.setProperties(yaml.getObject());
configurer.setLocations(resources);
return configurer;
}
yml 和properties的配置的注意点是一样的,只是需要YamlPropertiesFactoryBean来加载
2. 如果引用到属性文件中的值
test.propertise
grady.username=jiang
grady.password=1234567
TestConfig.java
@Configuration
// 这里不需要了
//@PropertySource("classpath:test.properties")
public class TestConfig {
@Value("${grady.username}")
private String username;
@Value("${grady.password}")
private String password;
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
这里用@Configuration或@Component都可以获得到值,
注意:这里不需要使用@PropertySource了,直接用就可以了
3. 在Controller中使用(其他地方也可,这里是举例)
public class UserController {
@Autowired
private TestConfig testConfig;
@Autowired
private SystemConfig systemConfig;
@PostMapping("/hello")
public String Hello() {
String datasourcePassword = systemConfig.getDatasourcePassword();
return "Hello World" + testConfig.getUsername() + " " + testConfig.getPassword()
+ " datasourcePassword= " + datasourcePassword;
}
postMan中的结果
Hello Worldjiang 1234567 datasourcePassword= Root123#
4. 更简洁的写法
@Configuration
public class SystemConfig {
private static List<Resource> resourceList = new ArrayList<>();
static {
resourceList.add(new ClassPathResource("test.properties"));
resourceList.add(new ClassPathResource("jdbc.properties"));
}
@Value("${spring.datasource.password}")
private String datasourcePassword;
/**
* 这里必须是static函数
* 如果不是 application.propertise 将读取不到
* application.properties 是默认加载的,这里配置自己的properties就好
* @return
*/
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
// properties 加载方式
configurer.setLocations(resourceList.stream().toArray(Resource[]::new));
return configurer;
}
public String getDatasourcePassword() {
return datasourcePassword;
}
}
Springboot多属性文件配置的更多相关文章
- SpringBoot多重属性文件配置方案笔记
SpringBoot多重属性文件配置方案笔记 需要重写PropertyPlaceholderConfigurer 同时要忽略DataSourceAutoConfiguration @SpringBoo ...
- 初识spring boot maven管理--属性文件配置
在使用springboot的时候可以使用属性文件配置对属性值进行动态配置,官方文档原文如下: Spring Boot uses a very particular PropertySource ord ...
- 使用外部属性文件配置Bean以及Bean的生命周期方法
1.使用外部属性文件配置Bean 在配置文件里配置 Bean 时, 有时需要在 Bean 的配置里混入系统部署的细节信息(例如: 文件路径, 数据源配置信息等). 而这些部署细节实际上需要和 Bean ...
- Spring 使用外部属性文件配置
1.Spring提供了一个PropertyPlaceholderConfigurer的BeanFactory后置处理器,这个处理器允许用户将Bean的配置的部分内容 移到属性文件中.可以在Bean配置 ...
- Spring Boot属性文件配置文档(全部)
This sample file is meant as a guide only. Do not copy/paste the entire content into your applicatio ...
- VS2010默认属性文件配置
问题: 在VS2010中,同一个解决方案下有多个项目,都需要使用某一个库. 如果项目比较多,或者编译链接环境属性变动频繁,分别对项目进行配置就很麻烦. 解决: 在VS的配置文件中统一配置属性: 我的配 ...
- SpringBoot多profile文件配置
1.多Profile文件 我们在主配置文件编写的时候,文件名可以是 application-{profile}.properties/yml默认使用application.properties的配置: ...
- spring常用的连接池属性文件配置
(一) DBCP 引入jar文件 commons-dbcp-1.2.1.jar commons-pool-1.3.jar二个包. spring配置文件 <bean id="dataSo ...
- 使用SpringBoot的yml文件配置时踩的一个坑
问题描述:使用SpringBoot整合redis进行yml配置的时候,启动工程报错,提示加载application.yml配置文件失败: ::27.430 [main] ERROR org.sprin ...
随机推荐
- ms12-020漏洞
一.环境说明 kali linux windows 7 sp1 二.ms12-020漏洞利用 msf5 exploit(windows/browser/ms10_002_aurora) > se ...
- 一条update语句到底加了多少锁?带你深入理解底层原理
迎面走来了你的面试官,身穿格子衫,挺着啤酒肚,发际线严重后移的中年男子. 手拿泡着枸杞的保温杯,胳膊夹着MacBook,MacBook上还贴着公司标语:"我爱加班". 面试开始,直 ...
- Turtle绘图——python简单上手小案例
Turtle绘图 Turtle模块提供了在二维平面上移动的环境. Turtle可以实现位置.航向和各种可能的状态和动作. import turtle as tu roo = tu.Turtle() # ...
- NC20032 [HNOI2003]激光炸弹
NC20032 [HNOI2003]激光炸弹 题目 题目描述 一种新型的激光炸弹,可以摧毁一个边长为R的正方形内的所有的目标. 现在地图上有 \(n\) (\(N ≤ 10000\))个目标,用整数 ...
- typescript+webpack构建一个js库
依赖说明 入口文件 tsconfig配置 webpack配置文件 webpack入口文件配置 webpack为typescript和less文件配置各自的loader webpack的output配置 ...
- arcgis中栅格矢量计算技巧收藏
编辑 一.计算面积 ( 可以帮我们计算小班面积 )添加 AREA 字段,然后右键点击字段列,然后点击 CALCULATE VALUES; ---> 选择 ADVANCED -->把下 ...
- C++ 模板和泛型编程(掌握Vector等容器的使用)
1. 泛型 泛型在我的理解里,就是可以泛化到多种基本的数据类型,例如整数.浮点数.字符和布尔类型以及自己定义的结构体.而容器就是提供能够填充任意类型的数据的数据结构.例如vector就很类似于pyth ...
- AMS1117降压电路
AMS1117芯片为正向低压差稳压器,内部集成过热保护和限流电路,其固定输出版本电压可为1.5V.1.8V.2.5V.2.85V.3.0V.3.3V.5.0V,设计采用3.3V输出即ASM1117-3 ...
- NC20583 [SDOI2016]齿轮
题目链接 题目 题目描述 现有一个传动系统,包含了N个组合齿轮和M个链条.每一个链条连接了两个组合齿轮u和v,并提供了一个传动比x : y. 即如果只考虑这两个组合齿轮,编号为u的齿轮转动x圈,编号为 ...
- 作业一、安装Ubuntu系统
Ubuntu1804安装 一.安装环境 1.VMware Workstation 16 Pro 2.Ubuntu 18.04.6 LTS 二.部署系统 步骤1.进入VMware,点击创建新的虚拟机 步 ...