這里主要介紹PropertyPlaceholderConfigurer這個類的使用,spring中的該類主要用來讀取配置文件並將配置文件中的變量設置到上下文環境中,並進行賦值。
一、此處使用list標簽將多properties文件信息讀取到PropertyPlaceholderConfigurer類中
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <!-- 這里支持多種尋址方式:classpath和file --> <value>classpath:/opt/demo/config/demo-db.properties</value> <!-- 推薦使用file的方式引入,這樣可以將配置和代碼分離 --> <value>file:/opt/demo/config/demo-mq.properties</value> <value>file:/opt/demo/config/demo-remote.properties</value> </list> </property> </bean>
二、可以將多配置文件讀取到list中,然后再將list的引用賦值
<!-- 將多個配置文件位置放到列表中 --> <bean id="propertyResources" class="java.util.ArrayList"> <constructor-arg> <list> <!-- 這里支持多種尋址方式:classpath和file --> <value>classpath:/opt/demo/config/demo-db.properties</value> <!-- 推薦使用file的方式引入,這樣可以將配置和代碼分離 --> <value>file:/opt/demo/config/demo-mq.properties</value> <value>file:/opt/demo/config/demo-remote.properties</value> </list> </constructor-arg> </bean> <!-- 將配置文件讀取到容器中,交給Spring管理 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations" ref="propertyResources" /> </bean>