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. Java基础-JVM篇

    1.1 .线程 ​ 这里所说的线程指程序执行过程中的一个线程实体.JVM 允许一个应用并发执行多个线程.Hotspot JVM 中的 Java 线程与原生操作系统线程有直接的映射关系.当线程本地存储. ...

  2. C#判断数组或集合中是否含有属性值为value的对象

    /// <summary> /// 判断list中是否有某个对象的Id_srvplan为value /// </summary> /// <param name=&quo ...

  3. 分布式机器学习:模型平均MA与弹性平均EASGD(PySpark)

    计算机科学一大定律:许多看似过时的东西可能过一段时间又会以新的形式再次回归. 1 模型平均方法(MA) 1.1 算法描述与实现 我们在博客<分布式机器学习:同步并行SGD算法的实现与复杂度分析( ...

  4. Markdown第一次学习

    # # Markdown学习 一级标题: #空格+标题名称+回车得到一级标题 ## 二级标题 一级标题方法中变成两个##号 ### 三级标题 变成三个###号,以此类推,最多到六级标题 ## 字体 h ...

  5. Kafka KRaft模式探索

    1.概述 Kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者在网站中的所有动作流数据.其核心组件包含Producer.Broker.Consumer,以及依赖的Zookeeper集群. ...

  6. .Net之延迟队列

    介绍 具有队列的特性,再给它附加一个延迟消费队列消息的功能,也就是说可以指定队列中的消息在哪个时间点被消费. 使用场景 延时队列在项目中的应用还是比较多的,尤其像电商类平台: 订单成功后,在30分钟内 ...

  7. Graph Neural Networks:谱域图卷积

    以下学习内容参考了:1,2, 0.首先回忆CNN,卷积神经网络的结构和特点 处理的数据特征:具有规则的空间结构(Euclidean domains),都可以采用一维或者二维的矩阵描述.(Convolu ...

  8. SpringBoot开发 - 什么是热部署和热加载?devtool的原理是什么?

    在SpringBoot开发调试中,如果我每行代码的修改都需要重启启动再调试,可能比较费时间:SpringBoot团队针对此问题提供了spring-boot-devtools(简称devtools)插件 ...

  9. Kafka启动遇到ERROR Exiting Kafka due to fatal exception (kafka.Kafka$) 解决办法 从kafka的根目录启动 bin/kafka-server-start.sh config/server.properties

    Mysql配置读写数据库 ERROR 1227 (42000): Access denied; you need (at least one of) the SUPER privilege(s) fo ...

  10. [SWPU2019]Web1-1|SQL注入

    1.打开之后界面如下: 2.查看源代码.登录注入等未发现有用信息,结果如下: 3.进行注册试试,注册时发现admin账户已被注册,随便注册一个账户并登录,结果如下: 申请发布广告页面如下: 4.发布广 ...