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多属性文件配置的更多相关文章

  1. SpringBoot多重属性文件配置方案笔记

    SpringBoot多重属性文件配置方案笔记 需要重写PropertyPlaceholderConfigurer 同时要忽略DataSourceAutoConfiguration @SpringBoo ...

  2. 初识spring boot maven管理--属性文件配置

    在使用springboot的时候可以使用属性文件配置对属性值进行动态配置,官方文档原文如下: Spring Boot uses a very particular PropertySource ord ...

  3. 使用外部属性文件配置Bean以及Bean的生命周期方法

    1.使用外部属性文件配置Bean 在配置文件里配置 Bean 时, 有时需要在 Bean 的配置里混入系统部署的细节信息(例如: 文件路径, 数据源配置信息等). 而这些部署细节实际上需要和 Bean ...

  4. Spring 使用外部属性文件配置

    1.Spring提供了一个PropertyPlaceholderConfigurer的BeanFactory后置处理器,这个处理器允许用户将Bean的配置的部分内容 移到属性文件中.可以在Bean配置 ...

  5. Spring Boot属性文件配置文档(全部)

    This sample file is meant as a guide only. Do not copy/paste the entire content into your applicatio ...

  6. VS2010默认属性文件配置

    问题: 在VS2010中,同一个解决方案下有多个项目,都需要使用某一个库. 如果项目比较多,或者编译链接环境属性变动频繁,分别对项目进行配置就很麻烦. 解决: 在VS的配置文件中统一配置属性: 我的配 ...

  7. SpringBoot多profile文件配置

    1.多Profile文件 我们在主配置文件编写的时候,文件名可以是 application-{profile}.properties/yml默认使用application.properties的配置: ...

  8. spring常用的连接池属性文件配置

    (一) DBCP 引入jar文件 commons-dbcp-1.2.1.jar commons-pool-1.3.jar二个包. spring配置文件 <bean id="dataSo ...

  9. 使用SpringBoot的yml文件配置时踩的一个坑

    问题描述:使用SpringBoot整合redis进行yml配置的时候,启动工程报错,提示加载application.yml配置文件失败: ::27.430 [main] ERROR org.sprin ...

随机推荐

  1. python新建一个目录

    源码部分 import os # 创建目录 def mkdir(path): isExists = os.path.exists(path) if not isExists: os.makedirs( ...

  2. Vue 安装 vue的基本使用 vue的初步使用步骤

    1. 资源: https://cn.vuejs.org/v2/guide/#%E8%B5%B7%E6%AD%A5 进入官网学习 2. 点击安装,要把vue下载到本地文件的根目录中,不要选择压缩版的,这 ...

  3. Docker 与 K8S学习笔记(二十五)—— Pod的各种调度策略(上)

    上一篇,我们学习了各种工作负载的使用,工作负载它会自动帮我们完成Pod的调度和部署,但有时我们需要自己定义Pod的调度策略,这个时候该怎么办呢?今天我们就来看一下如何定义Pod调度策略. 一.Node ...

  4. 小样本利器2.文本对抗+半监督 FGSM & VAT & FGM代码实现

    小样本利器2.文本对抗+半监督 FGSM & VAT & FGM代码实现 上一章我们聊了聊通过一致性正则的半监督方案,使用大量的未标注样本来提升小样本模型的泛化能力.这一章我们结合FG ...

  5. 抓到Dubbo异步调用的小BUG,再送你一个贡献开源代码的机会

    hello,大家好呀,我是小楼. 最近一个技术群有同学at我,问我是否熟悉Dubbo,这我熟啊~ 他说遇到了一个Dubbo异步调用的问题,怀疑是个BUG,提到BUG我可就不困了,说不定可以水,哦不.. ...

  6. Java数组和Arrays 类

    1.创建数组的三种方式: ①动态初始化:数组的初始化和数组元素的赋值操作分开进行 dataType[ ] arrayRefVar = new dataType [ arraySize ] ; Emp ...

  7. XML方式配置切面

    1. 概述  一个切面中需要包含什么,才能够作用到连接点?切面中是包含通知的,通知作用到连接点需要有切入点表达式. 除了使用AspectJ注解声明切面,Spring也支持在bean配置文件中声明切面. ...

  8. labview从入门到出家4--用事件结构实现运算功能

    使用事件结构可以快速定位响应界面的操作事件,如按下,拖动,双击的事件.基本上我们所要实现的所有功能,都可以通过条件结构+事件结构去实现,比如后面进阶篇将会讲到的状态机就是通过条件结构和事件结构组成的. ...

  9. ElementUI嵌套页面及关联增删查改实现

    @ 目录 前言 一.ElementUI如何在原有页面添加另外一个页面并实现关联增删查改? 二.实现步骤 1.ElementUI代码 2.思路:很简单 1.1 首先通过el-row.el-col.el- ...

  10. CF1702B Polycarp Writes a Srting from Memory 题解

    给定一个字符串,每天可以记忆三个字符,求书写出整个字符串的天数. 每次确定要记忆的三个字母,并向后寻找,若有非三个字母其中一个,则重新开启一天记忆三个字母. #include<cstdio> ...