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文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
IntelliJ Idea SpringBoot 数据库增删改查实例详解
SpringBoot 是 SpringMVC 的升级,对于编码、配置、部署和监控,更加简单。这篇文章主要介绍了IntelliJ Idea SpringBoot 数据库增删改查实例,需要的朋友可以参考下2018-02-02
详解在IDEA中使用MyBatis Generator逆向工程生成代码
这篇文章主要介绍了详解在IDEA中使用MyBatis Generator逆向工程生成代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2018-06-06
SpringBoot整合MyBatis-Plus乐观锁不生效的问题及解决方法
这篇文章主要介绍了SpringBoot整合MyBatis-Plus乐观锁不生效的问题解决方案,通过实例代码介绍了SpringBoot各个层次的操作,需要的朋友可以参考下2022-04-04


最新评论