Skip to content

Commit 09a0615

Browse files
committed
Consistent callbacks for TypeFilters, ImportSelectors and ImportBeanDefinitionRegistrars
Issue: SPR-14686 (cherry picked from commit 0c2e8a6)
1 parent 669d581 commit 09a0615

File tree

4 files changed

+91
-74
lines changed

4 files changed

+91
-74
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/BeanFactory.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,24 +67,27 @@
6767
* 1. BeanNameAware's {@code setBeanName}<br>
6868
* 2. BeanClassLoaderAware's {@code setBeanClassLoader}<br>
6969
* 3. BeanFactoryAware's {@code setBeanFactory}<br>
70-
* 4. ResourceLoaderAware's {@code setResourceLoader}
70+
* 4. EnvironmentAware's {@code setEnvironment}
71+
* 5. EmbeddedValueResolverAware's {@code setEmbeddedValueResolver}
72+
* 6. ResourceLoaderAware's {@code setResourceLoader}
7173
* (only applicable when running in an application context)<br>
72-
* 5. ApplicationEventPublisherAware's {@code setApplicationEventPublisher}
74+
* 7. ApplicationEventPublisherAware's {@code setApplicationEventPublisher}
7375
* (only applicable when running in an application context)<br>
74-
* 6. MessageSourceAware's {@code setMessageSource}
76+
* 8. MessageSourceAware's {@code setMessageSource}
7577
* (only applicable when running in an application context)<br>
76-
* 7. ApplicationContextAware's {@code setApplicationContext}
78+
* 9. ApplicationContextAware's {@code setApplicationContext}
7779
* (only applicable when running in an application context)<br>
78-
* 8. ServletContextAware's {@code setServletContext}
80+
* 10. ServletContextAware's {@code setServletContext}
7981
* (only applicable when running in a web application context)<br>
80-
* 9. {@code postProcessBeforeInitialization} methods of BeanPostProcessors<br>
81-
* 10. InitializingBean's {@code afterPropertiesSet}<br>
82-
* 11. a custom init-method definition<br>
83-
* 12. {@code postProcessAfterInitialization} methods of BeanPostProcessors
82+
* 11. {@code postProcessBeforeInitialization} methods of BeanPostProcessors<br>
83+
* 12. InitializingBean's {@code afterPropertiesSet}<br>
84+
* 13. a custom init-method definition<br>
85+
* 14. {@code postProcessAfterInitialization} methods of BeanPostProcessors
8486
*
8587
* <p>On shutdown of a bean factory, the following lifecycle methods apply:<br>
86-
* 1. DisposableBean's {@code destroy}<br>
87-
* 2. a custom destroy-method definition
88+
* 1. {@code postProcessBeforeDestruction} methods of DestructionAwareBeanPostProcessors
89+
* 2. DisposableBean's {@code destroy}<br>
90+
* 3. a custom destroy-method definition
8891
*
8992
* @author Rod Johnson
9093
* @author Juergen Hoeller

spring-context/src/main/java/org/springframework/context/annotation/ComponentScanAnnotationParser.java

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,10 @@
2525
import java.util.regex.Pattern;
2626

2727
import org.springframework.beans.BeanUtils;
28-
import org.springframework.beans.factory.Aware;
29-
import org.springframework.beans.factory.BeanClassLoaderAware;
30-
import org.springframework.beans.factory.BeanFactory;
31-
import org.springframework.beans.factory.BeanFactoryAware;
3228
import org.springframework.beans.factory.config.BeanDefinitionHolder;
33-
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
3429
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
3530
import org.springframework.beans.factory.support.BeanNameGenerator;
3631
import org.springframework.context.ConfigurableApplicationContext;
37-
import org.springframework.context.EnvironmentAware;
38-
import org.springframework.context.ResourceLoaderAware;
3932
import org.springframework.core.annotation.AnnotationAttributes;
4033
import org.springframework.core.env.Environment;
4134
import org.springframework.core.io.ResourceLoader;
@@ -61,16 +54,16 @@
6154
*/
6255
class ComponentScanAnnotationParser {
6356

64-
private final ResourceLoader resourceLoader;
65-
6657
private final Environment environment;
6758

68-
private final BeanDefinitionRegistry registry;
59+
private final ResourceLoader resourceLoader;
6960

7061
private final BeanNameGenerator beanNameGenerator;
7162

63+
private final BeanDefinitionRegistry registry;
64+
7265

73-
public ComponentScanAnnotationParser(ResourceLoader resourceLoader, Environment environment,
66+
public ComponentScanAnnotationParser(Environment environment, ResourceLoader resourceLoader,
7467
BeanNameGenerator beanNameGenerator, BeanDefinitionRegistry registry) {
7568

7669
this.resourceLoader = resourceLoader;
@@ -90,7 +83,7 @@ public Set<BeanDefinitionHolder> parse(AnnotationAttributes componentScan, final
9083
scanner.setResourceLoader(this.resourceLoader);
9184

9285
Class<? extends BeanNameGenerator> generatorClass = componentScan.getClass("nameGenerator");
93-
boolean useInheritedGenerator = BeanNameGenerator.class == generatorClass;
86+
boolean useInheritedGenerator = (BeanNameGenerator.class == generatorClass);
9487
scanner.setBeanNameGenerator(useInheritedGenerator ? this.beanNameGenerator :
9588
BeanUtils.instantiateClass(generatorClass));
9689

@@ -164,7 +157,8 @@ private List<TypeFilter> typeFiltersFor(AnnotationAttributes filterAttributes) {
164157
Assert.isAssignable(TypeFilter.class, filterClass,
165158
"An error occurred while processing a @ComponentScan CUSTOM type filter: ");
166159
TypeFilter filter = BeanUtils.instantiateClass(filterClass, TypeFilter.class);
167-
invokeAwareMethods(filter);
160+
ParserStrategyUtils.invokeAwareMethods(
161+
filter, this.environment, this.resourceLoader, this.registry);
168162
typeFilters.add(filter);
169163
break;
170164
default:
@@ -188,27 +182,4 @@ private List<TypeFilter> typeFiltersFor(AnnotationAttributes filterAttributes) {
188182
return typeFilters;
189183
}
190184

191-
/**
192-
* Invoke {@link ResourceLoaderAware}, {@link BeanClassLoaderAware} and
193-
* {@link BeanFactoryAware} contracts if implemented by the given {@code filter}.
194-
*/
195-
private void invokeAwareMethods(TypeFilter filter) {
196-
if (filter instanceof Aware) {
197-
if (filter instanceof EnvironmentAware) {
198-
((EnvironmentAware) filter).setEnvironment(this.environment);
199-
}
200-
if (filter instanceof ResourceLoaderAware) {
201-
((ResourceLoaderAware) filter).setResourceLoader(this.resourceLoader);
202-
}
203-
if (filter instanceof BeanClassLoaderAware) {
204-
ClassLoader classLoader = (this.registry instanceof ConfigurableBeanFactory ?
205-
((ConfigurableBeanFactory) this.registry).getBeanClassLoader() :
206-
this.resourceLoader.getClassLoader());
207-
((BeanClassLoaderAware) filter).setBeanClassLoader(classLoader);
208-
}
209-
if (filter instanceof BeanFactoryAware && this.registry instanceof BeanFactory) {
210-
((BeanFactoryAware) filter).setBeanFactory((BeanFactory) this.registry);
211-
}
212-
}
213-
}
214185
}

spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public ConfigurationClassParser(MetadataReaderFactory metadataReaderFactory,
158158
this.resourceLoader = resourceLoader;
159159
this.registry = registry;
160160
this.componentScanParser = new ComponentScanAnnotationParser(
161-
resourceLoader, environment, componentScanBeanNameGenerator, registry);
161+
environment, resourceLoader, componentScanBeanNameGenerator, registry);
162162
this.conditionEvaluator = new ConditionEvaluator(registry, environment, resourceLoader);
163163
}
164164

@@ -509,7 +509,8 @@ private void processImports(ConfigurationClass configClass, SourceClass currentS
509509
// Candidate class is an ImportSelector -> delegate to it to determine imports
510510
Class<?> candidateClass = candidate.loadClass();
511511
ImportSelector selector = BeanUtils.instantiateClass(candidateClass, ImportSelector.class);
512-
invokeAwareMethods(selector);
512+
ParserStrategyUtils.invokeAwareMethods(
513+
selector, this.environment, this.resourceLoader, this.registry);
513514
if (this.deferredImportSelectors != null && selector instanceof DeferredImportSelector) {
514515
this.deferredImportSelectors.add(
515516
new DeferredImportSelectorHolder(configClass, (DeferredImportSelector) selector));
@@ -526,7 +527,8 @@ else if (candidate.isAssignable(ImportBeanDefinitionRegistrar.class)) {
526527
Class<?> candidateClass = candidate.loadClass();
527528
ImportBeanDefinitionRegistrar registrar =
528529
BeanUtils.instantiateClass(candidateClass, ImportBeanDefinitionRegistrar.class);
529-
invokeAwareMethods(registrar);
530+
ParserStrategyUtils.invokeAwareMethods(
531+
registrar, this.environment, this.resourceLoader, this.registry);
530532
configClass.addImportBeanDefinitionRegistrar(registrar, currentSourceClass.getMetadata());
531533
}
532534
else {
@@ -565,30 +567,6 @@ private boolean isChainedImportOnStack(ConfigurationClass configClass) {
565567
return false;
566568
}
567569

568-
/**
569-
* Invoke {@link ResourceLoaderAware}, {@link BeanClassLoaderAware} and
570-
* {@link BeanFactoryAware} contracts if implemented by the given {@code bean}.
571-
*/
572-
private void invokeAwareMethods(Object importStrategyBean) {
573-
if (importStrategyBean instanceof Aware) {
574-
if (importStrategyBean instanceof EnvironmentAware) {
575-
((EnvironmentAware) importStrategyBean).setEnvironment(this.environment);
576-
}
577-
if (importStrategyBean instanceof ResourceLoaderAware) {
578-
((ResourceLoaderAware) importStrategyBean).setResourceLoader(this.resourceLoader);
579-
}
580-
if (importStrategyBean instanceof BeanClassLoaderAware) {
581-
ClassLoader classLoader = (this.registry instanceof ConfigurableBeanFactory ?
582-
((ConfigurableBeanFactory) this.registry).getBeanClassLoader() :
583-
this.resourceLoader.getClassLoader());
584-
((BeanClassLoaderAware) importStrategyBean).setBeanClassLoader(classLoader);
585-
}
586-
if (importStrategyBean instanceof BeanFactoryAware && this.registry instanceof BeanFactory) {
587-
((BeanFactoryAware) importStrategyBean).setBeanFactory((BeanFactory) this.registry);
588-
}
589-
}
590-
}
591-
592570

593571
/**
594572
* Validate each {@link ConfigurationClass} object.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.context.annotation;
18+
19+
import org.springframework.beans.factory.Aware;
20+
import org.springframework.beans.factory.BeanClassLoaderAware;
21+
import org.springframework.beans.factory.BeanFactory;
22+
import org.springframework.beans.factory.BeanFactoryAware;
23+
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
24+
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
25+
import org.springframework.context.EnvironmentAware;
26+
import org.springframework.context.ResourceLoaderAware;
27+
import org.springframework.core.env.Environment;
28+
import org.springframework.core.io.ResourceLoader;
29+
30+
/**
31+
* Common delegate code for the handling of parser strategies, e.g.
32+
* {@code TypeFilter}, {@code ImportSelector}, {@code ImportBeanDefinitionRegistrar}
33+
*
34+
* @author Juergen Hoeller
35+
* @since 4.3.3
36+
*/
37+
abstract class ParserStrategyUtils {
38+
39+
/**
40+
* Invoke {@link BeanClassLoaderAware}, {@link BeanFactoryAware},
41+
* {@link EnvironmentAware}, and {@link ResourceLoaderAware} contracts
42+
* if implemented by the given object.
43+
*/
44+
public static void invokeAwareMethods(Object parserStrategyBean, Environment environment,
45+
ResourceLoader resourceLoader, BeanDefinitionRegistry registry) {
46+
47+
if (parserStrategyBean instanceof Aware) {
48+
if (parserStrategyBean instanceof BeanClassLoaderAware) {
49+
ClassLoader classLoader = (registry instanceof ConfigurableBeanFactory ?
50+
((ConfigurableBeanFactory) registry).getBeanClassLoader() : resourceLoader.getClassLoader());
51+
((BeanClassLoaderAware) parserStrategyBean).setBeanClassLoader(classLoader);
52+
}
53+
if (parserStrategyBean instanceof BeanFactoryAware && registry instanceof BeanFactory) {
54+
((BeanFactoryAware) parserStrategyBean).setBeanFactory((BeanFactory) registry);
55+
}
56+
if (parserStrategyBean instanceof EnvironmentAware) {
57+
((EnvironmentAware) parserStrategyBean).setEnvironment(environment);
58+
}
59+
if (parserStrategyBean instanceof ResourceLoaderAware) {
60+
((ResourceLoaderAware) parserStrategyBean).setResourceLoader(resourceLoader);
61+
}
62+
}
63+
}
64+
65+
}

0 commit comments

Comments
 (0)