Skip to content

Commit

Permalink
Preserve existing imported class over scanned configuration class
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Feb 20, 2024
1 parent 2669531 commit 22b41c3
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -56,6 +56,8 @@ final class ConfigurationClass {
@Nullable
private String beanName;

private boolean scanned = false;

private final Set<ConfigurationClass> importedBy = new LinkedHashSet<>(1);

private final Set<BeanMethod> beanMethods = new LinkedHashSet<>();
Expand All @@ -73,7 +75,6 @@ final class ConfigurationClass {
* Create a new {@link ConfigurationClass} with the given name.
* @param metadataReader reader used to parse the underlying {@link Class}
* @param beanName must not be {@code null}
* @see ConfigurationClass#ConfigurationClass(Class, ConfigurationClass)
*/
ConfigurationClass(MetadataReader metadataReader, String beanName) {
Assert.notNull(beanName, "Bean name must not be null");
Expand All @@ -87,10 +88,10 @@ final class ConfigurationClass {
* using the {@link Import} annotation or automatically processed as a nested
* configuration class (if importedBy is not {@code null}).
* @param metadataReader reader used to parse the underlying {@link Class}
* @param importedBy the configuration class importing this one or {@code null}
* @param importedBy the configuration class importing this one
* @since 3.1.1
*/
ConfigurationClass(MetadataReader metadataReader, @Nullable ConfigurationClass importedBy) {
ConfigurationClass(MetadataReader metadataReader, ConfigurationClass importedBy) {
this.metadata = metadataReader.getAnnotationMetadata();
this.resource = metadataReader.getResource();
this.importedBy.add(importedBy);
Expand All @@ -100,7 +101,6 @@ final class ConfigurationClass {
* Create a new {@link ConfigurationClass} with the given name.
* @param clazz the underlying {@link Class} to represent
* @param beanName name of the {@code @Configuration} class bean
* @see ConfigurationClass#ConfigurationClass(Class, ConfigurationClass)
*/
ConfigurationClass(Class<?> clazz, String beanName) {
Assert.notNull(beanName, "Bean name must not be null");
Expand All @@ -114,10 +114,10 @@ final class ConfigurationClass {
* using the {@link Import} annotation or automatically processed as a nested
* configuration class (if imported is {@code true}).
* @param clazz the underlying {@link Class} to represent
* @param importedBy the configuration class importing this one (or {@code null})
* @param importedBy the configuration class importing this one
* @since 3.1.1
*/
ConfigurationClass(Class<?> clazz, @Nullable ConfigurationClass importedBy) {
ConfigurationClass(Class<?> clazz, ConfigurationClass importedBy) {
this.metadata = AnnotationMetadata.introspect(clazz);
this.resource = new DescriptiveResource(clazz.getName());
this.importedBy.add(importedBy);
Expand All @@ -127,13 +127,14 @@ final class ConfigurationClass {
* Create a new {@link ConfigurationClass} with the given name.
* @param metadata the metadata for the underlying class to represent
* @param beanName name of the {@code @Configuration} class bean
* @see ConfigurationClass#ConfigurationClass(Class, ConfigurationClass)
* @param scanned whether the underlying class has been registered through a scan
*/
ConfigurationClass(AnnotationMetadata metadata, String beanName) {
ConfigurationClass(AnnotationMetadata metadata, String beanName, boolean scanned) {
Assert.notNull(beanName, "Bean name must not be null");
this.metadata = metadata;
this.resource = new DescriptiveResource(metadata.getClassName());
this.beanName = beanName;
this.scanned = scanned;
}


Expand All @@ -149,22 +150,30 @@ String getSimpleName() {
return ClassUtils.getShortName(getMetadata().getClassName());
}

void setBeanName(String beanName) {
void setBeanName(@Nullable String beanName) {
this.beanName = beanName;
}

@Nullable
public String getBeanName() {
String getBeanName() {
return this.beanName;
}

/**
* Return whether this configuration class has been registered through a scan.
* @since 6.2
*/
boolean isScanned() {
return this.scanned;
}

/**
* Return whether this configuration class was registered via @{@link Import} or
* automatically registered due to being nested within another configuration class.
* @since 3.1.1
* @see #getImportedBy()
*/
public boolean isImported() {
boolean isImported() {
return !this.importedBy.isEmpty();
}

Expand Down Expand Up @@ -198,6 +207,10 @@ void addImportedResource(String importedResource, Class<? extends BeanDefinition
this.importedResources.put(importedResource, readerClass);
}

Map<String, Class<? extends BeanDefinitionReader>> getImportedResources() {
return this.importedResources;
}

void addImportBeanDefinitionRegistrar(ImportBeanDefinitionRegistrar registrar, AnnotationMetadata importingClassMetadata) {
this.importBeanDefinitionRegistrars.put(registrar, importingClassMetadata);
}
Expand All @@ -206,10 +219,6 @@ Map<ImportBeanDefinitionRegistrar, AnnotationMetadata> getImportBeanDefinitionRe
return this.importBeanDefinitionRegistrars;
}

Map<String, Class<? extends BeanDefinitionReader>> getImportedResources() {
return this.importedResources;
}

void validate(ProblemReporter problemReporter) {
Map<String, Object> attributes = this.metadata.getAnnotationAttributes(Configuration.class.getName());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -162,7 +162,7 @@ public void parse(Set<BeanDefinitionHolder> configCandidates) {
BeanDefinition bd = holder.getBeanDefinition();
try {
if (bd instanceof AnnotatedBeanDefinition annotatedBeanDef) {
parse(annotatedBeanDef.getMetadata(), holder.getBeanName());
parse(annotatedBeanDef, holder.getBeanName());
}
else if (bd instanceof AbstractBeanDefinition abstractBeanDef && abstractBeanDef.hasBeanClass()) {
parse(abstractBeanDef.getBeanClass(), holder.getBeanName());
Expand All @@ -183,31 +183,33 @@ else if (bd instanceof AbstractBeanDefinition abstractBeanDef && abstractBeanDef
this.deferredImportSelectorHandler.process();
}

protected final void parse(@Nullable String className, String beanName) throws IOException {
Assert.notNull(className, "No bean class name for configuration class bean definition");
MetadataReader reader = this.metadataReaderFactory.getMetadataReader(className);
processConfigurationClass(new ConfigurationClass(reader, beanName), DEFAULT_EXCLUSION_FILTER);
private void parse(AnnotatedBeanDefinition beanDef, String beanName) {
processConfigurationClass(
new ConfigurationClass(beanDef.getMetadata(), beanName, (beanDef instanceof ScannedGenericBeanDefinition)),
DEFAULT_EXCLUSION_FILTER);
}

protected final void parse(Class<?> clazz, String beanName) throws IOException {
private void parse(Class<?> clazz, String beanName) {
processConfigurationClass(new ConfigurationClass(clazz, beanName), DEFAULT_EXCLUSION_FILTER);
}

protected final void parse(AnnotationMetadata metadata, String beanName) throws IOException {
processConfigurationClass(new ConfigurationClass(metadata, beanName), DEFAULT_EXCLUSION_FILTER);
final void parse(@Nullable String className, String beanName) throws IOException {
Assert.notNull(className, "No bean class name for configuration class bean definition");
MetadataReader reader = this.metadataReaderFactory.getMetadataReader(className);
processConfigurationClass(new ConfigurationClass(reader, beanName), DEFAULT_EXCLUSION_FILTER);
}

/**
* Validate each {@link ConfigurationClass} object.
* @see ConfigurationClass#validate
*/
public void validate() {
void validate() {
for (ConfigurationClass configClass : this.configurationClasses.keySet()) {
configClass.validate(this.problemReporter);
}
}

public Set<ConfigurationClass> getConfigurationClasses() {
Set<ConfigurationClass> getConfigurationClasses() {
return this.configurationClasses.keySet();
}

Expand All @@ -216,7 +218,12 @@ List<PropertySourceDescriptor> getPropertySourceDescriptors() {
Collections.emptyList());
}

protected void processConfigurationClass(ConfigurationClass configClass, Predicate<String> filter) throws IOException {
ImportRegistry getImportRegistry() {
return this.importStack;
}


protected void processConfigurationClass(ConfigurationClass configClass, Predicate<String> filter) {
if (this.conditionEvaluator.shouldSkip(configClass.getMetadata(), ConfigurationPhase.PARSE_CONFIGURATION)) {
return;
}
Expand All @@ -230,6 +237,14 @@ protected void processConfigurationClass(ConfigurationClass configClass, Predica
// Otherwise ignore new imported config class; existing non-imported class overrides it.
return;
}
else if (configClass.isScanned()) {
String beanName = configClass.getBeanName();
if (beanName != null) {
this.registry.removeBeanDefinition(beanName);
}
// An implicitly scanned bean definition should not override an explicit import.
return;
}
else {
// Explicit bean definition found, probably replacing an import.
// Let's remove the old one and go with the new one.
Expand Down Expand Up @@ -563,11 +578,6 @@ private boolean isChainedImportOnStack(ConfigurationClass configClass) {
return false;
}

ImportRegistry getImportRegistry() {
return this.importStack;
}


/**
* Factory method to obtain a {@link SourceClass} from a {@link ConfigurationClass}.
*/
Expand Down Expand Up @@ -636,7 +646,7 @@ private static class ImportStack extends ArrayDeque<ConfigurationClass> implemen

private final MultiValueMap<String, AnnotationMetadata> imports = new LinkedMultiValueMap<>();

public void registerImport(AnnotationMetadata importingClass, String importedClass) {
void registerImport(AnnotationMetadata importingClass, String importedClass) {
this.imports.add(importedClass, importingClass);
}

Expand Down Expand Up @@ -691,7 +701,7 @@ private class DeferredImportSelectorHandler {
* @param configClass the source configuration class
* @param importSelector the selector to handle
*/
public void handle(ConfigurationClass configClass, DeferredImportSelector importSelector) {
void handle(ConfigurationClass configClass, DeferredImportSelector importSelector) {
DeferredImportSelectorHolder holder = new DeferredImportSelectorHolder(configClass, importSelector);
if (this.deferredImportSelectors == null) {
DeferredImportSelectorGroupingHandler handler = new DeferredImportSelectorGroupingHandler();
Expand All @@ -703,7 +713,7 @@ public void handle(ConfigurationClass configClass, DeferredImportSelector import
}
}

public void process() {
void process() {
List<DeferredImportSelectorHolder> deferredImports = this.deferredImportSelectors;
this.deferredImportSelectors = null;
try {
Expand All @@ -727,7 +737,7 @@ private class DeferredImportSelectorGroupingHandler {

private final Map<AnnotationMetadata, ConfigurationClass> configurationClasses = new HashMap<>();

public void register(DeferredImportSelectorHolder deferredImport) {
void register(DeferredImportSelectorHolder deferredImport) {
Class<? extends Group> group = deferredImport.getImportSelector().getImportGroup();
DeferredImportSelectorGrouping grouping = this.groupings.computeIfAbsent(
(group != null ? group : deferredImport),
Expand All @@ -737,7 +747,7 @@ public void register(DeferredImportSelectorHolder deferredImport) {
deferredImport.getConfigurationClass());
}

public void processGroupImports() {
void processGroupImports() {
for (DeferredImportSelectorGrouping grouping : this.groupings.values()) {
Predicate<String> exclusionFilter = grouping.getCandidateFilter();
grouping.getImports().forEach(entry -> {
Expand Down Expand Up @@ -775,16 +785,16 @@ private static class DeferredImportSelectorHolder {

private final DeferredImportSelector importSelector;

public DeferredImportSelectorHolder(ConfigurationClass configClass, DeferredImportSelector selector) {
DeferredImportSelectorHolder(ConfigurationClass configClass, DeferredImportSelector selector) {
this.configurationClass = configClass;
this.importSelector = selector;
}

public ConfigurationClass getConfigurationClass() {
ConfigurationClass getConfigurationClass() {
return this.configurationClass;
}

public DeferredImportSelector getImportSelector() {
DeferredImportSelector getImportSelector() {
return this.importSelector;
}
}
Expand All @@ -800,23 +810,23 @@ private static class DeferredImportSelectorGrouping {
this.group = group;
}

public void add(DeferredImportSelectorHolder deferredImport) {
void add(DeferredImportSelectorHolder deferredImport) {
this.deferredImports.add(deferredImport);
}

/**
* Return the imports defined by the group.
* @return each import with its associated configuration class
*/
public Iterable<Group.Entry> getImports() {
Iterable<Group.Entry> getImports() {
for (DeferredImportSelectorHolder deferredImport : this.deferredImports) {
this.group.process(deferredImport.getConfigurationClass().getMetadata(),
deferredImport.getImportSelector());
}
return this.group.selectImports();
}

public Predicate<String> getCandidateFilter() {
Predicate<String> getCandidateFilter() {
Predicate<String> mergedFilter = DEFAULT_EXCLUSION_FILTER;
for (DeferredImportSelectorHolder deferredImport : this.deferredImports) {
Predicate<String> selectorFilter = deferredImport.getImportSelector().getExclusionFilter();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.context.annotation.componentscan.ordered;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
* @author Vladislav Kisel
*/
@Configuration
@Import(SiblingImportingConfigB.class)
public class SiblingImportingConfigA {

@Bean(name = "a-imports-b")
String bean() {
return "valueFromA";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.context.annotation.componentscan.ordered;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* @author Vladislav Kisel
*/
@Configuration
public class SiblingImportingConfigB {

@Bean(name = "a-imports-b")
String bean() {
return "valueFromB";
}

}
Loading

0 comments on commit 22b41c3

Please sign in to comment.