<div class="iteye-blog-content-contain" style="font-size: 14px">
package com.alibaba.china.wdc.config;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.apache.commons.lang.StringUtils;
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 ModeSupportPlaceHolderConfigurer extends PropertyPlaceholderConfigurer {
private static Logger logger = LoggerFactory.getLogger(ModeSupportPlaceHolderConfigurer.class);
private static final String PRODUCTION_MODE = "production.mode";
private Resource[] locations;
private PropertiesPersister propertiesPersister = new DefaultPropertiesPersister();
private boolean ignoreResourceNotFound = false;
public static final String PROPERTY_FILE_EXTENSION = ".properties";
private String fileEncoding;
private static final String[] allowModes = new String[] { "daily", "publish" };
public static final boolean isOnline() {
return "publish".equalsIgnoreCase(System.getProperty(PRODUCTION_MODE));
}
/**
* Load properties into the given instance.
*
* @param props the Properties instance to load into
* @throws java.io.IOException in case of I/O errors
* @see #setLocations
*/
@Override
protected void loadProperties(Properties props) throws IOException {
if (this.locations != null) {
String mode = System.getProperty(PRODUCTION_MODE);
if (StringUtils.isEmpty(mode)) {
mode = "daily";
}
mode = mode.toLowerCase();
if (logger.isDebugEnabled()) {
logger.debug("production model is " + mode);
}
for (Resource location : this.locations) {
if (logger.isInfoEnabled()) {
logger.info("Loading properties file from " + location);
}
InputStream is = null;
try {
String filename = null;
try {
filename = location.getFilename();
} catch (IllegalStateException ex) {
// resource is not file-based. See SPR-7552.
}
is = location.getInputStream();
if (filename != null && filename.endsWith(PROPERTY_FILE_EXTENSION)
&& !isExcludeProperty(mode, filename)) {
if (this.fileEncoding != null) {
this.propertiesPersister.load(props, new InputStreamReader(is, this.fileEncoding));
} else {
this.propertiesPersister.load(props, is);
}
}
} catch (IOException ex) {
if (this.ignoreResourceNotFound) {
if (logger.isWarnEnabled()) {
logger.warn("Could not load properties from " + location + ": " + ex.getMessage());
}
} else {
throw ex;
}
} finally {
if (is != null) {
is.close();
}
}
}
}
}
private String[] getExcludeMode(String mode) {
if (StringUtils.isEmpty(mode)) {
return allowModes;
}
List<String> excludeModes = new ArrayList<String>();
mode = mode.toLowerCase();
for (String m : allowModes) {
if (!m.equals(mode)) {
excludeModes.add(m);
}
}
return excludeModes.toArray(new String[] {});
}
private boolean isExcludeProperty(String mode, String filename) {
String[] excludeModes = getExcludeMode(mode);
for (String m : excludeModes) {
if (filename.endsWith(m + PROPERTY_FILE_EXTENSION)) {
return true;
}
}
return false;
}
public void setLocations(Resource[] locations) {
this.locations = locations;
}
public void setPropertiesPersister(PropertiesPersister propertiesPersister) {
this.propertiesPersister = propertiesPersister;
}
public void setIgnoreResourceNotFound(boolean ignoreResourceNotFound) {
this.ignoreResourceNotFound = ignoreResourceNotFound;
}
public void setFileEncoding(String fileEncoding) {
this.fileEncoding = fileEncoding;
}
}
</div>
相关推荐
NULL 博文链接:https://wokeke.iteye.com/blog/2214113
Spring中PropertyPlaceholderConfigurer的使用
Spring属性占位符PropertyPlaceholderConfigurer的使用,自己写的一个demo
主要介绍了Spring如何使用PropertyPlaceholderConfigurer读取文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Spring.NET是一个应用程序框架,其目的是协助开发人员创建企业级的.NET应用程序。它提供了很多方面的功能,比如依赖注入、面向方面编程(AOP)、数据访问抽象及ASP.NET扩展等等。Spring.NET以Java版的Spring框架为...
在Spring3中配置数据源,包括DBCP,C3P0,Proxool,Bonecp主要的数据源,里面包含这些数据源的jar文件和依赖文件及配置文件。。 如Bonecp目前听说是最快的数据源,速度是传统的c3p0的25倍, bonecp.properties文件: ...
12.6.1. 在Spring环境中建立JPA 12.6.1.1. LocalEntityManagerFactoryBean 12.6.1.2. LocalContainerEntityManagerFactoryBean 12.6.1.3. 处理多个持久化单元 12.6.2. JpaTemplate 和 JpaDaoSupport 12.6.3. 基于...
6 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 7 8 9 <value>/WEB-INF/jdbc.properties 10 11 12 13 14 它配置了以下功能: 读取...
#Spring 属性注释扩展的 PropertyPlaceHolderConfigurer 使用注解将配置属性注入到 Spring 组件中。 注意:Spring 3 现在支持使用 @Value 注释的容器的。 该项目仅用于 Spring 2.5.x 支持。 ##入门Spring房产注解...
前几天搞 Spring + Hibernate + Struts 事务配置 ,网上找了好多资料,不过好无语,大多都是 Ctrl + V,浪费俺的宝贵时间 现在我总结配出一套,给大家参考参考,可能有不足,请大家多多交流。 附:内有弹出...
认识 Spring 来认识 Spring 的一些特性,并初步了解一下什么叫作 IoC?什么叫作 DI? 简介 Spring Inversion of Control Dependency Injection <br> 核心容器 Spring 核心容器实作...
NULL 博文链接:https://chyy001.iteye.com/blog/1338488
-- 当配置文件修改后,系统自动加载该文件。开发阶段建议打开此功能 --> <!-- 指定浏览器输出的编码格式 --> <!--将action内容放在package元素下,package元素的name值与extends值可以使用默认值,如下所...
第一部分 Spring framework 概述..5 第1 章 Spring Framework 介绍..6 1.1 依赖注入和控制反转.6 1.2 模块6 1.2.1 核心容器.7 1.2.2 数据访问/ 整合..7 1.2.3 Web ..8 1.2.4 AOP...
spring3.1中文参考文档,南磊翻译,现在有4章,目录如下: 第一部分 Spring framework概述.......................................................................................................................
PropertyPlaceholderConfigurer PropertyResourceConfigurer PropertyValue PropertyValues PropertyValuesEditor PrototypeAspectInstanceFactory PrototypeTargetSource ProxyConfig ProxyFactory ...
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
与通过从属性文件读取其配置来进行自我配置相比,对于应用程序而言,有什么可能更简单? 好吧,对于业务应用程序而言,事实并非如此简单! 出现的问题如下:0.如何根据执行平台读取不同的属性? 0.如何保护敏感值,...
JDBCTemp+MySQL示例。Spring,PropertyPlaceholderConfigurer,BeanPropertyRowMapper