首先,遇到一个问题,spring配置中加载properties文件配置如下:

<context:property-placeholder ignore-unresolvable="true" location="classpath:config/appconfig.properties"/>

结果运行时发现没加载到properties文件,需要把该配置改为如下:

<context:property-placeholder ignore-unresolvable="true" location="classpath*:config/appconfig.properties"/>

这样才OK了。由此研究了下spring配置中加载properties文件方法,大致有如下两种:

util:properties和context:property-placeholder标签都可以用来获取外部配置文件中的内容 
1、util:properties 
它是以声明bean方式来使用,创建了一个bean,下面使用的时候通过SpEL表达式#{}获取bean的属性。

<util:properties id="config" location="classpath:appconfig.properties" />
<!-- 配置连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"><property name="driverClassName" value="#{jdbc.driver}"/><property name="url" value="#{jdbc.url}"/><property name="username" value="#{jdbc.username}"/><property name="password" value="#{jdbc.password}"/>
</bean>

需要注意,这种方式需要在spring配置文件头部声明

xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"

2、context:property-placeholder 
它是将配置文件加载至spring上下文中,然后通过${}取得值,常用于bean的属性上

<context:property-placeholder location="classpath:appconfig.properties"/>
<!-- 配置连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"><property name="driverClassName" value="${jdbc.driver}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/>
</bean>

Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个 org.springframework.beans.config.PropertyPlaceholderConfigurer的Bean就会停止对剩余PropertyPlaceholderConfigurer的扫描, 换句话说,即Spring容器仅允许最多定义一个PropertyPlaceholderConfigurer 或 <content:property-placeholder>其余的会被Spring忽略。

由于Spring容器只能有一个PropertyPlaceholderConfigurer,如果有多个属性文件,这时就看谁先谁后了,先的保留 ,后的忽略。

还有一种情况,是Spring 自动注入 properties文件中的配置:要自动注入properties文件中的配置,需要在Spring配置文件中添加 org.springframework.beans.factory.config.PropertiesFactoryBean和org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer的实例配置

<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"><property name="locations"><list><value> classpath*:application.properties</value></list></property>
</bean>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"><property name="properties" ref="configProperties" />
</bean>

在这个配置文件中我们配置了注解扫描,和configProperties实例和propertyConfigurer实例,这样我们就可以在java类中自动注入配置了

@Component
public class Test{@Value("#{configProperties['userName']}")private String userName;public String getUserName(){return userName;}}

自动注入需要使用 @Value 这个注解,这个注解的格式 #{configProperties['userName']}其中configProperties是我们在配置文件中配置的bean的 id, userName 是在配置文件中配置的项 。

参考文章:https://blog.csdn.net/n447194252/article/details/77498916

https://blog.csdn.net/forlovehuan/article/details/80319107

spring配置中加载properties文件方法相关推荐

  1. spring配置数据源(加载properties文件)

    1.在spring中引入properties配置文件需要引入context的命名空间和真实地址 2.然后加载文件 需要注意的是这是采用的是set注入方式,所以name属性值必须是连接池set方法名去掉 ...

  2. Java中加载properties文件的6种方法

    .使用java.util.Properties类的load()方法 示例: InputStream in = lnew BufferedInputStream(new FileInputStream( ...

  3. [转载]spring使用PropertyPlaceholderConfigurer加载properties文件处理中文乱码

    在spring中我们常常使用.properties对一些属性进行一个提前配置,spring在读取∗.properties文件时,默认使用的是asci码,这时我们需要对其编码进行转换.当然方法有很多种, ...

  4. 自动注入、加载 properties 文件、scope 属性、单例设计模式

    一.自动注入 在 Spring 配置文件中对象名和 ref="id"id 名相同使用自动注入,可以不配置<property/> 两种配置办法 2.1 在<bean ...

  5. Spring加载properties文件的两种方式

    2019独角兽企业重金招聘Python工程师标准>>> 在项目中如果有些参数经常需要修改,或者后期可能需要修改,那我们最好把这些参数放到properties文件中,源代码中读取pro ...

  6. springboot 读取配置文件_使用 @ConfigurationProperties 在 Spring Boot 中加载配置

    本文地址: 使用 @ConfigurationProperties 在 Spring Boot 中加载配置 使用 Spring Boot 加载配置文件的配置非常便利,我们只需要使用一些注解配置一下就能 ...

  7. html 调用c#dll中的控件,C#实现反射调用动态加载的DLL文件中的方法和在窗体中加载DLL文件的用户控件...

    反射的作用: 1. 可以使用反射动态地创建类型的实例,将类型绑定到现有对象,或从现有对象中获取类型 2. 应用程序需要在运行时从某个特定的程序集中载入一个特定的类型,以便实现某个任务时可以用到反射. ...

  8. 在Spring Boot中加载初始化数据

    文章目录 依赖条件 data.sql文件 schema.sql 文件 @sql注解 @SqlConfig 注解 在Spring Boot中加载初始化数据 在Spring Boot中,Spring Bo ...

  9. java加载properties文件的几种方式,java高级面试笔试题

    我总结出了很多互联网公司的面试题及答案,并整理成了文档,以及各种学习的进阶学习资料,免费分享给大家. 扫描二维码或搜索下图红色VX号,加VX好友,拉你进[程序员面试学习交流群]免费领取.也欢迎各位一起 ...

最新文章

  1. 百练1724:ROADS
  2. leetcode 593. Valid Square | 593. 有效的正方形(Java)
  3. python支持向量机回归_Python中支持向量机SVM的使用方法详解
  4. 如何让梯形变成平行四边形_开放的课堂 创新的天地——平行四边形的面积教学片段与反思...
  5. PAT乙级(1024 科学计数法)
  6. acm的ubuntu (ubuntu16.04 安装指南,chrome安装,vim配置,git设置和github,装QQ)
  7. 【转】switch与if的区别
  8. 新版中日交流标准日本语多媒体版(全25CD)下载
  9. 吴恩达深度学习笔记——优化算法
  10. certificate expired
  11. 2016年360校招笔试题
  12. 人脸识别摄像头开发板和模组选型
  13. url的转换与重定向
  14. tagcanvas.min.js 文字云
  15. windows10批量修改文件后缀名
  16. java下拉框怎么做_java下拉框怎么做?
  17. ZJOI2017 仙人掌
  18. 程序员必备网站Collection~
  19. 浅析电感噪音以及解决办法
  20. 520c语言程序表白,C语言实现520表白代码 祝你表白成功!

热门文章

  1. 天翼云从业认证(4.4)异构双活云灾备实例
  2. /(^\s*)|(\s*$)/g
  3. 语义分割各种评价指标实现
  4. Linux socket网络编程实现FTP服务器
  5. 远心镜头参数之一:远心镜头景深计算
  6. java,png,jpg,多张图片合成一个pdf,压缩图片,并且保证图片不失帧。
  7. WOSADO悦瞳获近3亿人民币融资;研卤堂获数千万人民币A轮融资
  8. 专项测试实战 | 如何测试 App 流畅度(基于 FPS 和丢帧率)
  9. 整理一下虚拟化与Linux的学习经历
  10. FilterConfig接口及其使用方法详解