org.springframework.beans.factory.support.DefaultListableBeanFactory
instance and does not assume a specific bean definition format. Implements
the org.springframework.beans.factory.support.BeanDefinitionRegistry
interface in order to allow for applying any bean definition readers to it.
Typical usage is to register a variety of bean definitions via the
org.
interface and then call refresh() to initialize those beans
with application context semantics (handling
org., auto-detecting
BeanFactoryPostProcessors,
etc).
In contrast to other ApplicationContext implementations that create a new
internal BeanFactory instance for each refresh, the internal BeanFactory of
this context is available right from the start, to be able to register bean
definitions on it. refresh() may only be called once.
This ApplicationContext implementation is suitable for Ahead of Time
processing, using refreshForAotProcessing as an alternative to the
regular refresh().
Usage example:
GenericApplicationContext ctx = new GenericApplicationContext();
XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
xmlReader.loadBeanDefinitions(new ClassPathResource("applicationContext.xml"));
PropertiesBeanDefinitionReader propReader = new PropertiesBeanDefinitionReader(ctx);
propReader.loadBeanDefinitions(new ClassPathResource("otherBeans.properties"));
ctx.refresh();
MyBean myBean = (MyBean) ctx.getBean("myBean");
...
For the typical case of XML bean definitions, you may also use
ClassPathXmlApplicationContext or FileSystemXmlApplicationContext,
which are easier to set up - but less flexible, since you can just use standard
resource locations for XML bean definitions, rather than mixing arbitrary bean
definition formats. For a custom application context implementation supposed to
read a specific bean definition format in a refreshable manner, consider
deriving from the AbstractRefreshableApplicationContext base class.
registerBeanDefinition, refresh(), org.springframework.beans.factory.xml.XmlBeanDefinitionReader , org.springframework.beans.factory.support.PropertiesBeanDefinitionReader
| Modifier and Type | Class and Description |
|---|---|
| private static class | GenericApplicationContext.
|
| Modifier and Type | Field and Description |
|---|---|
| private final DefaultListableBeanFactory | |
| private boolean | |
| private final AtomicBoolean | |
| private ResourceLoader |
| Access | Constructor and Description |
|---|---|
| public | |
| public | GenericApplicationContext(DefaultListableBeanFactory
the DefaultListableBeanFactory instance to use for this context beanFactory)Create a new GenericApplicationContext with the given DefaultListableBeanFactory. |
| public | GenericApplicationContext(ApplicationContext
the parent application context parent)Create a new GenericApplicationContext with the given parent. |
| public | GenericApplicationContext(DefaultListableBeanFactory
the DefaultListableBeanFactory instance to use for this context beanFactory, ApplicationContext the parent application context parent)Create a new GenericApplicationContext with the given DefaultListableBeanFactory. |
| Modifier and Type | Method and Description |
|---|---|
| protected void | cancelRefresh(Throwable
the exception that led to the cancellation ex)Overrides org. Cancel this context's refresh attempt, resetting the |
| protected final void | closeBeanFactory()
Implements abstract org. Not much to do: We hold a single internal BeanFactory that will never get released. |
| public AutowireCapableBeanFactory | getAutowireCapableBeanFactory()
Overrides org. Implements org. Return this context's internal bean factory as AutowireCapableBeanFactory, if already available. |
| public BeanDefinition | getBeanDefinition(String
name of the bean to find a definition for beanName)Implements org. Return the BeanDefinition for the given bean name. |
| public final ConfigurableListableBeanFactory | getBeanFactory()
Implements abstract org. Implements org. Return the single internal BeanFactory held by this context (as ConfigurableListableBeanFactory). |
| public ClassLoader | getClassLoader()
Overrides org. Implements org. Expose the |
| public final DefaultListableBeanFactory | Returns: the internal bean factory (as DefaultListableBeanFactory)Return the underlying bean factory of this context, available for registering bean definitions. |
| public Resource | getResource(String
the resource location location)Overrides org. Implements org. This implementation delegates to this context's |
| public Resource[] | getResources(String
the location pattern to resolve locationPattern)Overrides org. Implements org. This implementation delegates to this context's ResourceLoader if it implements the ResourcePatternResolver interface, falling back to the default superclass behavior otherwise. |
| public boolean | isAlias(String
the name to check beanName)Implements org. Determine whether the given name is defined as an alias (as opposed to the name of an actually registered component). |
| public boolean | isBeanDefinitionOverridable(String
the name to check beanName)Overrides default org. Determine whether the bean definition for the given name is overridable,
i.e. whether |
| public boolean | isBeanNameInUse(String
the name to check beanName)Implements org. Determine whether the given bean name is already in use within this registry, i.e. whether there is a local bean or alias registered under this name. |
| private void | preDetermineBeanTypes(RuntimeHints runtimeHints)
Pre-determine bean types in order to trigger early proxy class creation. |
| protected final void | refreshBeanFactory()
Implements abstract org. Do nothing: We hold a single internal BeanFactory and rely on callers to register beans through our public methods (or the BeanFactory's). |
| public void | refreshForAotProcessing(RuntimeHints
the runtime hints runtimeHints)Load or refresh the persistent representation of the configuration up to a point where the underlying bean factory is ready to create bean instances. |
| public void | registerAlias(String
the canonical name beanName, String the alias to be registered alias)Implements org. Given a name, register an alias for it. |
| public <T> void | registerBean(Class<T>
the class of the bean beanClass, Object... custom argument values to be fed into Spring's
constructor resolution algorithm, resolving either all arguments or just
specific ones, with the rest to be resolved through regular autowiring
(may be constructorArgs)null or empty)Register a bean from the given bean class, optionally providing explicit constructor arguments for consideration in the autowiring process. |
| public <T> void | registerBean(String
the name of the bean (may be beanName, Class<T> null)the class of the bean beanClass, Object... custom argument values to be fed into Spring's
constructor resolution algorithm, resolving either all arguments or just
specific ones, with the rest to be resolved through regular autowiring
(may be constructorArgs)null or empty)Register a bean from the given bean class, optionally providing explicit constructor arguments for consideration in the autowiring process. |
| public final <T> void | registerBean(Class<T>
the class of the bean (resolving a public constructor
to be autowired, possibly simply the default constructor) beanClass, BeanDefinitionCustomizer... one or more callbacks for customizing the factory's
customizers)BeanDefinition, e.g. setting a lazy-init or primary flagRegister a bean from the given bean class, optionally customizing its bean definition metadata (typically declared as a lambda expression). |
| public final <T> void | registerBean(String
the name of the bean (may be beanName, Class<T> null)the class of the bean (resolving a public constructor
to be autowired, possibly simply the default constructor) beanClass, BeanDefinitionCustomizer... one or more callbacks for customizing the factory's
customizers)BeanDefinition, e.g. setting a lazy-init or primary flagRegister a bean from the given bean class, optionally customizing its bean definition metadata (typically declared as a lambda expression). |
| public final <T> void | registerBean(Class<T>
the class of the bean beanClass, Supplier<T> a callback for creating an instance of the bean supplier, BeanDefinitionCustomizer... one or more callbacks for customizing the factory's
customizers)BeanDefinition, e.g. setting a lazy-init or primary flagRegister a bean from the given bean class, using the given supplier for obtaining a new instance (typically declared as a lambda expression or method reference), optionally customizing its bean definition metadata (again typically declared as a lambda expression). |
| public <T> void | registerBean(String
the name of the bean (may be beanName, Class<T> null)the class of the bean beanClass, Supplier<T> a callback for creating an instance of the bean (in case
of supplier, BeanDefinitionCustomizer... null, resolving a public constructor to be autowired instead)one or more callbacks for customizing the factory's
customizers)BeanDefinition, e.g. setting a lazy-init or primary flagRegister a bean from the given bean class, using the given supplier for obtaining a new instance (typically declared as a lambda expression or method reference), optionally customizing its bean definition metadata (again typically declared as a lambda expression). |
| public void | registerBeanDefinition(String
the name of the bean instance to register beanName, BeanDefinition definition of the bean instance to register beanDefinition)Implements org. Register a new bean definition with this registry. |
| public void | removeAlias(String
the alias to remove alias)Implements org. Remove the specified alias from this registry. |
| public void | removeBeanDefinition(String
the name of the bean instance to register beanName)Implements org. Remove the BeanDefinition for the given name. |
| public void | setAllowBeanDefinitionOverriding(boolean allowBeanDefinitionOverriding)
Set whether it should be allowed to override bean definitions by registering a different definition with the same name, automatically replacing the former. |
| public void | setAllowCircularReferences(boolean allowCircularReferences)
Set whether to allow circular references between beans - and automatically try to resolve them. |
| public void | setApplicationStartup(ApplicationStartup
the new context event factory applicationStartup)Overrides org. Implements org. Set the |
| public void | setClassLoader(ClassLoader classLoader)
Overrides org. Implements org. Specify the ClassLoader to load class path resources and bean classes with. |
| public void | setParent(ApplicationContext
the parent context parent)Overrides org. Implements org. Set the parent of this application context, also setting the parent of the internal BeanFactory accordingly. |
| public void |