发布日期:2009-9-25 8:02:48
实现:config文件有多个地方,按照优先次序依次查找,按照第一个找到的config来应用程序。
具体代码:覆盖spring类,建立类CustomPropertyPlaceholderConfigurer.java
package com.XXX.config;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.core.io.Resource;
public class CustomPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
@Override
public void setLocations(Resource[] locations) {
List<Resource> existResourceList = new ArrayList<Resource>();
for (int i = locations.length-1; i >=0 ; i--) {
Resource resource = locations[i];
if(resource.exists())
existResourceList.add(resource);
}
Resource[] existResource = new Resource[existResourceList.size()];
super.setLocations(existResourceList.toArray(existResource));
}
}
spring配置文件中使用:
<bean class="com.XXX.config.CustomPropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>file:./config.properties</value>
<value>classpath:/config/config.properties</value>
</list>
</property>
</bean>
<bean id="dataSource_XXX_db"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>${jdbc.XXX_db.url}</value>
</property>
<property name="username">
<value>${jdbc.XXX_db.username}</value>
</property>
<property name="password">
<value>${jdbc.XXX_db.password}</value>
</property>
</bean>