java.lang.Object | |||
↳ | org.springframework.core.io.DefaultResourceLoader | ||
↳ | org.springframework.context.support.AbstractApplicationContext | ||
↳ | org.springframework.context.support.GenericApplicationContext |
Known Direct Subclasses |
Known Indirect Subclasses |
Generic ApplicationContext implementation that holds a single internal
DefaultListableBeanFactory
instance and does not assume a specific bean definition format. Implements
the 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
BeanDefinitionRegistry
interface and then call refresh()
to initialize those beans
with application context semantics (handling
ApplicationContextAware
, 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.
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, simply 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. The equivalent in a web environment is
XmlWebApplicationContext
.
For custom application context implementations that are supposed to read
special bean definition formats in a refreshable manner, consider deriving
from the AbstractRefreshableApplicationContext
base class.
[Expand]
Inherited Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
org.springframework.context.support.AbstractApplicationContext
| |||||||||||
From interface
org.springframework.beans.factory.BeanFactory
| |||||||||||
From interface
org.springframework.context.ConfigurableApplicationContext
| |||||||||||
From interface
org.springframework.core.io.ResourceLoader
| |||||||||||
From interface
org.springframework.core.io.support.ResourcePatternResolver
|
[Expand]
Inherited Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
org.springframework.context.support.AbstractApplicationContext
|
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Create a new GenericApplicationContext.
| |||||||||||
Create a new GenericApplicationContext with the given DefaultListableBeanFactory.
| |||||||||||
Create a new GenericApplicationContext with the given parent.
| |||||||||||
Create a new GenericApplicationContext with the given DefaultListableBeanFactory.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Return the BeanDefinition for the given bean name.
| |||||||||||
Return the single internal BeanFactory held by this context
(as ConfigurableListableBeanFactory).
| |||||||||||
Return the underlying bean factory of this context,
available for registering bean definitions.
| |||||||||||
This implementation delegates to this context's ResourceLoader if set,
falling back to the default superclass behavior else.
| |||||||||||
This implementation delegates to this context's ResourceLoader if it
implements the ResourcePatternResolver interface, falling back to the
default superclass behavior else.
| |||||||||||
Determine whether this given name is defines as an alias
(as opposed to the name of an actually registered component).
| |||||||||||
Determine whether the given bean name is already in use within this registry,
i.e.
| |||||||||||
Given a name, register an alias for it.
| |||||||||||
Register a new bean definition with this registry.
| |||||||||||
Remove the specified alias from this registry.
| |||||||||||
Remove the BeanDefinition for the given name.
| |||||||||||
Set whether it should be allowed to override bean definitions by registering
a different definition with the same name, automatically replacing the former.
| |||||||||||
Set whether to allow circular references between beans - and automatically
try to resolve them.
| |||||||||||
Set the unique id of this application context.
| |||||||||||
Set the parent of this application context, also setting
the parent of the internal BeanFactory accordingly.
| |||||||||||
Set a ResourceLoader to use for this context.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Cancel this context's refresh attempt, resetting the
active flag
after an exception got thrown. | |||||||||||
Not much to do: We hold a single internal BeanFactory that will never
get released.
| |||||||||||
Do nothing: We hold a single internal BeanFactory and rely on callers
to register beans through our public methods (or the BeanFactory's).
|
Create a new GenericApplicationContext.
Create a new GenericApplicationContext with the given DefaultListableBeanFactory.
beanFactory | the DefaultListableBeanFactory instance to use for this context |
---|
Create a new GenericApplicationContext with the given parent.
parent | the parent application context |
---|
Create a new GenericApplicationContext with the given DefaultListableBeanFactory.
beanFactory | the DefaultListableBeanFactory instance to use for this context |
---|---|
parent | the parent application context |
Return the BeanDefinition for the given bean name.
beanName | name of the bean to find a definition for |
---|
null
)Return the single internal BeanFactory held by this context (as ConfigurableListableBeanFactory).
null
)Return the underlying bean factory of this context, available for registering bean definitions.
NOTE: You need to call refresh()
to initialize the
bean factory and its contained beans with application context semantics
(autodetecting BeanFactoryPostProcessors, etc).
This implementation delegates to this context's ResourceLoader if set, falling back to the default superclass behavior else.
location | the resource location |
---|
This implementation delegates to this context's ResourceLoader if it implements the ResourcePatternResolver interface, falling back to the default superclass behavior else.
locationPattern | the location pattern to resolve |
---|
IOException |
---|
Determine whether this given name is defines as an alias (as opposed to the name of an actually registered component).
beanName | the bean name to check |
---|
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.
beanName | the name to check |
---|
Given a name, register an alias for it.
beanName | the canonical name |
---|---|
alias | the alias to be registered |
Register a new bean definition with this registry. Must support RootBeanDefinition and ChildBeanDefinition.
beanName | the name of the bean instance to register |
---|---|
beanDefinition | definition of the bean instance to register |
BeanDefinitionStoreException |
---|
Remove the specified alias from this registry.
alias | the alias to remove |
---|
Remove the BeanDefinition for the given name.
beanName | the name of the bean instance to register |
---|
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".
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.
Set the unique id of this application context.
Default is the object id of the context instance, or the name of the context bean if the context is itself defined as a bean.
id | the unique id of the context |
---|
Set the parent of this application context, also setting the parent of the internal BeanFactory accordingly.
parent | the parent context |
---|
Set a ResourceLoader to use for this context. If set, the context will
delegate all getResource
calls to the given ResourceLoader.
If not set, default resource loading will apply.
The main reason to specify a custom ResourceLoader is to resolve resource paths (without URL prefix) in a specific fashion. The default behavior is to resolve such paths as class path locations. To resolve resource paths as file system locations, specify a FileSystemResourceLoader here.
You can also pass in a full ResourcePatternResolver, which will
be autodetected by the context and used for getResources
calls as well. Else, default resource pattern matching will apply.
Cancel this context's refresh attempt, resetting the active
flag
after an exception got thrown.
ex | the exception that led to the cancellation |
---|
Not much to do: We hold a single internal BeanFactory that will never get released.
Do nothing: We hold a single internal BeanFactory and rely on callers to register beans through our public methods (or the BeanFactory's).
IllegalStateException |
---|