Skip to content

Commit 1080c14

Browse files
committed
Polish ApplicationContextAwareProcessor
1 parent f726e80 commit 1080c14

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

spring-context/src/main/java/org/springframework/context/support/ApplicationContextAwareProcessor.java

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,12 +32,15 @@
3232
import org.springframework.util.StringValueResolver;
3333

3434
/**
35-
* {@link BeanPostProcessor} implementation that supplies the {@code ApplicationContext},
36-
* {@link org.springframework.core.env.Environment Environment}, or
37-
* {@link StringValueResolver} for the {@code ApplicationContext} to beans that
38-
* implement the {@link EnvironmentAware}, {@link EmbeddedValueResolverAware},
39-
* {@link ResourceLoaderAware}, {@link ApplicationEventPublisherAware},
40-
* {@link MessageSourceAware}, and/or {@link ApplicationContextAware} interfaces.
35+
* {@link BeanPostProcessor} implementation that supplies the
36+
* {@link org.springframework.context.ApplicationContext ApplicationContext},
37+
* {@link org.springframework.core.env.Environment Environment},
38+
* {@link StringValueResolver}, or
39+
* {@link org.springframework.core.metrics.ApplicationStartup ApplicationStartup}
40+
* for the {@code ApplicationContext} to beans that implement the {@link EnvironmentAware},
41+
* {@link EmbeddedValueResolverAware}, {@link ResourceLoaderAware},
42+
* {@link ApplicationEventPublisherAware}, {@link MessageSourceAware},
43+
* {@link ApplicationStartupAware}, and/or {@link ApplicationContextAware} interfaces.
4144
*
4245
* <p>Implemented interfaces are satisfied in the order in which they are
4346
* mentioned above.
@@ -55,6 +58,7 @@
5558
* @see org.springframework.context.ResourceLoaderAware
5659
* @see org.springframework.context.ApplicationEventPublisherAware
5760
* @see org.springframework.context.MessageSourceAware
61+
* @see org.springframework.context.ApplicationStartupAware
5862
* @see org.springframework.context.ApplicationContextAware
5963
* @see org.springframework.context.support.AbstractApplicationContext#refresh()
6064
*/
@@ -77,10 +81,10 @@ public ApplicationContextAwareProcessor(ConfigurableApplicationContext applicati
7781
@Override
7882
@Nullable
7983
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
80-
if (!(bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware ||
84+
if (!((bean instanceof Aware) && (bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware ||
8185
bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware ||
82-
bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware ||
83-
bean instanceof ApplicationStartupAware)) {
86+
bean instanceof MessageSourceAware || bean instanceof ApplicationStartupAware ||
87+
bean instanceof ApplicationContextAware))) {
8488
return bean;
8589
}
8690

@@ -89,28 +93,26 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) thro
8993
}
9094

9195
private void invokeAwareInterfaces(Object bean) {
92-
if (bean instanceof Aware) {
93-
if (bean instanceof EnvironmentAware environmentAware) {
94-
environmentAware.setEnvironment(this.applicationContext.getEnvironment());
95-
}
96-
if (bean instanceof EmbeddedValueResolverAware embeddedValueResolverAware) {
97-
embeddedValueResolverAware.setEmbeddedValueResolver(this.embeddedValueResolver);
98-
}
99-
if (bean instanceof ResourceLoaderAware resourceLoaderAware) {
100-
resourceLoaderAware.setResourceLoader(this.applicationContext);
101-
}
102-
if (bean instanceof ApplicationEventPublisherAware applicationEventPublisherAware) {
103-
applicationEventPublisherAware.setApplicationEventPublisher(this.applicationContext);
104-
}
105-
if (bean instanceof MessageSourceAware messageSourceAware) {
106-
messageSourceAware.setMessageSource(this.applicationContext);
107-
}
108-
if (bean instanceof ApplicationStartupAware applicationStartupAware) {
109-
applicationStartupAware.setApplicationStartup(this.applicationContext.getApplicationStartup());
110-
}
111-
if (bean instanceof ApplicationContextAware applicationContextAware) {
112-
applicationContextAware.setApplicationContext(this.applicationContext);
113-
}
96+
if (bean instanceof EnvironmentAware environmentAware) {
97+
environmentAware.setEnvironment(this.applicationContext.getEnvironment());
98+
}
99+
if (bean instanceof EmbeddedValueResolverAware embeddedValueResolverAware) {
100+
embeddedValueResolverAware.setEmbeddedValueResolver(this.embeddedValueResolver);
101+
}
102+
if (bean instanceof ResourceLoaderAware resourceLoaderAware) {
103+
resourceLoaderAware.setResourceLoader(this.applicationContext);
104+
}
105+
if (bean instanceof ApplicationEventPublisherAware applicationEventPublisherAware) {
106+
applicationEventPublisherAware.setApplicationEventPublisher(this.applicationContext);
107+
}
108+
if (bean instanceof MessageSourceAware messageSourceAware) {
109+
messageSourceAware.setMessageSource(this.applicationContext);
110+
}
111+
if (bean instanceof ApplicationStartupAware applicationStartupAware) {
112+
applicationStartupAware.setApplicationStartup(this.applicationContext.getApplicationStartup());
113+
}
114+
if (bean instanceof ApplicationContextAware applicationContextAware) {
115+
applicationContextAware.setApplicationContext(this.applicationContext);
114116
}
115117
}
116118

0 commit comments

Comments
 (0)