Springboot 多属性文件配置

配置文件后缀有两种: .properties和.yml

要完成多属性配置需要自定义PropertySourcesPlaceholderConfigurer 这个Bean

properties配置方法

  1. /**
  2. * 这里必须是static函数
  3. * 如果不是 application.propertise 将读取不到
  4. * application.properties 是默认加载的,这里配置自己的properties就好
  5. * @return
  6. */
  7. @Bean
  8. public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
  9. PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
  10. // properties 加载方式
  11. Resource[] resources = {
  12. //new ClassPathResource("application.properties"),
  13. //ClassPathResource针对的是resource目录下的文件
  14. new ClassPathResource("test.properties")
  15. };
  16. configurer.setLocations(resources);
  17. return configurer;
  18. }

注意这个Bean 的函数必须是static的,否则会加载不到application.properties中的内容

这里不需要将application.properties也加进来,因为application.properties是默认加进来的,这里只要写其他的属性文件就好了

yml配置方法

  1. @Bean
  2. public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
  3. PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
  4. // yml 加载方式
  5. YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
  6. //这里可以传入多个自定义属性文件
  7. yaml.setResources(new ClassPathResource("test.yml"));
  8. configurer.setProperties(yaml.getObject());
  9. configurer.setLocations(resources);
  10. return configurer;
  11. }

yml 和properties的配置的注意点是一样的,只是需要YamlPropertiesFactoryBean来加载

2. 如果引用到属性文件中的值

test.propertise

  1. grady.username=jiang
  2. grady.password=1234567

TestConfig.java

  1. @Configuration
  2. // 这里不需要了
  3. //@PropertySource("classpath:test.properties")
  4. public class TestConfig {
  5. @Value("${grady.username}")
  6. private String username;
  7. @Value("${grady.password}")
  8. private String password;
  9. public String getUsername() {
  10. return username;
  11. }
  12. public String getPassword() {
  13. return password;
  14. }
  15. }

这里用@Configuration或@Component都可以获得到值,

注意:这里不需要使用@PropertySource了,直接用就可以了

3. 在Controller中使用(其他地方也可,这里是举例)

  1. public class UserController {
  2. @Autowired
  3. private TestConfig testConfig;
  4. @Autowired
  5. private SystemConfig systemConfig;
  6. @PostMapping("/hello")
  7. public String Hello() {
  8. String datasourcePassword = systemConfig.getDatasourcePassword();
  9. return "Hello World" + testConfig.getUsername() + " " + testConfig.getPassword()
  10. + " datasourcePassword= " + datasourcePassword;
  11. }

postMan中的结果

  1. Hello Worldjiang 1234567 datasourcePassword= Root123#

4. 更简洁的写法

  1. @Configuration
  2. public class SystemConfig {
  3. private static List<Resource> resourceList = new ArrayList<>();
  4. static {
  5. resourceList.add(new ClassPathResource("test.properties"));
  6. resourceList.add(new ClassPathResource("jdbc.properties"));
  7. }
  8. @Value("${spring.datasource.password}")
  9. private String datasourcePassword;
  10. /**
  11. * 这里必须是static函数
  12. * 如果不是 application.propertise 将读取不到
  13. * application.properties 是默认加载的,这里配置自己的properties就好
  14. * @return
  15. */
  16. @Bean
  17. public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
  18. PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
  19. // properties 加载方式
  20. configurer.setLocations(resourceList.stream().toArray(Resource[]::new));
  21. return configurer;
  22. }
  23. public String getDatasourcePassword() {
  24. return datasourcePassword;
  25. }
  26. }

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. bat-使用bat安装jdk和配置环境变量

    文件路径 @echo off Setlocal enabledelayedexpansion @REM vscode中自动开启延迟环境变量扩展, %~d0 cd %~dp0 @REM dir echo ...

  2. 说什么也要脱单——Python WEB开发:用Tornado框架制作简易【表白墙】网站

    先来哔哔两句:(https://jq.qq.com/?_wv=1027&k=QgGWqAVF) 今天我们要用Python做Web开发,做一个简单的[表白墙]网站.众所周知表白墙的功能普遍更多的 ...

  3. halcon变量窗口的图像变量不显示,重启软件和电脑都没用

    有幸遇到halcon变量窗口的图像变量不显示,重启软件和电脑都没用这个沙雕问题,也是找了蛮久才发现解决办法特意记录一下. 这是正常情况下的窗口(左边)和图像变量不显示的窗口(右边): 解决方法: 鼠标 ...

  4. Golang仿云盘项目-2.2 保留文件元信息

    本文来自博客园,作者:Jayvee,转载请注明原文链接:https://www.cnblogs.com/cenjw/p/16459817.html 目录结构 E:\goproj\FileStorage ...

  5. for增强

    package study5ran2yl.study; public class ForDemo02 { public static void main(String[] args) { int[] ...

  6. MIT 6.824 Lab2D Raft之日志压缩

    书接上文Raft Part C | MIT 6.824 Lab2C Persistence. 实验准备 实验代码:git://g.csail.mit.edu/6.824-golabs-2021/src ...

  7. expect交互学习笔记

    expect主要应用于自动化交互式操作的场景;比如服务器过多,密码也不尽相同的情况下,需要便捷的登陆服务器,而无需输入密码的情况,亦或者便捷登陆mysql/ftp等需要交互的场景:也可以在服务器之间通 ...

  8. 2022-7-18 第五组 pan 面向对象

    面向过程 向过程就是:面向过程,其实就是面向着具体的每一个步骤和过程,把每一个步骤和过程完成,然后由这些功能方法相互调用,完成需求. 面向对象 什么是面向对象: 面向对象思想就是不断的创建对象,使用对 ...

  9. MPI简谈

    MPI简谈 MPI是分布式内存系统,区别于OpenMP和Pthreads的共享内存系统.MPI是一种基于消息传递的并行编程技术,是如今最为广泛的并行程序开发方法. MPI前世今生 MPI(Messag ...

  10. YII场景

    YII在模型中定义场景后 public function scenarios(){//场景 return [ 'sco1'=>['aid','uphone'], 'sco2'=>['aid ...