在spring配置文件中引入外部properties配置文件 context:property-placeholder
2024-10-19 09:57:15
在spring的配置文件中,有时我们需要注入很多属性值,这些属性全都写在spring的配置文件中的话,后期管理起来会非常麻烦。所以我们可以把某一类的属性抽取到一个外部配置文件中,使用时通用spring的EL表达式引入就可以了。这样可以方便我们管理属性。
步骤:
1.引入context名称空间,并使用 <context:property-placeholder location="外部配置文件"/> 引入外部配置文件
<?xml version="1.0" encoding="UTF-8"?>
<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
"> <context:component-scan base-package="com.xj"/>
<context:property-placeholder location="classpath:jdbc.properties"/> </beans>
2.创建一个外部配置文件jdbc.properties,并设置键值对
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/userDb
jdbc.userName=root
jdbc.password=123456
3.在spring配置文件中通过spEL表达式引入外部配置文件jdbc.properties中的值
<?xml version="1.0" encoding="UTF-8"?>
<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
"> <context:component-scan base-package="com.xj"/> <context:property-placeholder location="classpath:jdbc.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> </beans>
最新文章
- iOS---观察者模式之--->;KVO
- string.Format格式化用法详解
- PowerShell添加或修改注册表开机启动项脚本
- 多台linux服务器时间同步
- Android开发环境搭建及常见问题解决方法
- PHP+jQuery 注册模块开发
- 第二章 D - Number Sequence(1.5.10)
- 区间k大数查询
- window下手动搭建 PHP+Nginx+Mysql(转)
- Android-IM架构设计
- 配置zabbix agent向多个server发送数据
- Power shell 重启IIS
- Django中用户权限模块
- hadoop2.x HDFS HA linux环境搭建
- JQuery 的遍历方法 $.each
- iptables的增删改查
- Luogu P2286 [HNOI2004]宠物收养场
- OpenSips使用说明
- 递归获取包下的class文件
- rank over partition by