spring中PropertyPlaceholderConfigurer的使用,引入外部资源文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cijiancao/article/details/77448948
PropertyPlaceholderConfigurer的使用
在开发web端应用时,我们一般都是把数据库那部分资源写在一个***.properties文件中,然后在application中通过${}获取相应的值。

1.db.properties
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/study?characterEncoding=UTF-8&useUnicode=true
db.username=root
db.password=root
2.applicationContext.xml
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
default-lazy-init="false">

<bean id="placeholderConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db.properties</value>
</list>
</property>
</bean>

  <bean id="dataSource"  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
            <value>${db.driver}</value>
</property>
  <property name="url">
             <value>${db.url}</value>
  </property>
        <property name="username">
             <value>${db.username}</value>
        </property>
        <property name="password">
             <value>${db.password}</value>
        </property>
    </bean>

展开阅读全文

没有更多推荐了,返回首页