|
The Spring Framework | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.core.io.DefaultResourceLoader org.springframework.context.support.AbstractApplicationContext org.springframework.context.support.GenericApplicationContext
public class GenericApplicationContext
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 AbstractApplicationContext.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. AbstractApplicationContext.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.
registerBeanDefinition(java.lang.String, org.springframework.beans.factory.config.BeanDefinition)
,
AbstractApplicationContext.refresh()
,
XmlBeanDefinitionReader
,
PropertiesBeanDefinitionReader
Field Summary |
---|
Fields inherited from class org.springframework.context.support.AbstractApplicationContext |
---|
APPLICATION_EVENT_MULTICASTER_BEAN_NAME, logger, MESSAGE_SOURCE_BEAN_NAME |
Fields inherited from interface org.springframework.beans.factory.BeanFactory |
---|
FACTORY_BEAN_PREFIX |
Fields inherited from interface org.springframework.beans.factory.BeanFactory |
---|
FACTORY_BEAN_PREFIX |
Fields inherited from interface org.springframework.core.io.support.ResourcePatternResolver |
---|
CLASSPATH_ALL_URL_PREFIX |
Fields inherited from interface org.springframework.core.io.ResourceLoader |
---|
CLASSPATH_URL_PREFIX |
Constructor Summary | |
---|---|
GenericApplicationContext()
Create a new GenericApplicationContext. |
|
GenericApplicationContext(ApplicationContext parent)
Create a new GenericApplicationContext with the given parent. |
|
GenericApplicationContext(DefaultListableBeanFactory beanFactory)
Create a new GenericApplicationContext with the given DefaultListableBeanFactory. |
|
GenericApplicationContext(DefaultListableBeanFactory beanFactory,
ApplicationContext parent)
Create a new GenericApplicationContext with the given DefaultListableBeanFactory. |
Method Summary | |
---|---|
protected void |
closeBeanFactory()
Do nothing: We hold a single internal BeanFactory that will never get released. |
BeanDefinition |
getBeanDefinition(String beanName)
Return the BeanDefinition for the given bean name. |
ConfigurableListableBeanFactory |
getBeanFactory()
Return the single internal BeanFactory held by this context (as ConfigurableListableBeanFactory). |
DefaultListableBeanFactory |
getDefaultListableBeanFactory()
Return the underlying bean factory of this context, available for registering bean definitions. |
Resource |
getResource(String location)
This implementation delegates to this context's ResourceLoader if set, falling back to the default superclass behavior else. |
Resource[] |
getResources(String locationPattern)
This implementation delegates to this context's ResourceLoader if it implements the ResourcePatternResolver interface, falling back to the default superclass behavior else. |
protected void |
refreshBeanFactory()
Do nothing: We hold a single internal BeanFactory and rely on callers to register beans through our public methods (or the BeanFactory's). |
void |
registerAlias(String beanName,
String alias)
Given a bean name, create an alias. |
void |
registerBeanDefinition(String beanName,
BeanDefinition beanDefinition)
Register a new bean definition with this registry. |
void |
setParent(ApplicationContext parent)
Set the parent of this application context, also setting the parent of the internal BeanFactory accordingly. |
void |
setResourceLoader(ResourceLoader resourceLoader)
Set a ResourceLoader to use for this context. |
Methods inherited from class org.springframework.core.io.DefaultResourceLoader |
---|
getClassLoader, getResourceByPath, setClassLoader |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Methods inherited from interface org.springframework.beans.factory.support.BeanDefinitionRegistry |
---|
containsBeanDefinition, getAliases, getBeanDefinitionCount, getBeanDefinitionNames |
Methods inherited from interface org.springframework.core.io.ResourceLoader |
---|
getClassLoader |
Constructor Detail |
---|
public GenericApplicationContext()
public GenericApplicationContext(DefaultListableBeanFactory beanFactory)
beanFactory
- the DefaultListableBeanFactory instance to use for this contextregisterBeanDefinition(java.lang.String, org.springframework.beans.factory.config.BeanDefinition)
,
AbstractApplicationContext.refresh()
public GenericApplicationContext(ApplicationContext parent)
parent
- the parent application contextregisterBeanDefinition(java.lang.String, org.springframework.beans.factory.config.BeanDefinition)
,
AbstractApplicationContext.refresh()
public GenericApplicationContext(DefaultListableBeanFactory beanFactory, ApplicationContext parent)
beanFactory
- the DefaultListableBeanFactory instance to use for this contextparent
- the parent application contextregisterBeanDefinition(java.lang.String, org.springframework.beans.factory.config.BeanDefinition)
,
AbstractApplicationContext.refresh()
Method Detail |
---|
public void setParent(ApplicationContext parent)
setParent
in interface ConfigurableApplicationContext
setParent
in class AbstractApplicationContext
parent
- the parent contextConfigurableBeanFactory.setParentBeanFactory(org.springframework.beans.factory.BeanFactory)
public void setResourceLoader(ResourceLoader resourceLoader)
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 (withour 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.
public Resource getResource(String location)
getResource
in interface ResourceLoader
getResource
in class DefaultResourceLoader
location
- the resource location
setResourceLoader(org.springframework.core.io.ResourceLoader)
public Resource[] getResources(String locationPattern) throws IOException
getResources
in interface ResourcePatternResolver
getResources
in class AbstractApplicationContext
locationPattern
- the location pattern to resolve
IOException
- in case of I/O errorssetResourceLoader(org.springframework.core.io.ResourceLoader)
protected final void refreshBeanFactory() throws IllegalStateException
refreshBeanFactory
in class AbstractApplicationContext
IllegalStateException
- if already initialized and multiple refresh
attempts are not supportedregisterBeanDefinition(java.lang.String, org.springframework.beans.factory.config.BeanDefinition)
protected final void closeBeanFactory()
closeBeanFactory
in class AbstractApplicationContext
public final ConfigurableListableBeanFactory getBeanFactory()
getBeanFactory
in interface ConfigurableApplicationContext
getBeanFactory
in class AbstractApplicationContext
null
)AbstractApplicationContext.refreshBeanFactory()
,
AbstractApplicationContext.closeBeanFactory()
public final DefaultListableBeanFactory getDefaultListableBeanFactory()
NOTE: You need to call AbstractApplicationContext.refresh()
to initialize the
bean factory and its contained beans with application context semantics
(autodetecting BeanFactoryPostProcessors, etc).
public BeanDefinition getBeanDefinition(String beanName) throws BeansException
BeanDefinitionRegistry
getBeanDefinition
in interface BeanDefinitionRegistry
beanName
- name of the bean to find a definition for
null
)
NoSuchBeanDefinitionException
- if there is no such bean definition
BeansException
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) throws BeansException
BeanDefinitionRegistry
registerBeanDefinition
in interface BeanDefinitionRegistry
beanName
- the name of the bean instance to registerbeanDefinition
- definition of the bean instance to register
BeanDefinitionStoreException
- if the BeanDefinition is invalid
or if there is already a BeanDefinition for the specified bean name
(and we are not allowed to override it)
BeansException
RootBeanDefinition
,
ChildBeanDefinition
public void registerAlias(String beanName, String alias) throws BeansException
BeanDefinitionRegistry
registerAlias
in interface BeanDefinitionRegistry
beanName
- the canonical name of the beanalias
- the alias to be registered for the bean
BeanDefinitionStoreException
- if the alias is already in use
BeansException
|
The Spring Framework | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |