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