编写类来加载Jar包中的Spring配置文件
2014-03-27 19:09
375 查看
public class ContextLoader extends org.springframework.web.context.ContextLoader { protected final Log logger = LogFactory.getLog(this.getClass()); //该方法是创建webApplictionContext的回调方法 protected WebApplicationContext createWebApplicationContext( ServletContext servletContext, ApplicationContext parent) throws BeansException { // 所有待加载配置文件jar包的url字符串 List<String> jarUrls = new ArrayList<String>(); // 因为有可能在平台开发时不加载jar包中的文件 if(HiConfigHolder.getJarFile() != null){ // 找到要加载spring配置文件的jar包文件 String[] jars = HiConfigHolder.getJarFile().trim().split("[,]"); for (String jarFileName : jars) { String hiJarUrl = null; try { File classFile = new File(this.getClass().getClassLoader().getResource("").toURI()); hiJarUrl = (new URL(classFile.getParentFile().toURI().toString() + "/lib/"+jarFileName)).getFile(); if(hiJarUrl != null) jarUrls.add( URLDecoder.decode(hiJarUrl,"utf-8")); } catch (Exception e) { logger.error("error:load jar file == " + jarFileName); } } } Class contextClass = determineContextClass(servletContext); if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) { throw new ApplicationContextException("Custom context class [" + contextClass.getName() + "] is not of type ConfigurableWebApplicationContext"); } List<String> allResources = new ArrayList<String>(); ConfigurableWebApplicationContext wac = (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass); wac.setParent(parent); wac.setServletContext(servletContext); String configLocation = servletContext.getInitParameter(CONFIG_LOCATION_PARAM); if (configLocation != null) { String[] resources = StringUtils.tokenizeToStringArray(configLocation,ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS); // 第一次加载resources的目的是为jar文件中的配置所须的依赖提供支持 for (int i = 0; i < resources.length; i++) { if(resources[i] == null || resources[i].equals("")) continue; if(resources[i].toUpperCase().indexOf("WEB-INF") >= 0) allResources.add(resources[i]); } // 加载jar包中的配置文件 if(jarUrls.size() > 0){ for (String hiJarUrl : jarUrls) { if(hiJarUrl != null){ Resource[] jarResources = JarResource.getInstance(new File(hiJarUrl), "*appContext-*.xml"); for (Resource resource : jarResources) { try { allResources.add(resource.getURL().toString()); } catch (IOException e) {} } } } } // 第二次加载resources的目标的覆盖jar文件中有可能与classes中重名的配置文件,从而使classes中的配置文件优先于jar中的配置文件 for (int i = 0; i < resources.length; i++) { if(resources[i] == null || resources[i].equals("")) continue; if(resources[i].toUpperCase().indexOf("WEB-INF") < 0) allResources.add(resources[i]); } wac.setConfigLocations(allResources.toArray(new String[allResources.size()])); } wac.refresh(); return wac; } }
/** * 获取WEB-INF/lib的路径 * @return * lib的绝对路径 */ private String getWebInfLibPath() { URL url = getClass().getProtectionDomain().getCodeSource() .getLocation(); String path = url.toString(); int index = path.indexOf("WEB-INF"); if (index == -1) { index = path.indexOf("classes"); } if (index == -1) { index = path.indexOf("bin"); } path = path.substring(0, index+7)+"/lib/"; if (path.startsWith("zip")) {// 当class文件在war中时,此时返回zip:D:/...这样的路径 path = path.substring(4); } else if (path.startsWith("file")) {// 当class文件在class文件中时,此时返回file:/D:/...这样的路径 path = path.substring(6); } else if (path.startsWith("jar")) {// 当class文件在jar文件里面时,此时返回jar:file:/D:/...这样的路径 path = path.substring(10); } try { path = URLDecoder.decode(path, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return path; }
//获取jar包中以某个字符串开头的jar
//文件,例如platform private List<String> getPlatformJarFileName() { String path = getWebInfLibPath(); List<String> platformJarName = new ArrayList<String>(); File file = new File(path); File[] array = file.listFiles(); for (int i = 0; i < array.length; i++) { //如果是jar文件并且以platform开头,记录一下改jar文件 if (array[i].isFile() && org.apache.commons.lang.StringUtils.isNotEmpty(array[i].getName()) && array[i].getName().startsWith("platform")) { platformJarName.add(array[i].getName()); } } return platformJarName; }
相关文章推荐
- spring boot通过jar包启动时,配置文件的加载顺序
- spring加载jar包中多个配置文件(转)
- spring加载jar包中多个配置文件
- spring加载jar包中多个配置文件
- 可以从Jar外部加载JDBC.properties的Spring-mybatis配置文件
- spring加载jar包中多个xml配置文件 mappingLocations
- Spring配置文件打包到jar中无法加载问题之解决方案
- 可以从Jar外部加载JDBC.properties的Spring-mybatis配置文件
- is not mapped问题,Spring加载jar中配置文件
- spring加载jar包中多个配置文件
- Spring的ApplicationContext加载多个配置文件的三种方式
- 加载Spring配置文件常用的三种方法
- spring学习总结(六):IOC & DI 配置Bean之作用域、加载外部属性文件、SPEL
- spring加载多个配置文件
- Spring中加载xml配置文件的方式
- Springboot jar包外指定配置文件及原理
- Spring : Spring加载配置文件classpath、classpath*、file解析
- spring-boot 加载本地静态资源文件路径配置
- spring之把配置文件的加载交给服务
- Spring配置文件加载流程