spring中在xml配置中加载properties文件的步骤
在Spring中,你可以使用PropertyPlaceholderConfigurer
来加载和解析properties文件。以下是在XML配置中加载properties文件的步骤:
假设我们有一个jdbc.properties的配置文件:
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssm753as jdbc.username=root jdbc.password=123456
首先,在XML配置文件中引入context
命名空间,如下所示:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
然后,在XML配置文件中添加PropertyPlaceholderConfigurer
bean的定义,指定要加载的properties文件路径,例如:
<context:property-placeholder location="classpath:jdbc.properties" />
最后,你可以通过${}
语法在其他bean定义中使用properties文件中的属性值,例如:
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <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中如何在xml配置中加载properties文件的文章就介绍到这了,更多相关spring加载properties文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Spring中的NamespaceHandler与BeanDefinitionParser详解
这篇文章主要介绍了Spring中的NamespaceHandler与BeanDefinitionParser详解,由Spring启动过程之obtainFreshBeanFactory() ,可以看到NamespaceHandler、BeanDefinitionParser为解析配置文件中的Element起重要作用,那么它本身是如何被加载的呢,需要的朋友可以参考下2023-12-12通过FeignClient如何获取文件流steam is close问题
这篇文章主要介绍了通过FeignClient如何获取文件流steam is close问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-06-06IntelliJ IDEA 2021.1 首个 Beta 版本发布
这篇文章主要介绍了IntelliJ IDEA 2021.1 首个 Beta 版本发布,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2021-03-03
最新评论