📄 abstractapplicationcontext.java
字号:
/**
* Modify the application context's internal bean factory after its standard
* initialization. All bean definitions will have been loaded, but no beans
* will have been instantiated yet. This allows for registering special
* BeanPostProcessors etc in certain ApplicationContext implementations.
* @param beanFactory the bean factory used by the application context
* @throws org.springframework.beans.BeansException in case of errors
*/
protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
}
/**
* Instantiate and invoke all registered BeanFactoryPostProcessor beans,
* respecting explicit order if given.
* Must be called before singleton instantiation.
*/
private void invokeBeanFactoryPostProcessors() throws BeansException {
Map factoryProcessorMap = getBeansOfType(BeanFactoryPostProcessor.class, true, false);
List factoryProcessors = new ArrayList(factoryProcessorMap.values());
Collections.sort(factoryProcessors, new OrderComparator());
for (Iterator it = factoryProcessors.iterator(); it.hasNext();) {
BeanFactoryPostProcessor factoryProcessor = (BeanFactoryPostProcessor) it.next();
factoryProcessor.postProcessBeanFactory(getBeanFactory());
}
}
/**
* Instantiate and invoke all registered BeanPostProcessor beans,
* respecting explicit order if given.
* <p>Must be called before any instantiation of application beans.
*/
private void registerBeanPostProcessors() throws BeansException {
Map beanProcessorMap = getBeansOfType(BeanPostProcessor.class, true, false);
List beanProcessors = new ArrayList(beanProcessorMap.values());
Collections.sort(beanProcessors, new OrderComparator());
for (Iterator it = beanProcessors.iterator(); it.hasNext();) {
getBeanFactory().addBeanPostProcessor((BeanPostProcessor) it.next());
}
}
/**
* Initialize the MessageSource.
* Use parent's if none defined in this context.
*/
private void initMessageSource() throws BeansException {
if (containsLocalBean(MESSAGE_SOURCE_BEAN_NAME)) {
this.messageSource = (MessageSource) getBean(MESSAGE_SOURCE_BEAN_NAME);
// Make MessageSource aware of parent MessageSource.
if (this.parent != null && this.messageSource instanceof HierarchicalMessageSource) {
MessageSource parentMessageSource = getInternalParentMessageSource();
((HierarchicalMessageSource) this.messageSource).setParentMessageSource(parentMessageSource);
}
if (logger.isInfoEnabled()) {
logger.info("Using MessageSource [" + this.messageSource + "]");
}
}
else {
// Use empty MessageSource to be able to accept getMessage calls.
DelegatingMessageSource dms = new DelegatingMessageSource();
dms.setParentMessageSource(getInternalParentMessageSource());
this.messageSource = dms;
if (logger.isInfoEnabled()) {
logger.info("Unable to locate MessageSource with name '" + MESSAGE_SOURCE_BEAN_NAME +
"': using default [" + this.messageSource + "]");
}
}
}
/**
* Initialize the ApplicationEventMulticaster.
* Uses SimpleApplicationEventMulticaster if none defined in the context.
* @see org.springframework.context.event.SimpleApplicationEventMulticaster
*/
private void initApplicationEventMulticaster() throws BeansException {
if (containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {
this.applicationEventMulticaster =
(ApplicationEventMulticaster) getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME);
if (logger.isInfoEnabled()) {
logger.info("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
}
}
else {
this.applicationEventMulticaster = new SimpleApplicationEventMulticaster();
if (logger.isInfoEnabled()) {
logger.info("Unable to locate ApplicationEventMulticaster with name '" +
APPLICATION_EVENT_MULTICASTER_BEAN_NAME +
"': using default [" + this.applicationEventMulticaster + "]");
}
}
}
/**
* Return whether the local bean factory of this context contains a bean
* of the given name, ignoring beans defined in ancestor contexts.
* <p>Needs to check both bean definitions and manually registered singletons.
* We cannot use <code>containsBean</code> here, as we do not want a bean
* from an ancestor bean factory.
*/
private boolean containsLocalBean(String beanName) {
return (containsBeanDefinition(beanName) || getBeanFactory().containsSingleton(beanName));
}
/**
* Template method which can be overridden to add context-specific refresh work.
* Called on initialization of special beans, before instantiation of singletons.
* @throws BeansException in case of errors during refresh
* @see #refresh
*/
protected void onRefresh() throws BeansException {
// for subclasses: do nothing by default
}
/**
* Add beans that implement ApplicationListener as listeners.
* Doesn't affect other listeners, which can be added without being beans.
*/
private void refreshListeners() throws BeansException {
logger.debug("Refreshing listeners");
Collection listeners = getBeansOfType(ApplicationListener.class, true, false).values();
if (logger.isDebugEnabled()) {
logger.debug("Found " + listeners.size() + " listeners in bean factory");
}
for (Iterator it = listeners.iterator(); it.hasNext();) {
ApplicationListener listener = (ApplicationListener) it.next();
addListener(listener);
if (logger.isInfoEnabled()) {
logger.info("Application listener [" + listener + "] added");
}
}
}
/**
* Subclasses can invoke this method to register a listener.
* Any beans in the context that are listeners are automatically added.
* @param listener the listener to register
*/
protected void addListener(ApplicationListener listener) {
getApplicationEventMulticaster().addApplicationListener(listener);
}
/**
* Destroy the singletons in the bean factory of this application context.
*/
public void close() {
if (logger.isInfoEnabled()) {
logger.info("Closing application context [" + getDisplayName() + "]");
}
// publish corresponding event
publishEvent(new ContextClosedEvent(this));
// Destroy all cached singletons in this context,
// invoking DisposableBean.destroy and/or "destroy-method".
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
if (beanFactory != null) {
beanFactory.destroySingletons();
}
}
//---------------------------------------------------------------------
// Implementation of BeanFactory
//---------------------------------------------------------------------
public Object getBean(String name) throws BeansException {
return getBeanFactory().getBean(name);
}
public Object getBean(String name, Class requiredType) throws BeansException {
return getBeanFactory().getBean(name, requiredType);
}
public boolean containsBean(String name) {
return getBeanFactory().containsBean(name);
}
public boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
return getBeanFactory().isSingleton(name);
}
public Class getType(String name) throws NoSuchBeanDefinitionException {
return getBeanFactory().getType(name);
}
public String[] getAliases(String name) throws NoSuchBeanDefinitionException {
return getBeanFactory().getAliases(name);
}
//---------------------------------------------------------------------
// Implementation of ListableBeanFactory
//---------------------------------------------------------------------
public boolean containsBeanDefinition(String name) {
return getBeanFactory().containsBeanDefinition(name);
}
public int getBeanDefinitionCount() {
return getBeanFactory().getBeanDefinitionCount();
}
public String[] getBeanDefinitionNames() {
return getBeanFactory().getBeanDefinitionNames();
}
public String[] getBeanDefinitionNames(Class type) {
return getBeanFactory().getBeanDefinitionNames(type);
}
public String[] getBeanNamesForType(Class type) {
return getBeanFactory().getBeanNamesForType(type);
}
public Map getBeansOfType(Class type) throws BeansException {
return getBeanFactory().getBeansOfType(type);
}
public Map getBeansOfType(Class type, boolean includePrototypes, boolean includeFactoryBeans)
throws BeansException {
return getBeanFactory().getBeansOfType(type, includePrototypes, includeFactoryBeans);
}
//---------------------------------------------------------------------
// Implementation of HierarchicalBeanFactory
//---------------------------------------------------------------------
public BeanFactory getParentBeanFactory() {
return getParent();
}
/**
* Return the internal bean factory of the parent context if it implements
* ConfigurableApplicationContext; else, return the parent context itself.
* @see org.springframework.context.ConfigurableApplicationContext#getBeanFactory
*/
protected BeanFactory getInternalParentBeanFactory() {
return (getParent() instanceof ConfigurableApplicationContext) ?
((ConfigurableApplicationContext) getParent()).getBeanFactory() : (BeanFactory) getParent();
}
//---------------------------------------------------------------------
// Implementation of MessageSource
//---------------------------------------------------------------------
public String getMessage(String code, Object args[], String defaultMessage, Locale locale) {
return getMessageSource().getMessage(code, args, defaultMessage, locale);
}
public String getMessage(String code, Object args[], Locale locale) throws NoSuchMessageException {
return getMessageSource().getMessage(code, args, locale);
}
public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
return getMessageSource().getMessage(resolvable, locale);
}
/**
* Return the internal MessageSource used by the context.
* @return the internal MessageSource (never null)
* @throws IllegalStateException if the context has not been initialized yet
*/
private MessageSource getMessageSource() throws IllegalStateException {
if (this.messageSource == null) {
throw new IllegalStateException("MessageSource not initialized - " +
"call 'refresh' before accessing messages via the context: " + this);
}
return this.messageSource;
}
/**
* Return the internal message source of the parent context if it is an
* AbstractApplicationContext too; else, return the parent context itself.
*/
protected MessageSource getInternalParentMessageSource() {
return (getParent() instanceof AbstractApplicationContext) ?
((AbstractApplicationContext) getParent()).messageSource : getParent();
}
//---------------------------------------------------------------------
// Implementation of ResourcePatternResolver
//---------------------------------------------------------------------
public Resource[] getResources(String locationPattern) throws IOException {
return this.resourcePatternResolver.getResources(locationPattern);
}
//---------------------------------------------------------------------
// Abstract methods that must be implemented by subclasses
//---------------------------------------------------------------------
/**
* Subclasses must implement this method to perform the actual configuration load.
* The method is invoked by refresh before any other initialization work.
* <p>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.
* @throws BeansException if initialization of the bean factory failed
* @throws IllegalStateException if already initialized and multiple refresh
* attempts are not supported
* @see #refresh
*/
protected abstract void refreshBeanFactory() throws BeansException, IllegalStateException;
/**
* Subclasses must return their internal bean factory here.
* They should implement the lookup efficiently, so that it can be called
* repeatedly without a performance penalty.
* @return this application context's internal bean factory
* @throws IllegalStateException if the context does not hold an internal
* bean factory yet (usually if <code>refresh</code> has never been called)
* @see #refresh
*/
public abstract ConfigurableListableBeanFactory getBeanFactory() throws IllegalStateException;
/**
* Return information about this context.
*/
public String toString() {
StringBuffer sb = new StringBuffer(getClass().getName());
sb.append(": ");
sb.append("display name [").append(this.displayName).append("]; ");
sb.append("startup date [").append(new Date(this.startupTime)).append("]; ");
if (this.parent == null) {
sb.append("root of context hierarchy");
}
else {
sb.append("child of [").append(this.parent).append(']');
}
return sb.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -