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 ...
随机推荐
- 深入解析kubernetes中的选举机制
Overview 在 Kubernetes的 kube-controller-manager , kube-scheduler, 以及使用 Operator 的底层实现 controller-rumt ...
- 链表设计与Java实现,手写LinkedList这也太清楚了吧!!!
链表设计与实现 在谈链表之前,我们先谈谈我们平常编程会遇到的很常见的一个问题.如果在编程的时候,某个变量在后续编程中仍需使用,我们可以用一个局部变量来保存该值,除此之外一个更加常用的方法就是使用容器了 ...
- 《深入理解java虚拟机》读书笔记-第二章Java内存区域和内存溢出异常
java1.7和java8的jvm存在差异,本文先按照<深入理解java虚拟机>的讲解内容总结,并将java8的改变作为附录放在文末 一丶运行时数据区域 图:java虚拟机运行时数据区 ...
- antd vue 折叠面板 v-for 循环点击无效
问题描述 实现一个折叠面板点击展开,但是必须点击两次才能展开,第一次无效 <a-collapse-panel v-for="(item, index) in dataMap" ...
- PaddleOCR系列(二)--hubserving & pdserving & hub install
一.各种部署方式特点及注意事项 简称 hubserving=PaddleHub Serving pdserving=PaddleHub Serving hub install =指通过paddlehu ...
- Windows 进程的创建和终止
创建一个进程 总述 如图,创建一个进程主要分为两部分,用户态部分和内核部分. 既然我们想看看一个进程是怎么被创建的,那我们就用 WinDbg 来看看从用户态到内核态都调用了什么: 第一步:我们先看看 ...
- Scala的基础用法 和 Java相对应学习(二)变量、循环、语法
一.配置相关环境 1.增加项目 在idea里面创建新的maven项目 2. 在pom文件中增加依赖 <?xml version="1.0" encoding="UT ...
- Hadoop学习 Hadoop-HA 解释和概念介绍
一.Hadoop-HA 1.1 Hadoop1.x带来的问题 1.单点故障 a. 每个群集只有一个NameNode,NameNode存在单点故障(SPOF). b. 如果该计算机或进程不可用,则整 ...
- cookie和seesion的区别和联系
今天来聊聊cookie和session的区别和联系.首先先确定一个各自的定义吧: cookies: 网站用于鉴别用户身份和追踪用户登录状态. 存在于浏览器端的一小段文本数据 session: 中文称之 ...
- NOI / 2.3基本算法之递归变递推-6262:流感传染
OpenJudge - 6262:流感传染http://noi.openjudge.cn/ch0203/6262/ 6262:流感传染 总时间限制: 1000ms 内存限制: 65536k ...