diff --git a/org.springframework.context/src/main/java/org/springframework/context/annotation/AnnotationConfigApplicationContext.java b/org.springframework.context/src/main/java/org/springframework/context/annotation/AnnotationConfigApplicationContext.java index 8ee35b30635..a60f30e9da5 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/annotation/AnnotationConfigApplicationContext.java +++ b/org.springframework.context/src/main/java/org/springframework/context/annotation/AnnotationConfigApplicationContext.java @@ -27,9 +27,6 @@ import org.springframework.context.support.GenericApplicationContext; * registering classes one by one ({@link #register}) as well as for classpath scanning * ({@link #scan}). * - *

Useful for test harnesses or any other scenario where XML-based configuration - * is unnecessary or undesired. - * *

In case of multiple Configuration classes, {@link Bean} methods defined in later * classes will override those defined in earlier classes. This can be leveraged to * deliberately override certain bean definitions via an extra Configuration class. @@ -37,8 +34,11 @@ import org.springframework.context.support.GenericApplicationContext; * @author Chris Beams * @author Juergen Hoeller * @since 3.0 + * @see #register + * @see #scan * @see AnnotatedBeanDefinitionReader * @see ClassPathBeanDefinitionScanner + * @see org.springframework.context.support.GenericXmlApplicationContext */ public class AnnotationConfigApplicationContext extends GenericApplicationContext { @@ -55,9 +55,8 @@ public class AnnotationConfigApplicationContext extends GenericApplicationContex } /** - * Create a new AnnotationConfigApplicationContext, deriving bean - * definitions from the given annotated classes and automatically - * refreshing the context. + * Create a new AnnotationConfigApplicationContext, deriving bean definitions + * from the given annotated classes and automatically refreshing the context. * @param annotatedClasses one or more annotated classes, * e.g. {@link Configuration @Configuration} classes */ @@ -67,9 +66,8 @@ public class AnnotationConfigApplicationContext extends GenericApplicationContex } /** - * Create a new AnnotationConfigApplicationContext, scanning for bean - * definitions in the given packages and automatically refreshing the - * context. + * Create a new AnnotationConfigApplicationContext, scanning for bean definitions + * in the given packages and automatically refreshing the context. * @param basePackages the packages to check for annotated classes */ public AnnotationConfigApplicationContext(String... basePackages) { diff --git a/org.springframework.context/src/main/java/org/springframework/context/support/GenericApplicationContext.java b/org.springframework.context/src/main/java/org/springframework/context/support/GenericApplicationContext.java index cc0218172c8..f6db1726836 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/support/GenericApplicationContext.java +++ b/org.springframework.context/src/main/java/org/springframework/context/support/GenericApplicationContext.java @@ -156,6 +156,27 @@ public class GenericApplicationContext extends AbstractApplicationContext implem this.beanFactory.setSerializationId(id); } + /** + * Set whether it should be allowed to override bean definitions by registering + * a different definition with the same name, automatically replacing the former. + * If not, an exception will be thrown. Default is "true". + * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowBeanDefinitionOverriding + */ + public void setAllowBeanDefinitionOverriding(boolean allowBeanDefinitionOverriding) { + this.beanFactory.setAllowBeanDefinitionOverriding(allowBeanDefinitionOverriding); + } + + /** + * Set whether to allow circular references between beans - and automatically + * try to resolve them. + *

Default is "true". Turn this off to throw an exception when encountering + * a circular reference, disallowing them completely. + * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowCircularReferences + */ + public void setAllowCircularReferences(boolean allowCircularReferences) { + this.beanFactory.setAllowCircularReferences(allowCircularReferences); + } + /** * Set a ResourceLoader to use for this context. If set, the context will * delegate all getResource calls to the given ResourceLoader. diff --git a/org.springframework.context/src/main/java/org/springframework/context/support/GenericXmlApplicationContext.java b/org.springframework.context/src/main/java/org/springframework/context/support/GenericXmlApplicationContext.java new file mode 100644 index 00000000000..d4f759d4b70 --- /dev/null +++ b/org.springframework.context/src/main/java/org/springframework/context/support/GenericXmlApplicationContext.java @@ -0,0 +1,96 @@ +/* + * Copyright 2002-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.context.support; + +import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; +import org.springframework.core.io.Resource; + +/** + * Convenient application context with built-in XML support. + * This is a flexible alternative to {@link ClassPathXmlApplicationContext} + * and {@link FileSystemXmlApplicationContext}, to be configured via setters, + * with an eventual {@link #refresh()} call activating the context. + * + *

In case of multiple configuration files, bean definitions in later files + * will override those defined in earlier files. This can be leveraged to + * deliberately override certain bean definitions via an extra configuration file. + * + * @author Juergen Hoeller + * @since 3.0 + * @see #load + * @see XmlBeanDefinitionReader + * @see org.springframework.context.annotation.AnnotationConfigApplicationContext + */ +public class GenericXmlApplicationContext extends GenericApplicationContext { + + private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this); + + + /** + * Create a new GenericXmlApplicationContext that needs to be populated + * through {@link #load} calls and then manually {@link #refresh refreshed}. + */ + public GenericXmlApplicationContext() { + } + + /** + * Create a new GenericXmlApplicationContext, loading bean definitions + * from the given resources and automatically refreshing the context. + * @param resources the resources to load from + */ + public GenericXmlApplicationContext(Resource... resources) { + load(resources); + refresh(); + } + + /** + * Create a new GenericXmlApplicationContext, loading bean definitions + * from the given resource locations and automatically refreshing the context. + * @param resourceLocations the resources to load from + */ + public GenericXmlApplicationContext(String... resourceLocations) { + load(resourceLocations); + refresh(); + } + + + /** + * Set whether to use XML validation. + */ + public void setValidating(boolean validating) { + this.reader.setValidationMode(validating ? + XmlBeanDefinitionReader.VALIDATION_AUTO : XmlBeanDefinitionReader.VALIDATION_NONE); + } + + + /** + * Load bean definitions from the given XML resources. + * @param resources one or more resources to load from + */ + public void load(Resource... resources) { + this.reader.loadBeanDefinitions(resources); + } + + /** + * Load bean definitions from the given XML resources. + * @param resourceLocations one or more resource locations to load from + */ + public void load(String... resourceLocations) { + this.reader.loadBeanDefinitions(resourceLocations); + } + +}