spring加载properties配置文件的三种方式

Aaron2705      2022-04-10     745

关键词:

一、通过 context:property-placeholder 标签实现配置文件加载

1) 用法:

1、在spring.xml配置文件中添加标签

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

2、在 spring.xml 中使用 配置文件属性:$

<!-- 基本属性 url、user、password -->
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" /

3、在java文件中使用:

@Value("${jdbc.url}")  
private  String jdbcUrl; // 注意:这里变量不能定义成static

2) 注意点:踩过的坑

Spring中的xml中使用<context:property-placeholderlocation>标签导入配置文件时,想要导入多个properties配置文件,如下:

<context:property-placeholderlocation="classpath:db.properties " />

<context:property-placeholderlocation="classpath:zxg.properties " />

结果发现不行,第二个配置文件始终读取不到,Spring容器是采用反射扫描的发现机制,通过标签的命名空间实例化实例,当Spring探测到容器中有一个org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的Bean就会停止对剩余PropertyPlaceholderConfigurer的扫描,即只能存在一个实例

 

如果有多个配置文件可以使用 “,” 分隔

<context:property-placeholderlocation="classpath:db.properties,classpath:monitor.properties" />

可以使用通配符 *

<context:property-placeholderlocation="classpath:*.properties" />

3) 属性用法

ignore-resource-not-found //如果属性文件找不到,是否忽略,默认false,即不忽略,找不到文件并不会抛出异常。 
ignore-unresolvable //是否忽略解析不到的属性,如果不忽略,找不到将抛出异常。但它设置为true的主要原因是:

 

 

二、通过 util:properties 标签实现配置文件加载

1)  用法

 1、用法示例: 在spring.xml配置文件中添加标签

<?xml version="1.0" encoding="UTF-8"?>
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">
<!-- 加载配置文件 --> <util:properties id="jdbc" local-override="true" location="classpath:properties/jdbc.properties"/>

 

 2、在spring.xml 中使用配置文件属性:#

<!-- dataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="#{jdbc.driverClass}" />
    <property name="jdbcUrl" value="#{jdbc.jdbcUrl}" />
    <property name="user" value="#{jdbc.user}" />
    <property name="password" value="#{jdbc.password}" />
</bean>

 

 3.java文件,让Spring注入从资源文件中读取到的属性的值,,为了简便,把几种注入的方式直接写入到一个文件中进行展示:

@Component  
public class SysConf {  
  
    @Value("#{jdbc.url}")  
    private String url;  
    @Value("#{jdbc}")  
    public void setJdbcConf(Properties jdbc){  
        url= sys.getProperty("url");  
    }  
}  

 注意:这里的#{jdbc} 是与第1步的id="jdbc" 相对应的

 

 三、通过 @PropertySource 注解实现配置文件加载

使用和  context:property-placeholder 差不多

 1、用法示例:在java类文件中使用 PropertySource 注解

@PropertySource(value={"classpath:mail.properties"})
public class ReadProperties {
  @Value(value="${mail.username}")
   private String USER_NAME;
}

 


spring-boot的三种启动方式

...taticvoidmain(String[]args)/***SpringApplication会自动加载application.properties文件, 查看详情

spring-boot的三种启动方式

...taticvoidmain(String[]args)/***SpringApplication会自动加载application.properties文件, 查看详情

log4j介绍(代码片段)

log4j.properties文件的三种加载方式1.spring默认自动加载  满足以下条件时:    1).配置文件名为log4j.properties    2).在classpath根目录下(即resources根目录下)  spring会自动加载log4j.properties文件,无需显式加载.2.spring手动加载... 查看详情

springboot简述springboot项目启动数据加载内存中的三种方法

一、前言一般来说,SpringBoot工程环境配置放在properties文件中,启动的时候将工程中的properties/yaml文件的配置项加载到内存中。但这种方式改配置项的时候,需要重新编译部署,考虑到这种因素,今天介绍将配... 查看详情

springboot读取yml配置文件的三种方式(包含以及非component下)(代码片段)

...0c;service或component注解下的文件想要读取时,由于不是properties也不能直接通过properties的方式直接加载,直接读取文件流也不知道是否可行,查找部分资料后找到了解决方式,下面做下记录标准读取方式一(一... 查看详情

springboot读取配置文件的三种方法

分析SpringBoot分别提供3中方式读取项目的application.properties配置文件的内容。这个方式分别为:Environment类、@Value注解以及@ConfigurationProperties注解。你必须要知道的事情:下面提供的三种方式,都可以拿到配置文... 查看详情

spring配置加载多个properties文件

(一)首先,我们要先在spring配置文件中。定义一个专门读取properties文件的类.例:1<beanid="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">2<propertyname="locations">3< 查看详情

spring--spring配置文件详解(bean实例化的三种方式ioc(控制反转)与di(依赖注入)依赖注入详解)(代码片段)

01:Spring配置文件详解(Bean标签的基本配置(id,class)/范围配置/不同范围时的对象的创建时期/Bean生命周期配置(生命周期方法))02:Spring配置文件详解(Bean实例化的三种方式、IoC(... 查看详情

springboot入门十,获取配置文件信息

...这里介绍比较常用的三种方式 默认获取的都是application.properties文件中的信息1.application.properties配置文件内容如下:server.port=80spring.mvc.view.prefix=/jsp/spring.mvc.view.suffix=.jsp#中文需要转换成为ASCII码,否则取值的时候是乱码 查看详情

Spring boot:从文件系统加载配置文件特定的 application.properties

】Springboot:从文件系统加载配置文件特定的application.properties【英文标题】:Springboot:loadprofilespecificapplication.propertiesfromfilesystem【发布时间】:2019-02-2211:47:22【问题描述】:我正在尝试根据当前活动配置文件加载外部属性文件以... 查看详情

spring加载属性(properties)文件(代码片段)

  在开发的过程中,配置文件往往就是那些属性(properties)文件,比如使用properties文件配置数据库文件,又如database-config.properties  代码清单:database-config.propertiesjdbc.database.driver=com.mysql.cj.jdbc.Driverjdbc.database.url=jdbc:mysql://lo 查看详情

spring boot 在 application.properties 中使用 spring.profile.default 时未加载默认配置文件

】springboot在application.properties中使用spring.profile.default时未加载默认配置文件【英文标题】:springbootnotloadingdefaultprofilewhileusingspring.profile.defaultinapplication.properties【发布时间】:2020-02-1309:59:26【问题描述】:仅供参考:我发誓没有... 查看详情

在vs中添加lib库的三种方法

...件到相应目录,或者设定DLL目录的位置,具体方法为:"Properties" -> "Configuration Properties" -> "Debugging",在"Working Directory"设置dll的路径就可以了2、无论是设置DLL目录,或者是Lib目录,亦或是头文件的目录... 查看详情

spring中创建bean对象的三种方式以及作用范围

 时间:2020/02/02 一.在spring的xml配置文件中创建bean对象的三种方式:1.使用默认构造函数创建。在spring的配置文件中使用bean标签,配以id和class属性之后,且没有其他属性和标签时采用的就是默认构造函数创建bean对象,此... 查看详情

springboot的三种启动方式(代码片段)

springBoot是什么  springboot是一个框架,它依赖于spring,在过去我们使用spring项目的时候,需要大量各种繁琐的配置文件才能让spring跑起来,但自从有了springboot之后,它简化了使用方式,做到了无xml文件的... 查看详情

spring加载属性(properties)文件

   一、注解方式加载jdbc.driver=org.mariadb.jdbc.Driverjdbc.url=jdbc:mariadb://localhost:3306/ktjdbc.user=rootjdbc.password=12345创建配置类:packagecom.wbg.springAnnotaion.config;importorg.springframe 查看详情

分享spring中接口注入的三种方式

参考技术A  下面是Spring开发指南中所说的三种注入方式我看了但不太懂大家也看看看有没有更好的理解方式请给于指点我的Email    Type接口注入    我们常常借助接口来将调用者与实现者分离如:  publicclassClassA ... 查看详情

springboot与dubbo整合的三种方式

1.使用默认application.properties和注解的方式导入dubbo-starter,在application.properties配置属性,使用@Service注解来暴露服务,使用@Reference来引用服务。具体可参考 Dubbo整合SpringBoot,这里截取部分代码方便理解。属性在application.propert... 查看详情