import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.util.Assert;
public class RegexXmlFileResources {
protected Log logger = LogFactory.getLog(this.getClass());
private List<Resource> mappingResources; //解析后,保存符合条件的所有资源
//支持通配符匹配
private PathMatchingResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
private String[] configLoactions; //在配置文件中配的xml文件路径,包括通配符
public void setConfigLoactions(String[] configLoactions) {
this.configLoactions = configLoactions;
}
/**
* 所有属性设置完毕后执行
*
*/
public void init() throws Exception {
Assert.notNull(this.configLoactions, "Mapping file's config location can not be null.");
mappingResources = new ArrayList<Resource>(configLoactions.length);
for(String configLocation : configLoactions) {
mappingResources.addAll(Arrays.asList(patternResolver.getResources(configLocation)));
}
}
public void destroy() {
if(mappingResources != null) {
for(Resource mappingResource : mappingResources) {
try {
mappingResource.getInputStream().close();
if(logger.isInfoEnabled()) {
logger.info("Close mapping file inputstream");
}
} catch(IOException e) {
if(logger.isErrorEnabled()) {
logger.error("Can not close mapping resource.", e);
}
}
}
}
}
public void test() {
if(mappingResources != null) {
for(Resource mappingResource : mappingResources) {
try {
System.out.println(mappingResource.getFilename());
if(logger.isInfoEnabled()) {
logger.info("Close mapping file inputstream");
}
} catch(Exception e) {
if(logger.isErrorEnabled()) {
logger.error("Can not close mapping resource.", e);
}
}
}
}
}
}
<bean id="regexXmlFileResources" class="test.RegexXmlFileResources" init-method="init" destroy-method="destroy">
<property name="configLoactions">
<list>
<value>application*.xml</value>
</list>
</property>
</bean>
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.util.Assert;
public class RegexXmlFileResources {
protected Log logger = LogFactory.getLog(this.getClass());
private List<Resource> mappingResources; //解析后,保存符合条件的所有资源
//支持通配符匹配
private PathMatchingResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
private String[] configLoactions; //在配置文件中配的xml文件路径,包括通配符
public void setConfigLoactions(String[] configLoactions) {
this.configLoactions = configLoactions;
}
/**
* 所有属性设置完毕后执行
*
*/
public void init() throws Exception {
Assert.notNull(this.configLoactions, "Mapping file's config location can not be null.");
mappingResources = new ArrayList<Resource>(configLoactions.length);
for(String configLocation : configLoactions) {
mappingResources.addAll(Arrays.asList(patternResolver.getResources(configLocation)));
}
}
public void destroy() {
if(mappingResources != null) {
for(Resource mappingResource : mappingResources) {
try {
mappingResource.getInputStream().close();
if(logger.isInfoEnabled()) {
logger.info("Close mapping file inputstream");
}
} catch(IOException e) {
if(logger.isErrorEnabled()) {
logger.error("Can not close mapping resource.", e);
}
}
}
}
}
public void test() {
if(mappingResources != null) {
for(Resource mappingResource : mappingResources) {
try {
System.out.println(mappingResource.getFilename());
if(logger.isInfoEnabled()) {
logger.info("Close mapping file inputstream");
}
} catch(Exception e) {
if(logger.isErrorEnabled()) {
logger.error("Can not close mapping resource.", e);
}
}
}
}
}
}
<bean id="regexXmlFileResources" class="test.RegexXmlFileResources" init-method="init" destroy-method="destroy">
<property name="configLoactions">
<list>
<value>application*.xml</value>
</list>
</property>
</bean>