相关源码注释
ApplicationContext
Spring 5 DefaultResourceLoader 源码注释 Spring 5 AbstractApplicationContext 源码注释
BeanFactory
Spring 5 SimpleAliasRegistry 源码注释 Spring 5 DefaultSingletonBeanRegistry 源码注释 Spring 5 FactoryBeanRegistrySupport 源码注释 Spring 5 AbstractBeanFactory 源码注释 Spring 5 AbstractAutowireCapableBeanFactory 源码注释 Spring 5 DefaultLisbaleBeanFactory 源码注释
UML类图
源码
public abstract class AbstractApplicationContext extends DefaultResourceLoader
implements ConfigurableApplicationContext {
public static final String MESSAGE_SOURCE_BEAN_NAME
= "messageSource";
public static final String LIFECYCLE_PROCESSOR_BEAN_NAME
= "lifecycleProcessor";
public static final String APPLICATION_EVENT_MULTICASTER_BEAN_NAME
= "applicationEventMulticaster";
static {
ContextClosedEvent
.class.getName();
}
protected final Log logger
= LogFactory
.getLog(getClass());
private String id
= ObjectUtils
.identityToString(this);
private String displayName
= ObjectUtils
.identityToString(this);
@Nullable
private ApplicationContext parent
;
@Nullable
private ConfigurableEnvironment environment
;
private final List
<BeanFactoryPostProcessor> beanFactoryPostProcessors
= new ArrayList<>();
private long startupDate
;
private final AtomicBoolean active
= new AtomicBoolean();
private final AtomicBoolean closed
= new AtomicBoolean();
private final Object startupShutdownMonitor
= new Object();
@Nullable
private Thread shutdownHook
;
private ResourcePatternResolver resourcePatternResolver
;
@Nullable
private LifecycleProcessor lifecycleProcessor
;
@Nullable
private MessageSource messageSource
;
@Nullable
private ApplicationEventMulticaster applicationEventMulticaster
;
private final Set
<ApplicationListener
<?>> applicationListeners
= new LinkedHashSet<>();
@Nullable
private Set
<ApplicationListener
<?>> earlyApplicationListeners
;
@Nullable
private Set
<ApplicationEvent> earlyApplicationEvents
;
public AbstractApplicationContext() {
this.resourcePatternResolver
= getResourcePatternResolver();
}
public AbstractApplicationContext(@Nullable ApplicationContext parent
) {
this();
setParent(parent
);
}
@Override
public void setId(String id
) {
this.id
= id
;
}
@Override
public String
getId() {
return this.id
;
}
@Override
public String
getApplicationName() {
return "";
}
public void setDisplayName(String displayName
) {
Assert
.hasLength(displayName
, "Display name must not be empty");
this.displayName
= displayName
;
}
@Override
public String
getDisplayName() {
return this.displayName
;
}
@Override
@Nullable
public ApplicationContext
getParent() {
return this.parent
;
}
@Override
public void setEnvironment(ConfigurableEnvironment environment
) {
this.environment
= environment
;
}
@Override
public ConfigurableEnvironment
getEnvironment() {
if (this.environment
== null
) {
this.environment
= createEnvironment();
}
return this.environment
;
}
protected ConfigurableEnvironment
createEnvironment() {
return new StandardEnvironment();
}
@Override
public AutowireCapableBeanFactory
getAutowireCapableBeanFactory() throws IllegalStateException
{
return getBeanFactory();
}
@Override
public long getStartupDate() {
return this.startupDate
;
}
@Override
public void publishEvent(ApplicationEvent event
) {
publishEvent(event
, null
);
}
@Override
public void publishEvent(Object event
) {
publishEvent(event
, null
);
}
protected void publishEvent(Object event
, @Nullable ResolvableType eventType
) {
Assert
.notNull(event
, "Event must not be null");
ApplicationEvent applicationEvent
;
if (event
instanceof ApplicationEvent) {
applicationEvent
= (ApplicationEvent
) event
;
}
else {
applicationEvent
= new PayloadApplicationEvent<>(this, event
);
if (eventType
== null
) {
eventType
= ((PayloadApplicationEvent
<?>) applicationEvent
).getResolvableType();
}
}
if (this.earlyApplicationEvents
!= null
) {
this.earlyApplicationEvents
.add(applicationEvent
);
}
else {
getApplicationEventMulticaster().multicastEvent(applicationEvent
, eventType
);
}
if (this.parent
!= null
) {
if (this.parent
instanceof AbstractApplicationContext) {
((AbstractApplicationContext
) this.parent
).publishEvent(event
, eventType
);
}
else {
this.parent
.publishEvent(event
);
}
}
}
ApplicationEventMulticaster
getApplicationEventMulticaster() throws IllegalStateException
{
if (this.applicationEventMulticaster
== null
) {
throw new IllegalStateException("ApplicationEventMulticaster not initialized - " +
"call 'refresh' before multicasting events via the context: " + this);
}
return this.applicationEventMulticaster
;
}
LifecycleProcessor
getLifecycleProcessor() throws IllegalStateException
{
if (this.lifecycleProcessor
== null
) {
throw new IllegalStateException("LifecycleProcessor not initialized - " +
"call 'refresh' before invoking lifecycle methods via the context: " + this);
}
return this.lifecycleProcessor
;
}
protected ResourcePatternResolver
getResourcePatternResolver() {
return new PathMatchingResourcePatternResolver(this);
}
@Override
public void setParent(@Nullable ApplicationContext parent
) {
this.parent
= parent
;
if (parent
!= null
) {
Environment parentEnvironment
= parent
.getEnvironment();
if (parentEnvironment
instanceof ConfigurableEnvironment) {
getEnvironment().merge((ConfigurableEnvironment
) parentEnvironment
);
}
}
}
@Override
public void addBeanFactoryPostProcessor(BeanFactoryPostProcessor postProcessor
) {
Assert
.notNull(postProcessor
, "BeanFactoryPostProcessor must not be null");
this.beanFactoryPostProcessors
.add(postProcessor
);
}
public List
<BeanFactoryPostProcessor> getBeanFactoryPostProcessors() {
return this.beanFactoryPostProcessors
;
}
@Override
public void addApplicationListener(ApplicationListener
<?> listener
) {
Assert
.notNull(listener
, "ApplicationListener must not be null");
if (this.applicationEventMulticaster
!= null
) {
this.applicationEventMulticaster
.addApplicationListener(listener
);
}
this.applicationListeners
.add(listener
);
}
public Collection
<ApplicationListener
<?>> getApplicationListeners() {
return this.applicationListeners
;
}
@Override
public void refresh() throws BeansException
, IllegalStateException
{
synchronized (this.startupShutdownMonitor
) {
prepareRefresh();
ConfigurableListableBeanFactory beanFactory
= obtainFreshBeanFactory();
prepareBeanFactory(beanFactory
);
try {
postProcessBeanFactory(beanFactory
);
invokeBeanFactoryPostProcessors(beanFactory
);
registerBeanPostProcessors(beanFactory
);
initMessageSource();
initApplicationEventMulticaster();
onRefresh();
registerListeners();
finishBeanFactoryInitialization(beanFactory
);
finishRefresh();
}
catch (BeansException ex
) {
if (logger
.isWarnEnabled()) {
logger
.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex
);
}
destroyBeans();
cancelRefresh(ex
);
throw ex
;
}
finally {
resetCommonCaches();
}
}
}
protected void prepareRefresh() {
this.startupDate
= System
.currentTimeMillis();
this.closed
.set(false);
this.active
.set(true);
if (logger
.isDebugEnabled()) {
if (logger
.isTraceEnabled()) {
logger
.trace("Refreshing " + this);
}
else {
logger
.debug("Refreshing " + getDisplayName());
}
}
initPropertySources();
getEnvironment().validateRequiredProperties();
if (this.earlyApplicationListeners
== null
) {
this.earlyApplicationListeners
= new LinkedHashSet<>(this.applicationListeners
);
}
else {
this.applicationListeners
.clear();
this.applicationListeners
.addAll(this.earlyApplicationListeners
);
}
this.earlyApplicationEvents
= new LinkedHashSet<>();
}
protected void initPropertySources() {
}
protected ConfigurableListableBeanFactory
obtainFreshBeanFactory() {
refreshBeanFactory();
return getBeanFactory();
}
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory
) {
beanFactory
.setBeanClassLoader(getClassLoader());
beanFactory
.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory
.getBeanClassLoader()));
beanFactory
.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
beanFactory
.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
beanFactory
.ignoreDependencyInterface(EnvironmentAware
.class);
beanFactory
.ignoreDependencyInterface(EmbeddedValueResolverAware
.class);
beanFactory
.ignoreDependencyInterface(ResourceLoaderAware
.class);
beanFactory
.ignoreDependencyInterface(ApplicationEventPublisherAware
.class);
beanFactory
.ignoreDependencyInterface(MessageSourceAware
.class);
beanFactory
.ignoreDependencyInterface(ApplicationContextAware
.class);
beanFactory
.registerResolvableDependency(BeanFactory
.class, beanFactory
);
beanFactory
.registerResolvableDependency(ResourceLoader
.class, this);
beanFactory
.registerResolvableDependency(ApplicationEventPublisher
.class, this);
beanFactory
.registerResolvableDependency(ApplicationContext
.class, this);
beanFactory
.addBeanPostProcessor(new ApplicationListenerDetector(this));
if (beanFactory
.containsBean(LOAD_TIME_WEAVER_BEAN_NAME
)) {
beanFactory
.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory
));
beanFactory
.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory
.getBeanClassLoader()));
}
if (!beanFactory
.containsLocalBean(ENVIRONMENT_BEAN_NAME
)) {
beanFactory
.registerSingleton(ENVIRONMENT_BEAN_NAME
, getEnvironment());
}
if (!beanFactory
.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME
)) {
beanFactory
.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME
, getEnvironment().getSystemProperties());
}
if (!beanFactory
.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME
)) {
beanFactory
.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME
, getEnvironment().getSystemEnvironment());
}
}
protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory
) {
}
protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory
) {
PostProcessorRegistrationDelegate
.invokeBeanFactoryPostProcessors(beanFactory
, getBeanFactoryPostProcessors());
if (beanFactory
.getTempClassLoader() == null
&& beanFactory
.containsBean(LOAD_TIME_WEAVER_BEAN_NAME
)) {
beanFactory
.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory
));
beanFactory
.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory
.getBeanClassLoader()));
}
}
protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory
) {
PostProcessorRegistrationDelegate
.registerBeanPostProcessors(beanFactory
, this);
}
protected void initMessageSource() {
ConfigurableListableBeanFactory beanFactory
= getBeanFactory();
if (beanFactory
.containsLocalBean(MESSAGE_SOURCE_BEAN_NAME
)) {
this.messageSource
= beanFactory
.getBean(MESSAGE_SOURCE_BEAN_NAME
, MessageSource
.class);
if (this.parent
!= null
&& this.messageSource
instanceof HierarchicalMessageSource) {
HierarchicalMessageSource hms
= (HierarchicalMessageSource
) this.messageSource
;
if (hms
.getParentMessageSource() == null
) {
hms
.setParentMessageSource(getInternalParentMessageSource());
}
}
if (logger
.isTraceEnabled()) {
logger
.trace("Using MessageSource [" + this.messageSource
+ "]");
}
}
else {
DelegatingMessageSource dms
= new DelegatingMessageSource();
dms
.setParentMessageSource(getInternalParentMessageSource());
this.messageSource
= dms
;
beanFactory
.registerSingleton(MESSAGE_SOURCE_BEAN_NAME
, this.messageSource
);
if (logger
.isTraceEnabled()) {
logger
.trace("No '" + MESSAGE_SOURCE_BEAN_NAME
+ "' bean, using [" + this.messageSource
+ "]");
}
}
}
protected void initApplicationEventMulticaster() {
ConfigurableListableBeanFactory beanFactory
= getBeanFactory();
if (beanFactory
.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME
)) {
this.applicationEventMulticaster
=
beanFactory
.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME
, ApplicationEventMulticaster
.class);
if (logger
.isTraceEnabled()) {
logger
.trace("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster
+ "]");
}
}
else {
this.applicationEventMulticaster
= new SimpleApplicationEventMulticaster(beanFactory
);
beanFactory
.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME
, this.applicationEventMulticaster
);
if (logger
.isTraceEnabled()) {
logger
.trace("No '" + APPLICATION_EVENT_MULTICASTER_BEAN_NAME
+ "' bean, using " +
"[" + this.applicationEventMulticaster
.getClass().getSimpleName() + "]");
}
}
}
protected void initLifecycleProcessor() {
ConfigurableListableBeanFactory beanFactory
= getBeanFactory();
if (beanFactory
.containsLocalBean(LIFECYCLE_PROCESSOR_BEAN_NAME
)) {
this.lifecycleProcessor
=
beanFactory
.getBean(LIFECYCLE_PROCESSOR_BEAN_NAME
, LifecycleProcessor
.class);
if (logger
.isTraceEnabled()) {
logger
.trace("Using LifecycleProcessor [" + this.lifecycleProcessor
+ "]");
}
}
else {
DefaultLifecycleProcessor defaultProcessor
= new DefaultLifecycleProcessor();
defaultProcessor
.setBeanFactory(beanFactory
);
this.lifecycleProcessor
= defaultProcessor
;
beanFactory
.registerSingleton(LIFECYCLE_PROCESSOR_BEAN_NAME
, this.lifecycleProcessor
);
if (logger
.isTraceEnabled()) {
logger
.trace("No '" + LIFECYCLE_PROCESSOR_BEAN_NAME
+ "' bean, using " +
"[" + this.lifecycleProcessor
.getClass().getSimpleName() + "]");
}
}
}
protected void onRefresh() throws BeansException
{
}
protected void registerListeners() {
for (ApplicationListener
<?> listener
: getApplicationListeners()) {
getApplicationEventMulticaster().addApplicationListener(listener
);
}
String
[] listenerBeanNames
= getBeanNamesForType(ApplicationListener
.class, true, false);
for (String listenerBeanName
: listenerBeanNames
) {
getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName
);
}
Set
<ApplicationEvent> earlyEventsToProcess
= this.earlyApplicationEvents
;
this.earlyApplicationEvents
= null
;
if (earlyEventsToProcess
!= null
) {
for (ApplicationEvent earlyEvent
: earlyEventsToProcess
) {
getApplicationEventMulticaster().multicastEvent(earlyEvent
);
}
}
}
protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory
) {
if (beanFactory
.containsBean(CONVERSION_SERVICE_BEAN_NAME
) &&
beanFactory
.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME
, ConversionService
.class)) {
beanFactory
.setConversionService(
beanFactory
.getBean(CONVERSION_SERVICE_BEAN_NAME
, ConversionService
.class));
}
if (!beanFactory
.hasEmbeddedValueResolver()) {
beanFactory
.addEmbeddedValueResolver(strVal
->
getEnvironment().resolvePlaceholders(strVal
));
}
String
[] weaverAwareNames
= beanFactory
.getBeanNamesForType(LoadTimeWeaverAware
.class, false, false);
for (String weaverAwareName
: weaverAwareNames
) {
getBean(weaverAwareName
);
}
beanFactory
.setTempClassLoader(null
);
beanFactory
.freezeConfiguration();
beanFactory
.preInstantiateSingletons();
}
protected void finishRefresh() {
clearResourceCaches();
initLifecycleProcessor();
getLifecycleProcessor().onRefresh();
publishEvent(new ContextRefreshedEvent(this));
LiveBeansView
.registerApplicationContext(this);
}
protected void cancelRefresh(BeansException ex
) {
this.active
.set(false);
}
protected void resetCommonCaches() {
ReflectionUtils
.clearCache();
AnnotationUtils
.clearCache();
ResolvableType
.clearCache();
CachedIntrospectionResults
.clearClassLoader(getClassLoader());
}
@Override
public void registerShutdownHook() {
if (this.shutdownHook
== null
) {
this.shutdownHook
= new Thread(SHUTDOWN_HOOK_THREAD_NAME
) {
@Override
public void run() {
synchronized (startupShutdownMonitor
) {
doClose();
}
}
};
Runtime
.getRuntime().addShutdownHook(this.shutdownHook
);
}
}
@Deprecated
public void destroy() {
close();
}
@Override
public void close() {
synchronized (this.startupShutdownMonitor
) {
doClose();
if (this.shutdownHook
!= null
) {
try {
Runtime
.getRuntime().removeShutdownHook(this.shutdownHook
);
}
catch (IllegalStateException ex
) {
}
}
}
}
protected void doClose() {
if (this.active
.get() && this.closed
.compareAndSet(false, true)) {
if (logger
.isDebugEnabled()) {
logger
.debug("Closing " + this);
}
LiveBeansView
.unregisterApplicationContext(this);
try {
publishEvent(new ContextClosedEvent(this));
}
catch (Throwable ex
) {
logger
.warn("Exception thrown from ApplicationListener handling ContextClosedEvent", ex
);
}
if (this.lifecycleProcessor
!= null
) {
try {
this.lifecycleProcessor
.onClose();
}
catch (Throwable ex
) {
logger
.warn("Exception thrown from LifecycleProcessor on context close", ex
);
}
}
destroyBeans();
closeBeanFactory();
onClose();
if (this.earlyApplicationListeners
!= null
) {
this.applicationListeners
.clear();
this.applicationListeners
.addAll(this.earlyApplicationListeners
);
}
this.active
.set(false);
}
}
protected void destroyBeans() {
getBeanFactory().destroySingletons();
}
protected void onClose() {
}
@Override
public boolean isActive() {
return this.active
.get();
}
protected void assertBeanFactoryActive() {
if (!this.active
.get()) {
if (this.closed
.get()) {
throw new IllegalStateException(getDisplayName() + " has been closed already");
}
else {
throw new IllegalStateException(getDisplayName() + " has not been refreshed yet");
}
}
}
@Override
public Object
getBean(String name
) throws BeansException
{
assertBeanFactoryActive();
return getBeanFactory().getBean(name
);
}
@Override
public <T> T
getBean(String name
, Class
<T> requiredType
) throws BeansException
{
assertBeanFactoryActive();
return getBeanFactory().getBean(name
, requiredType
);
}
@Override
public Object
getBean(String name
, Object
... args
) throws BeansException
{
assertBeanFactoryActive();
return getBeanFactory().getBean(name
, args
);
}
@Override
public <T> T
getBean(Class
<T> requiredType
) throws BeansException
{
assertBeanFactoryActive();
return getBeanFactory().getBean(requiredType
);
}
@Override
public <T> T
getBean(Class
<T> requiredType
, Object
... args
) throws BeansException
{
assertBeanFactoryActive();
return getBeanFactory().getBean(requiredType
, args
);
}
@Override
public <T> ObjectProvider
<T> getBeanProvider(Class
<T> requiredType
) {
assertBeanFactoryActive();
return getBeanFactory().getBeanProvider(requiredType
);
}
@Override
public <T> ObjectProvider
<T> getBeanProvider(ResolvableType requiredType
) {
assertBeanFactoryActive();
return getBeanFactory().getBeanProvider(requiredType
);
}
@Override
public boolean containsBean(String name
) {
return getBeanFactory().containsBean(name
);
}
@Override
public boolean isSingleton(String name
) throws NoSuchBeanDefinitionException
{
assertBeanFactoryActive();
return getBeanFactory().isSingleton(name
);
}
@Override
public boolean isPrototype(String name
) throws NoSuchBeanDefinitionException
{
assertBeanFactoryActive();
return getBeanFactory().isPrototype(name
);
}
@Override
public boolean isTypeMatch(String name
, ResolvableType typeToMatch
) throws NoSuchBeanDefinitionException
{
assertBeanFactoryActive();
return getBeanFactory().isTypeMatch(name
, typeToMatch
);
}
@Override
public boolean isTypeMatch(String name
, Class
<?> typeToMatch
) throws NoSuchBeanDefinitionException
{
assertBeanFactoryActive();
return getBeanFactory().isTypeMatch(name
, typeToMatch
);
}
@Override
@Nullable
public Class
<?> getType(String name
) throws NoSuchBeanDefinitionException
{
assertBeanFactoryActive();
return getBeanFactory().getType(name
);
}
@Override
@Nullable
public Class
<?> getType(String name
, boolean allowFactoryBeanInit
) throws NoSuchBeanDefinitionException
{
assertBeanFactoryActive();
return getBeanFactory().getType(name
, allowFactoryBeanInit
);
}
@Override
public String
[] getAliases(String name
) {
return getBeanFactory().getAliases(name
);
}
@Override
public boolean containsBeanDefinition(String beanName
) {
return getBeanFactory().containsBeanDefinition(beanName
);
}
@Override
public int getBeanDefinitionCount() {
return getBeanFactory().getBeanDefinitionCount();
}
@Override
public String
[] getBeanDefinitionNames() {
return getBeanFactory().getBeanDefinitionNames();
}
@Override
public String
[] getBeanNamesForType(ResolvableType type
) {
assertBeanFactoryActive();
return getBeanFactory().getBeanNamesForType(type
);
}
@Override
public String
[] getBeanNamesForType(ResolvableType type
, boolean includeNonSingletons
, boolean allowEagerInit
) {
assertBeanFactoryActive();
return getBeanFactory().getBeanNamesForType(type
, includeNonSingletons
, allowEagerInit
);
}
@Override
public String
[] getBeanNamesForType(@Nullable Class
<?> type
) {
assertBeanFactoryActive();
return getBeanFactory().getBeanNamesForType(type
);
}
@Override
public String
[] getBeanNamesForType(@Nullable Class
<?> type
, boolean includeNonSingletons
, boolean allowEagerInit
) {
assertBeanFactoryActive();
return getBeanFactory().getBeanNamesForType(type
, includeNonSingletons
, allowEagerInit
);
}
@Override
public <T> Map
<String, T> getBeansOfType(@Nullable Class
<T> type
) throws BeansException
{
assertBeanFactoryActive();
return getBeanFactory().getBeansOfType(type
);
}
@Override
public <T> Map
<String, T> getBeansOfType(@Nullable Class
<T> type
, boolean includeNonSingletons
, boolean allowEagerInit
)
throws BeansException
{
assertBeanFactoryActive();
return getBeanFactory().getBeansOfType(type
, includeNonSingletons
, allowEagerInit
);
}
@Override
public String
[] getBeanNamesForAnnotation(Class
<? extends Annotation> annotationType
) {
assertBeanFactoryActive();
return getBeanFactory().getBeanNamesForAnnotation(annotationType
);
}
@Override
public Map
<String, Object> getBeansWithAnnotation(Class
<? extends Annotation> annotationType
)
throws BeansException
{
assertBeanFactoryActive();
return getBeanFactory().getBeansWithAnnotation(annotationType
);
}
@Override
@Nullable
public <A
extends Annotation> A
findAnnotationOnBean(String beanName
, Class
<A> annotationType
)
throws NoSuchBeanDefinitionException
{
assertBeanFactoryActive();
return getBeanFactory().findAnnotationOnBean(beanName
, annotationType
);
}
@Override
@Nullable
public BeanFactory
getParentBeanFactory() {
return getParent();
}
@Override
public boolean containsLocalBean(String name
) {
return getBeanFactory().containsLocalBean(name
);
}
@Nullable
protected BeanFactory
getInternalParentBeanFactory() {
return (getParent() instanceof ConfigurableApplicationContext ?
((ConfigurableApplicationContext
) getParent()).getBeanFactory() : getParent());
}
@Override
public String
getMessage(String code
, @Nullable Object
[] args
, @Nullable String defaultMessage
, Locale locale
) {
return getMessageSource().getMessage(code
, args
, defaultMessage
, locale
);
}
@Override
public String
getMessage(String code
, @Nullable Object
[] args
, Locale locale
) throws NoSuchMessageException
{
return getMessageSource().getMessage(code
, args
, locale
);
}
@Override
public String
getMessage(MessageSourceResolvable resolvable
, Locale locale
) throws NoSuchMessageException
{
return getMessageSource().getMessage(resolvable
, locale
);
}
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
;
}
@Nullable
protected MessageSource
getInternalParentMessageSource() {
return (getParent() instanceof AbstractApplicationContext ?
((AbstractApplicationContext
) getParent()).messageSource
: getParent());
}
@Override
public Resource
[] getResources(String locationPattern
) throws IOException
{
return this.resourcePatternResolver
.getResources(locationPattern
);
}
@Override
public void start() {
getLifecycleProcessor().start();
publishEvent(new ContextStartedEvent(this));
}
@Override
public void stop() {
getLifecycleProcessor().stop();
publishEvent(new ContextStoppedEvent(this));
}
@Override
public boolean isRunning() {
return (this.lifecycleProcessor
!= null
&& this.lifecycleProcessor
.isRunning());
}
protected abstract void refreshBeanFactory() throws BeansException
, IllegalStateException
;
protected abstract void closeBeanFactory();
@Override
public abstract ConfigurableListableBeanFactory
getBeanFactory() throws IllegalStateException
;
@Override
public String
toString() {
StringBuilder sb
= new StringBuilder(getDisplayName());
sb
.append(", started on ").append(new Date(getStartupDate()));
ApplicationContext parent
= getParent();
if (parent
!= null
) {
sb
.append(", parent: ").append(parent
.getDisplayName());
}
return sb
.toString();
}
}