Skip to content

Enhance conditional bean creation when using method overloading #28019

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package org.springframework.context.annotation;

import java.util.HashSet;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
Expand Down Expand Up @@ -65,8 +65,7 @@ final class ConfigurationClass {
private final Map<ImportBeanDefinitionRegistrar, AnnotationMetadata> importBeanDefinitionRegistrars =
new LinkedHashMap<>();

final Set<String> skippedBeanMethods = new HashSet<>();

final Map<String, MethodMetadataWrapper> skippedBeanMethods = new HashMap<>();

/**
* Create a new {@link ConfigurationClass} with the given name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
* @author Phillip Webb
* @author Sam Brannen
* @author Sebastien Deleuze
* @author Hakan Altindag
* @since 3.0
* @see ConfigurationClassParser
*/
Expand Down Expand Up @@ -187,12 +188,26 @@ private void loadBeanDefinitionsForBeanMethod(BeanMethod beanMethod) {
MethodMetadata metadata = beanMethod.getMetadata();
String methodName = metadata.getMethodName();

// Do we need to mark the bean as skipped by its condition?
MethodMetadataWrapper metadataWrapper = new MethodMetadataWrapper(metadata);

if (this.conditionEvaluator.shouldSkip(metadata, ConfigurationPhase.REGISTER_BEAN)) {
configClass.skippedBeanMethods.add(methodName);
configClass.skippedBeanMethods.put(methodName, new MethodMetadataWrapper(metadata));
return;
}
if (configClass.skippedBeanMethods.contains(methodName)) {
else if (configClass.skippedBeanMethods.containsKey(methodName)){
MethodMetadataWrapper toBeSkippedBeanMethod = configClass.skippedBeanMethods.get(methodName);

if (!toBeSkippedBeanMethod.getSuperClassForDeclaredBean().isPresent()) {
configClass.skippedBeanMethods.remove(methodName);
}

if (toBeSkippedBeanMethod.getClassForDeclaredBean().isPresent()
&& metadataWrapper.getSuperClassForDeclaredBean().isPresent()
&& toBeSkippedBeanMethod.getClassForDeclaredBean().get() == metadataWrapper.getSuperClassForDeclaredBean().get()) {
configClass.skippedBeanMethods.remove(methodName);
}
}
if (configClass.skippedBeanMethods.containsKey(methodName)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2002-2022 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;

import java.util.Optional;

import org.springframework.core.type.MethodMetadata;

public class MethodMetadataWrapper {

private final MethodMetadata methodMetadata;

public MethodMetadataWrapper(MethodMetadata methodMetadata) {
this.methodMetadata = methodMetadata;
}

public String getBeanName() {
return this.methodMetadata.getMethodName();
}

public Optional<Class<?>> getClassForDeclaredBean() {
try {
Class<?> clazz = Class.forName(this.methodMetadata.getDeclaringClassName());
return Optional.of(clazz);
}
catch (ClassNotFoundException ex) {
return Optional.empty();
}
}

public Optional<? extends Class<?>> getSuperClassForDeclaredBean() {
return getClassForDeclaredBean()
.map(Class::getSuperclass)
.filter(superClass -> !"java.lang.Object".equals(superClass.getName()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,17 @@ public void importsNotCreated() throws Exception {
}

@Test
public void conditionOnOverriddenMethodHonored() {
public void conditionOnOverriddenMethodHonoredWhenHavingInheritance() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConfigWithBeanSkipped.class);
assertThat(context.getBeansOfType(ExampleBean.class).size()).isEqualTo(0);
}

@Test
public void conditionOnOverriddenMethodHonoredWhenHavingMethodOverloading() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConfigWithBeanNotSkippedWithMethodOverloading.class);
assertThat(context.getBeansOfType(ExampleBean.class).size()).isEqualTo(1);
}

@Test
public void noConditionOnOverriddenMethodHonored() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConfigWithBeanReactivated.class);
Expand Down Expand Up @@ -358,6 +364,27 @@ public ExampleBean baz() {
}
}

static class ConfigWithBeanNotSkippedWithMethodOverloading {

@Bean
@Conditional(NeverCondition.class)
public ExampleBean baz() {
return new ExampleBean();
}

@Bean
@Conditional(AlwaysCondition.class)
public ExampleBean baz(Object foo) {
return new ExampleBean();
}

@Bean
public Object foo() {
return new Object();
}

}

static class ConfigWithBeanReactivated extends ConfigWithBeanSkipped {

@Override
Expand Down