dubbo spring 不同的环境配置多个配置文件
2018-01-24 00:00
190 查看
<!-- 配置文件,按配置顺序,后面的比前面的优先 -->
<bean id="myConfig" class="com.***.common.util.MyConfigPropertyPlaceholder">
<property name="locations">
<list>
<value>classpath:dubbo.properties</value>
<value>file:/home/config/user/dubbo.properties</value>
<value>file:D:\config\user\dubbo.properties</value>
</list>
</property>
</bean>
自己写的类继承自PropertyPlaceholderConfigurer,读取多个配置文件的时候,即使文件不存在也不会报错。不影响程序启动。
在xml里用${jdbc.master.user}读取配置项,
在java里在属性的set方法上面用@Value(value="${jdbc.master.user}") 就可以了。
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.core.io.Resource;
import org.springframework.util.DefaultPropertiesPersister;
import org.springframework.util.PropertiesPersister;
public class MyConfigPropertyPlaceholder extends PropertyPlaceholderConfigurer{
private Logger logger = LoggerFactory.getLogger(getClass());
protected Properties[] localProperties;
protected boolean localOverride = false;
private Resource[] locations;
private PropertiesPersister propertiesPersister = new DefaultPropertiesPersister();
public void setLocations(Resource[] locations) {
this.locations = locations;
}
public void setLocalOverride(boolean localOverride) {
this.localOverride = localOverride;
}
protected void loadProperties(Properties props) throws IOException {
if (locations != null) {
for (Resource location : this.locations) {
InputStream is = null;
try {
is = location.getInputStream();
this.propertiesPersister.load(props, is);
logger.info("读取配置文件{}成功",location);
} catch (IOException ex) {
logger.info("读取配置文件{}失败.....",location);
} finally {
if (is != null) {
is.close();
}
}
}
}
}
}
<bean id="myConfig" class="com.***.common.util.MyConfigPropertyPlaceholder">
<property name="locations">
<list>
<value>classpath:dubbo.properties</value>
<value>file:/home/config/user/dubbo.properties</value>
<value>file:D:\config\user\dubbo.properties</value>
</list>
</property>
</bean>
自己写的类继承自PropertyPlaceholderConfigurer,读取多个配置文件的时候,即使文件不存在也不会报错。不影响程序启动。
在xml里用${jdbc.master.user}读取配置项,
在java里在属性的set方法上面用@Value(value="${jdbc.master.user}") 就可以了。
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.core.io.Resource;
import org.springframework.util.DefaultPropertiesPersister;
import org.springframework.util.PropertiesPersister;
public class MyConfigPropertyPlaceholder extends PropertyPlaceholderConfigurer{
private Logger logger = LoggerFactory.getLogger(getClass());
protected Properties[] localProperties;
protected boolean localOverride = false;
private Resource[] locations;
private PropertiesPersister propertiesPersister = new DefaultPropertiesPersister();
public void setLocations(Resource[] locations) {
this.locations = locations;
}
public void setLocalOverride(boolean localOverride) {
this.localOverride = localOverride;
}
protected void loadProperties(Properties props) throws IOException {
if (locations != null) {
for (Resource location : this.locations) {
InputStream is = null;
try {
is = location.getInputStream();
this.propertiesPersister.load(props, is);
logger.info("读取配置文件{}成功",location);
} catch (IOException ex) {
logger.info("读取配置文件{}失败.....",location);
} finally {
if (is != null) {
is.close();
}
}
}
}
}
}
相关文章推荐
- spring profile来用不同的文件,配置不同的环境, deploy不同的war
- Maven根据不同环境打包不同配置文件
- Maven根据不同环境打包不同配置文件
- maven发布时在不同的环境使用不同的配置文件
- 不同环境(配置文件)下的项目发布
- 利用maven的resources、filter和profile实现不同环境使用不同配置文件
- laravel配置不同环境的配置文件
- Maven根据不同环境打包不同配置文件
- Maven根据Profile读取不同配置环境配置文件
- 使用Maven filter和profile隔离不同环境的配置文件
- Maven profile结合Maven AntRun Plugin实现不同环境配置文件分离打包
- SpringBoot 不同环境读取不同的配置信息文件
- Laravel在不同的环境调用不同的配置文件
- Spring-Boot使用profile来配置不同环境的配置文件
- Maven根据不同环境打包不同配置文件
- 使用Spring3.1后的的Profile配置使不同环境加载不同配置文件
- [置顶] spring mvc 利用maven实现不同环境使用不同配置文件
- maven 不同环境加载不同spring配置文件
- 项目实现不同环境不同配置文件-maven profile
- 用maven filter 管理不同环境的配置文件