spring加载文件使用通配符

来源:互联网 发布:双十一淘宝一元秒杀 编辑:程序博客网 时间:2024/06/02 03:54

不管是在web应用还是非Web应用中我们都希望在一个目录下放置配置文件,然后spring启动时加载配置目录下所有配置文件初始化context,这个要怎么做么首先想到的是使用通配符如:

spring-*.xml

表示以spring-开头的xml文件


需要注意的是要加上URL前缀或协议告诉spring怎么加载文件.

参考org.springframework.util.ResourceUtils:

/** Pseudo URL prefix for loading from the class path: "classpath:" */public static final String CLASSPATH_URL_PREFIX = "classpath:";/** URL prefix for loading from the file system: "file:" */public static final String FILE_URL_PREFIX = "file:";/** URL protocol for a file in the file system: "file" */public static final String URL_PROTOCOL_FILE = "file";/** URL protocol for an entry from a jar file: "jar" */public static final String URL_PROTOCOL_JAR = "jar";/** URL protocol for an entry from a zip file: "zip" */public static final String URL_PROTOCOL_ZIP = "zip";/** URL protocol for an entry from a JBoss jar file: "vfszip" */public static final String URL_PROTOCOL_VFSZIP = "vfszip";/** URL protocol for a JBoss VFS resource: "vfs" */public static final String URL_PROTOCOL_VFS = "vfs";/** URL protocol for an entry from a WebSphere jar file: "wsjar" */public static final String URL_PROTOCOL_WSJAR = "wsjar";/** URL protocol for an entry from an OC4J jar file: "code-source" */public static final String URL_PROTOCOL_CODE_SOURCE = "code-source";/** Separator between JAR URL and file path within the JAR */public static final String JAR_URL_SEPARATOR = "!/";
如要加载classpath下所有以spring-开头的xml文件应该这样写:

classpath:spring-*.xml
又如要加载f:/config下下所有以spring-开头的xml文件应该这样写:

file:f:/config/*.xml