Spring中配置 读取多个Properties 文件

一个系统中会有多个properties 配置文件

1 数据库的配置文件(db.properties)

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/four
user=ws
password=1

2 redis 的配置文件 (redis.properties)

redis.maxIdle=300 
redis.minIdle=100
redis.maxWaitMillis=3000
redis.testOnBorrow=true
redis.maxTotal=500
redis.host=127.0.0.1
redis.port=6379
redis.password=ws

在 applicationContext.xml 中配置 如下即可

<!-- 将多个配置文件位置放到列表中 -->
<bean id="propertyResources" class="java.util.ArrayList">
		<constructor-arg>
			<list>
				<!-- 这里支持多种寻址方式:classpath和file  这里用的classpath-->
				<value>classpath:redis.properties</value>
				<value>classpath:db.properties</value>
			</list>
		</constructor-arg>
	</bean>
 <!-- 将配置文件读取到容器中,交给Spring管理 -->
 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations" ref="propertyResources">

		</property>
	</bean>

猜你喜欢

转载自blog.csdn.net/weixin_43726822/article/details/87859854