001/* 002 * Copyright 2002-2017 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.ClassPathResource; 022import org.springframework.core.io.Resource; 023import org.springframework.lang.Nullable; 024import org.springframework.util.Assert; 025 026/** 027 * Standalone XML application context, taking the context definition files 028 * from the class path, interpreting plain paths as class path resource names 029 * that include the package path (e.g. "mypackage/myresource.txt"). Useful for 030 * test harnesses as well as for application contexts embedded within JARs. 031 * 032 * <p>The config location defaults can be overridden via {@link #getConfigLocations}, 033 * Config locations can either denote concrete files like "/myfiles/context.xml" 034 * or Ant-style patterns like "/myfiles/*-context.xml" (see the 035 * {@link org.springframework.util.AntPathMatcher} javadoc for pattern details). 036 * 037 * <p>Note: In case of multiple config locations, later bean definitions will 038 * override ones defined in earlier loaded files. This can be leveraged to 039 * deliberately override certain bean definitions via an extra XML file. 040 * 041 * <p><b>This is a simple, one-stop shop convenience ApplicationContext. 042 * Consider using the {@link GenericApplicationContext} class in combination 043 * with an {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader} 044 * for more flexible context setup.</b> 045 * 046 * @author Rod Johnson 047 * @author Juergen Hoeller 048 * @see #getResource 049 * @see #getResourceByPath 050 * @see GenericApplicationContext 051 */ 052public class ClassPathXmlApplicationContext extends AbstractXmlApplicationContext { 053 054 @Nullable 055 private Resource[] configResources; 056 057 058 /** 059 * Create a new ClassPathXmlApplicationContext for bean-style configuration. 060 * @see #setConfigLocation 061 * @see #setConfigLocations 062 * @see #afterPropertiesSet() 063 */ 064 public ClassPathXmlApplicationContext() { 065 } 066 067 /** 068 * Create a new ClassPathXmlApplicationContext for bean-style configuration. 069 * @param parent the parent context 070 * @see #setConfigLocation 071 * @see #setConfigLocations 072 * @see #afterPropertiesSet() 073 */ 074 public ClassPathXmlApplicationContext(ApplicationContext parent) { 075 super(parent); 076 } 077 078 /** 079 * Create a new ClassPathXmlApplicationContext, loading the definitions 080 * from the given XML file and automatically refreshing the context. 081 * @param configLocation resource location 082 * @throws BeansException if context creation failed 083 */ 084 public ClassPathXmlApplicationContext(String configLocation) throws BeansException { 085 this(new String[] {configLocation}, true, null); 086 } 087 088 /** 089 * Create a new ClassPathXmlApplicationContext, loading the definitions 090 * from the given XML files and automatically refreshing the context. 091 * @param configLocations array of resource locations 092 * @throws BeansException if context creation failed 093 */ 094 public ClassPathXmlApplicationContext(String... configLocations) throws BeansException { 095 this(configLocations, true, null); 096 } 097 098 /** 099 * Create a new ClassPathXmlApplicationContext with the given parent, 100 * loading the definitions from the given XML files and automatically 101 * refreshing the context. 102 * @param configLocations array of resource locations 103 * @param parent the parent context 104 * @throws BeansException if context creation failed 105 */ 106 public ClassPathXmlApplicationContext(String[] configLocations, @Nullable ApplicationContext parent) 107 throws BeansException { 108 109 this(configLocations, true, parent); 110 } 111 112 /** 113 * Create a new ClassPathXmlApplicationContext, loading the definitions 114 * from the given XML files. 115 * @param configLocations array of resource locations 116 * @param refresh whether to automatically refresh the context, 117 * loading all bean definitions and creating all singletons. 118 * Alternatively, call refresh manually after further configuring the context. 119 * @throws BeansException if context creation failed 120 * @see #refresh() 121 */ 122 public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh) throws BeansException { 123 this(configLocations, refresh, null); 124 } 125 126 /** 127 * Create a new ClassPathXmlApplicationContext with the given parent, 128 * loading the definitions from the given XML files. 129 * @param configLocations array of resource locations 130 * @param refresh whether to automatically refresh the context, 131 * loading all bean definitions and creating all singletons. 132 * Alternatively, call refresh manually after further configuring the context. 133 * @param parent the parent context 134 * @throws BeansException if context creation failed 135 * @see #refresh() 136 */ 137 public ClassPathXmlApplicationContext( 138 String[] configLocations, boolean refresh, @Nullable ApplicationContext parent) 139 throws BeansException { 140 141 super(parent); 142 setConfigLocations(configLocations); 143 if (refresh) { 144 refresh(); 145 } 146 } 147 148 149 /** 150 * Create a new ClassPathXmlApplicationContext, loading the definitions 151 * from the given XML file and automatically refreshing the context. 152 * <p>This is a convenience method to load class path resources relative to a 153 * given Class. For full flexibility, consider using a GenericApplicationContext 154 * with an XmlBeanDefinitionReader and a ClassPathResource argument. 155 * @param path relative (or absolute) path within the class path 156 * @param clazz the class to load resources with (basis for the given paths) 157 * @throws BeansException if context creation failed 158 * @see org.springframework.core.io.ClassPathResource#ClassPathResource(String, Class) 159 * @see org.springframework.context.support.GenericApplicationContext 160 * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader 161 */ 162 public ClassPathXmlApplicationContext(String path, Class<?> clazz) throws BeansException { 163 this(new String[] {path}, clazz); 164 } 165 166 /** 167 * Create a new ClassPathXmlApplicationContext, loading the definitions 168 * from the given XML files and automatically refreshing the context. 169 * @param paths array of relative (or absolute) paths within the class path 170 * @param clazz the class to load resources with (basis for the given paths) 171 * @throws BeansException if context creation failed 172 * @see org.springframework.core.io.ClassPathResource#ClassPathResource(String, Class) 173 * @see org.springframework.context.support.GenericApplicationContext 174 * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader 175 */ 176 public ClassPathXmlApplicationContext(String[] paths, Class<?> clazz) throws BeansException { 177 this(paths, clazz, null); 178 } 179 180 /** 181 * Create a new ClassPathXmlApplicationContext with the given parent, 182 * loading the definitions from the given XML files and automatically 183 * refreshing the context. 184 * @param paths array of relative (or absolute) paths within the class path 185 * @param clazz the class to load resources with (basis for the given paths) 186 * @param parent the parent context 187 * @throws BeansException if context creation failed 188 * @see org.springframework.core.io.ClassPathResource#ClassPathResource(String, Class) 189 * @see org.springframework.context.support.GenericApplicationContext 190 * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader 191 */ 192 public ClassPathXmlApplicationContext(String[] paths, Class<?> clazz, @Nullable ApplicationContext parent) 193 throws BeansException { 194 195 super(parent); 196 Assert.notNull(paths, "Path array must not be null"); 197 Assert.notNull(clazz, "Class argument must not be null"); 198 this.configResources = new Resource[paths.length]; 199 for (int i = 0; i < paths.length; i++) { 200 this.configResources[i] = new ClassPathResource(paths[i], clazz); 201 } 202 refresh(); 203 } 204 205 206 @Override 207 @Nullable 208 protected Resource[] getConfigResources() { 209 return this.configResources; 210 } 211 212}