|
|||||||||||
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
Partial implementation of ApplicationContext. Doesn't mandate the type of storage used for configuration, but implements common functionality. Uses the Template Method design pattern, requiring concrete subclasses to implement abstract methods.
In contrast to a plain bean factory, an ApplicationContext is supposed to detect special beans defined in its bean factory: Therefore, this class automatically registers BeanFactoryPostProcessors, BeanPostProcessors and ApplicationListeners that are defined as beans in the context.
A MessageSource may be also supplied as a bean in the context, with the name "messageSource". Else, message resolution is delegated to the parent context.
Implements resource loading through extending DefaultResourceLoader. Therefore, treats resource paths as class path resources. Only supports full classpath resource names that include the package path, like "mypackage/myresource.dat".
refreshBeanFactory()
,
getBeanFactory()
,
MESSAGE_SOURCE_BEAN_NAME
Field Summary | |
static String |
APPLICATION_EVENT_MULTICASTER_BEAN_NAME
Name of the ApplicationEventMulticaster bean in the factory. |
protected Log |
logger
Logger used by this class. |
static String |
MESSAGE_SOURCE_BEAN_NAME
Name of the MessageSource bean in the factory. |
Fields inherited from interface org.springframework.beans.factory.BeanFactory |
FACTORY_BEAN_PREFIX |
Fields inherited from interface org.springframework.core.io.ResourceLoader |
CLASSPATH_URL_PREFIX |
Constructor Summary | |
AbstractApplicationContext()
Create a new AbstractApplicationContext with no parent. |
|
AbstractApplicationContext(ApplicationContext parent)
Create a new AbstractApplicationContext with the given parent context. |
Method Summary | |
void |
addBeanFactoryPostProcessor(BeanFactoryPostProcessor beanFactoryPostProcessor)
Add a new BeanFactoryPostProcessor that will get applied to the internal bean factory of this application context on refresh, before any of the bean definitions get evaluated. |
protected void |
addListener(ApplicationListener listener)
Subclasses can invoke this method to register a listener. |
void |
close()
Destroy the singletons in the bean factory of this application context. |
boolean |
containsBean(String name)
Does this bean factory contain a bean definition with the given name? |
boolean |
containsBeanDefinition(String name)
Check if this bean factory contains a bean definition with the given name. |
String[] |
getAliases(String name)
Return the aliases for the given bean name, if defined. |
Object |
getBean(String name)
Return an instance, which may be shared or independent, of the given bean name. |
Object |
getBean(String name,
Class requiredType)
Return an instance (possibly shared or independent) of the given bean name. |
int |
getBeanDefinitionCount()
Return the number of beans defined in the factory. |
String[] |
getBeanDefinitionNames()
Return the names of all beans defined in this factory. |
String[] |
getBeanDefinitionNames(Class type)
Return the names of beans matching the given type (including subclasses), judging from the bean definitions. |
abstract ConfigurableListableBeanFactory |
getBeanFactory()
Subclasses must return their internal bean factory here. |
List |
getBeanFactoryPostProcessors()
Return the list of BeanPostProcessors that will get applied to beans created with this factory. |
Map |
getBeansOfType(Class type)
Return the bean instances that match the given object type (including subclasses), judging from either bean definitions or the value of getObjectType() in the case of FactoryBeans. |
Map |
getBeansOfType(Class type,
boolean includePrototypes,
boolean includeFactoryBeans)
Return the bean instances that match the given object type (including subclasses), judging from either bean definitions or the value of getObjectType() in the case of FactoryBeans. |
String |
getDisplayName()
Return a friendly name for this context. |
protected BeanFactory |
getInternalParentBeanFactory()
Return the internal bean factory of the parent context if it implements ConfigurableApplicationContext; else, return the parent context itself. |
protected MessageSource |
getInternalParentMessageSource()
Return the internal message source of the parent context if it is an AbstractApplicationContext too; else, return the parent context itself. |
String |
getMessage(MessageSourceResolvable resolvable,
Locale locale)
Try to resolve the message using all the attributes contained within the MessageSourceResolvable argument that was passed in.
|
String |
getMessage(String code,
Object[] args,
Locale locale)
Try to resolve the message. |
String |
getMessage(String code,
Object[] args,
String defaultMessage,
Locale locale)
Try to resolve the message. |
ApplicationContext |
getParent()
Return the parent context, or null if there is no parent (that is, this context is the root of the context hierarchy). |
BeanFactory |
getParentBeanFactory()
Return the parent bean factory, or null if there is none. |
protected ResourcePatternResolver |
getResourcePatternResolver()
Return the ResourcePatternResolver to use for resolving location patterns into Resource instances. |
long |
getStartupDate()
Return the timestamp (ms) when this context was first loaded. |
Class |
getType(String name)
Determine the type of the bean with the given name. |
boolean |
isSingleton(String name)
Is this bean a singleton? |
protected void |
onRefresh()
Template method which can be overridden to add context-specific refresh work. |
protected void |
postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
Modify the application context's internal bean factory after its standard initialization. |
void |
publishEvent(ApplicationEvent event)
Publish the given event to all listeners. |
void |
refresh()
Load respectively refresh the persistent representation of the configuration, which might an XML file, properties file, or relational database schema. |
protected abstract void |
refreshBeanFactory()
Subclasses must implement this method to perform the actual configuration load. |
protected void |
setDisplayName(String displayName)
Set a friendly name for this context. |
void |
setParent(ApplicationContext parent)
Set the parent of this application context. |
String |
toString()
Return information about this context. |
Methods inherited from class org.springframework.core.io.DefaultResourceLoader |
getClassLoader, getResource, getResourceByPath |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Methods inherited from interface org.springframework.core.io.ResourceLoader |
getResource |
Field Detail |
public static final String MESSAGE_SOURCE_BEAN_NAME
MessageSource
,
Constant Field Valuespublic static final String APPLICATION_EVENT_MULTICASTER_BEAN_NAME
ApplicationEventMulticaster
,
SimpleApplicationEventMulticaster
,
Constant Field Valuesprotected final Log logger
Constructor Detail |
public AbstractApplicationContext()
public AbstractApplicationContext(ApplicationContext parent)
parent
- the parent contextMethod Detail |
public ApplicationContext getParent()
getParent
in interface ApplicationContext
protected void setDisplayName(String displayName)
public String getDisplayName()
getDisplayName
in interface ApplicationContext
public long getStartupDate()
getStartupDate
in interface ApplicationContext
public void publishEvent(ApplicationEvent event)
Note: Listeners get initialized after the MessageSource, to be able to access it within listener implementations. Thus, MessageSource implementation cannot publish events.
publishEvent
in interface ApplicationContext
event
- event to publish (may be application-specific or a
standard framework event)RequestHandledEvent
public void setParent(ApplicationContext parent)
ConfigurableApplicationContext
Note that the parent shouldn't be changed: It should only be set outside a constructor if it isn't available when an object of this class is created, for example in case of WebApplicationContext setup.
setParent
in interface ConfigurableApplicationContext
parent
- the parent contextConfigurableWebApplicationContext
public void addBeanFactoryPostProcessor(BeanFactoryPostProcessor beanFactoryPostProcessor)
ConfigurableApplicationContext
addBeanFactoryPostProcessor
in interface ConfigurableApplicationContext
beanFactoryPostProcessor
- the factory processor to registerpublic List getBeanFactoryPostProcessors()
public void refresh() throws BeansException, IllegalStateException
ConfigurableApplicationContext
refresh
in interface ConfigurableApplicationContext
IllegalStateException
- if already initialized and multiple refresh
attempts are not supported
BeansException
- if the bean factory could not be initializedprotected ResourcePatternResolver getResourcePatternResolver()
Can be overridden in subclasses, for extended resolution strategies.
PathMatchingResourcePatternResolver
protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException
beanFactory
- the bean factory used by the application context
BeansException
- in case of errorsprotected void onRefresh() throws BeansException
BeansException
- in case of errors during refreshrefresh()
protected void addListener(ApplicationListener listener)
listener
- the listener to registerpublic void close()
close
in interface ConfigurableApplicationContext
public Object getBean(String name) throws BeansException
BeanFactory
Callers may retain references to returned objects in the case of Singleton beans.
This method delegates to the parent factory if the bean cannot be found in this factory instance.
getBean
in interface BeanFactory
name
- the name of the bean to return
BeansException
- if the bean could not be obtainedpublic Object getBean(String name, Class requiredType) throws BeansException
BeanFactory
Behaves the same as getBean(String), but provides a measure of type safety by throwing a Spring BeansException if the bean is not of the required type. This means that ClassCastException can't be thrown on casting the result correctly, as can happen with getBean(String).
getBean
in interface BeanFactory
name
- the name of the bean to returnrequiredType
- type the bean must match. Can be an interface or superclass
of the actual class, or null for any match. For example, if the value is
Object.class, this method will succeed whatever the class of the returned instance.
BeansException
- if the bean could not be createdpublic boolean containsBean(String name)
BeanFactory
Will ask the parent factory if the bean cannot be found in this factory instance.
containsBean
in interface BeanFactory
name
- the name of the bean to query
public boolean isSingleton(String name) throws NoSuchBeanDefinitionException
BeanFactory
Will ask the parent factory if the bean cannot be found in this factory instance.
isSingleton
in interface BeanFactory
name
- the name of the bean to query
NoSuchBeanDefinitionException
- if there is no bean with the given namepublic Class getType(String name) throws NoSuchBeanDefinitionException
BeanFactory
getType
in interface BeanFactory
name
- the name of the bean to query
NoSuchBeanDefinitionException
- if there is no bean with the given nameBeanFactory.getBean(java.lang.String)
,
FactoryBean.getObjectType()
public String[] getAliases(String name) throws NoSuchBeanDefinitionException
BeanFactory
Will ask the parent factory if the bean cannot be found in this factory instance.
getAliases
in interface BeanFactory
name
- the bean name to check for aliases
NoSuchBeanDefinitionException
- if there's no such bean definitionpublic int getBeanDefinitionCount()
ListableBeanFactory
Note: Ignores any singleton beans that have been registered by other means than bean definitions.
getBeanDefinitionCount
in interface ListableBeanFactory
BeanFactoryUtils.countBeansIncludingAncestors(org.springframework.beans.factory.ListableBeanFactory)
public String[] getBeanDefinitionNames()
ListableBeanFactory
Note: Ignores any singleton beans that have been registered by other means than bean definitions.
getBeanDefinitionNames
in interface ListableBeanFactory
BeanFactoryUtils.beanNamesIncludingAncestors(ListableBeanFactory)
public String[] getBeanDefinitionNames(Class type)
ListableBeanFactory
Note: Ignores any singleton beans that have been registered by other means than bean definitions.
getBeanDefinitionNames
in interface ListableBeanFactory
type
- the class or interface to match, or null for all bean names
BeanFactoryUtils.beanNamesIncludingAncestors(ListableBeanFactory, Class)
public boolean containsBeanDefinition(String name)
ListableBeanFactory
Note: Ignores any singleton beans that have been registered by other means than bean definitions.
containsBeanDefinition
in interface ListableBeanFactory
name
- the name of the bean to look for
BeanFactory.containsBean(java.lang.String)
public Map getBeansOfType(Class type) throws BeansException
ListableBeanFactory
If FactoryBean's getObjectType() returns null and the bean is a singleton, the type of the actually created objects should be evaluated. Prototypes without explicit object type specification should be ignored.
Note: Does not ignore singleton beans that have been registered by other means than bean definitions.
This version of getBeansOfType matches all kinds of beans, be it
singletons, prototypes, or FactoryBeans. In most implementations, the
result will be the same as for getBeansOfType(type, true, true)
.
getBeansOfType
in interface ListableBeanFactory
type
- the class or interface to match, or null for all concrete beans
BeansException
- if the beans could not be createdFactoryBean.getObjectType()
,
BeanFactoryUtils.beansOfTypeIncludingAncestors(ListableBeanFactory, Class)
public Map getBeansOfType(Class type, boolean includePrototypes, boolean includeFactoryBeans) throws BeansException
ListableBeanFactory
If FactoryBean's getObjectType() returns null and the bean is a singleton, the type of the actually created objects should be evaluated. Prototypes without explicit object type specification should be ignored.
Note: Does not ignore singleton beans that have been registered by other means than bean definitions.
getBeansOfType
in interface ListableBeanFactory
type
- the class or interface to match, or null for all concrete beansincludePrototypes
- whether to include prototype beans too
or just singletons (also applies to FactoryBeans)includeFactoryBeans
- whether to include FactoryBeans too
or just conventional beans
BeansException
- if the beans could not be createdFactoryBean.getObjectType()
,
BeanFactoryUtils.beansOfTypeIncludingAncestors(ListableBeanFactory, Class, boolean, boolean)
public BeanFactory getParentBeanFactory()
HierarchicalBeanFactory
getParentBeanFactory
in interface HierarchicalBeanFactory
protected BeanFactory getInternalParentBeanFactory()
ConfigurableApplicationContext.getBeanFactory()
public String getMessage(String code, Object[] args, String defaultMessage, Locale locale)
MessageSource
getMessage
in interface MessageSource
code
- the code to lookup up, such as 'calculator.noRateSet'. Users of
this class are encouraged to base message names on the relevant fully
qualified class name, thus avoiding conflict and ensuring maximum clarity.args
- array of arguments that will be filled in for params within
the message (params look like "{0}", "{1,date}", "{2,time}" within a message),
or null if none.locale
- the Locale in which to do the lookupdefaultMessage
- String to return if the lookup fails
public String getMessage(String code, Object[] args, Locale locale) throws NoSuchMessageException
MessageSource
getMessage
in interface MessageSource
code
- the code to lookup up, such as 'calculator.noRateSet'args
- Array of arguments that will be filled in for params within
the message (params look like "{0}", "{1,date}", "{2,time}" within a message),
or null if none.locale
- the Locale in which to do the lookup
NoSuchMessageException
- if the message wasn't foundpublic String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException
MessageSource
MessageSourceResolvable
argument that was passed in.
NOTE: We must throw a NoSuchMessageException
on this method
since at the time of calling this method we aren't able to determine if the
defaultMessage
property of the resolvable is null or not.
getMessage
in interface MessageSource
resolvable
- value object storing attributes required to properly resolve a messagelocale
- the Locale in which to do the lookup
NoSuchMessageException
- if the message wasn't foundprotected MessageSource getInternalParentMessageSource()
protected abstract void refreshBeanFactory() throws BeansException, IllegalStateException
A subclass will either create a new bean factory and hold a reference to it, or return a single bean factory instance that it holds. In the latter case, it will usually throw an IllegalStateException if refreshing the context more than once.
BeansException
- if initialization of the bean factory failed
IllegalStateException
- if already initialized and multiple refresh
attempts are not supportedrefresh()
public abstract ConfigurableListableBeanFactory getBeanFactory() throws IllegalStateException
getBeanFactory
in interface ConfigurableApplicationContext
IllegalStateException
- if the context does not hold an internal
bean factory yet (usually if refresh
has never been called)refresh()
public String toString()
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |