001/* 002 * Copyright 2002-2010 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.context.support; 018 019import org.springframework.beans.BeansException; 020import org.springframework.context.ApplicationContext; 021import org.springframework.core.io.FileSystemResource; 022import org.springframework.core.io.Resource; 023 024/** 025 * Standalone XML application context, taking the context definition files 026 * from the file system or from URLs, interpreting plain paths as relative 027 * file system locations (e.g. "mydir/myfile.txt"). Useful for test harnesses 028 * as well as for standalone environments. 029 * 030 * <p><b>NOTE:</b> Plain paths will always be interpreted as relative 031 * to the current VM working directory, even if they start with a slash. 032 * (This is consistent with the semantics in a Servlet container.) 033 * <b>Use an explicit "file:" prefix to enforce an absolute file path.</b> 034 * 035 * <p>The config location defaults can be overridden via {@link #getConfigLocations}, 036 * Config locations can either denote concrete files like "/myfiles/context.xml" 037 * or Ant-style patterns like "/myfiles/*-context.xml" (see the 038 * {@link org.springframework.util.AntPathMatcher} javadoc for pattern details). 039 * 040 * <p>Note: In case of multiple config locations, later bean definitions will 041 * override ones defined in earlier loaded files. This can be leveraged to 042 * deliberately override certain bean definitions via an extra XML file. 043 * 044 * <p><b>This is a simple, one-stop shop convenience ApplicationContext. 045 * Consider using the {@link GenericApplicationContext} class in combination 046 * with an {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader} 047 * for more flexible context setup.</b> 048 * 049 * @author Rod Johnson 050 * @author Juergen Hoeller 051 * @see #getResource 052 * @see #getResourceByPath 053 * @see GenericApplicationContext 054 */ 055public class FileSystemXmlApplicationContext extends AbstractXmlApplicationContext { 056 057 /** 058 * Create a new FileSystemXmlApplicationContext for bean-style configuration. 059 * @see #setConfigLocation 060 * @see #setConfigLocations 061 * @see #afterPropertiesSet() 062 */ 063 public FileSystemXmlApplicationContext() { 064 } 065 066 /** 067 * Create a new FileSystemXmlApplicationContext for bean-style configuration. 068 * @param parent the parent context 069 * @see #setConfigLocation 070 * @see #setConfigLocations 071 * @see #afterPropertiesSet() 072 */ 073 public FileSystemXmlApplicationContext(ApplicationContext parent) { 074 super(parent); 075 } 076 077 /** 078 * Create a new FileSystemXmlApplicationContext, loading the definitions 079 * from the given XML file and automatically refreshing the context. 080 * @param configLocation file path 081 * @throws BeansException if context creation failed 082 */ 083 public FileSystemXmlApplicationContext(String configLocation) throws BeansException { 084 this(new String[] {configLocation}, true, null); 085 } 086 087 /** 088 * Create a new FileSystemXmlApplicationContext, loading the definitions 089 * from the given XML files and automatically refreshing the context. 090 * @param configLocations array of file paths 091 * @throws BeansException if context creation failed 092 */ 093 public FileSystemXmlApplicationContext(String... configLocations) throws BeansException { 094 this(configLocations, true, null); 095 } 096 097 /** 098 * Create a new FileSystemXmlApplicationContext with the given parent, 099 * loading the definitions from the given XML files and automatically 100 * refreshing the context. 101 * @param configLocations array of file paths 102 * @param parent the parent context 103 * @throws BeansException if context creation failed 104 */ 105 public FileSystemXmlApplicationContext(String[] configLocations, ApplicationContext parent) throws BeansException { 106 this(configLocations, true, parent); 107 } 108 109 /** 110 * Create a new FileSystemXmlApplicationContext, loading the definitions 111 * from the given XML files. 112 * @param configLocations array of file paths 113 * @param refresh whether to automatically refresh the context, 114 * loading all bean definitions and creating all singletons. 115 * Alternatively, call refresh manually after further configuring the context. 116 * @throws BeansException if context creation failed 117 * @see #refresh() 118 */ 119 public FileSystemXmlApplicationContext(String[] configLocations, boolean refresh) throws BeansException { 120 this(configLocations, refresh, null); 121 } 122 123 /** 124 * Create a new FileSystemXmlApplicationContext with the given parent, 125 * loading the definitions from the given XML files. 126 * @param configLocations array of file paths 127 * @param refresh whether to automatically refresh the context, 128 * loading all bean definitions and creating all singletons. 129 * Alternatively, call refresh manually after further configuring the context. 130 * @param parent the parent context 131 * @throws BeansException if context creation failed 132 * @see #refresh() 133 */ 134 public FileSystemXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent) 135 throws BeansException { 136 137 super(parent); 138 setConfigLocations(configLocations); 139 if (refresh) { 140 refresh(); 141 } 142 } 143 144 145 /** 146 * Resolve resource paths as file system paths. 147 * <p>Note: Even if a given path starts with a slash, it will get 148 * interpreted as relative to the current VM working directory. 149 * This is consistent with the semantics in a Servlet container. 150 * @param path path to the resource 151 * @return Resource handle 152 * @see org.springframework.web.context.support.XmlWebApplicationContext#getResourceByPath 153 */ 154 @Override 155 protected Resource getResourceByPath(String path) { 156 if (path != null && path.startsWith("/")) { 157 path = path.substring(1); 158 } 159 return new FileSystemResource(path); 160 } 161 162}