Spring 实战-使用<context:property-placeholder>标签导入多个properties文件

使用<context:property-placeholder>标签导入多个properties文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sf_climber/article/details/78850038

Spring中的xml中使用<context:property-placeholderlocation>标签导入配置文件时,想要导入多个properties配置文件,如下:

<context:property-placeholder  location="classpath:db.properties " />

<context:property-placeholder  location="classpath:db2.properties " />

 

结果发现不行,第二个配置文件始终读取不到,后来发现<context:property-placeholder>标签在Spring配置文件中只能存在一份

Spring容器是采用反射扫描的发现机制,通过标签的命名空间实例化实例,当Spring探测到容器中有一个org.springframework.beans.factory.config.PropertyPlaceholderCVonfigurerBean就会停止对剩余PropertyPlaceholderConfigurer的扫描,即只能存在一个实例。

<context:property-placeholder   
       location=""  
       file-encoding="" ignore-resource-not-found="" ignore-unresolvable="" properties-ref="" local-override="" system-properties-mode="" order="" />

那如果有多个配置文件怎么办呢?那就多个文件之间以“,”分隔,如下:

<context:property-placeholderlocation="classpath:db.properties,classpath:monitor.properties" />

 

值得注意的是:多个配置文件将依次加载,如果后一个文件中有和前面某一个文件中属性名是相同的,最终取的值是后加载的值。

#jdbc配置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=root

 

扫描二维码关注公众号,回复: 3725608 查看本文章
<bean id="dataSource" class="org.springframework,jdbc,datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${driverClassName}"/>
    <property name="url" value="${url}"/>
    <property name="username" value="${username}"/>
    <property name="password" value="${password}"/>
</bean>

 

猜你喜欢

转载自www.cnblogs.com/jiangtao1218/p/9852862.html