在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>

最新文章

  1. iOS---观察者模式之---&gt;KVO
  2. string.Format格式化用法详解
  3. PowerShell添加或修改注册表开机启动项脚本
  4. 多台linux服务器时间同步
  5. Android开发环境搭建及常见问题解决方法
  6. PHP+jQuery 注册模块开发
  7. 第二章 D - Number Sequence(1.5.10)
  8. 区间k大数查询
  9. window下手动搭建 PHP+Nginx+Mysql(转)
  10. Android-IM架构设计
  11. 配置zabbix agent向多个server发送数据
  12. Power shell 重启IIS
  13. Django中用户权限模块
  14. hadoop2.x HDFS HA linux环境搭建
  15. JQuery 的遍历方法 $.each
  16. iptables的增删改查
  17. Luogu P2286 [HNOI2004]宠物收养场
  18. OpenSips使用说明
  19. 递归获取包下的class文件
  20. rank over partition by

热门文章

  1. 百度地图api逆地址解析 PHP
  2. js实现倒计时函数
  3. TortoiseGit生成.PPK拓展名的密钥
  4. 简单聊聊VisualStudio的断点调试
  5. 黄衫女子,黄衫好.png
  6. [bug] Job for network.service failed because the control process exited with error code
  7. 下载最新版本Fiddler
  8. 使用UltraISO制作ubuntu安装u盘启动盘图文教程
  9. spec2006与spec2000的对比简要说明
  10. Ansible_实施处理程序