Skip to content
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

OnBeanCondition fails to match on annotations when using Scoped Proxies #43423

Closed
hk-2keys opened this issue Dec 4, 2024 · 0 comments
Closed
Assignees
Labels
type: regression A regression from a previous release
Milestone

Comments

@hk-2keys
Copy link

hk-2keys commented Dec 4, 2024

Description

Previous to 3.4.0 (tested in 3.3.5), scoped beans using ScopedProxyMode.INTERFACES or ScopedProxyMode.TARGET_CLASS were being matched by any OnBeanCondition that checked for annotations. (e.g. @ConditionOnMissingBean(annotation = SomeAnnotation.class)). With 3.4.0 adding a check for autowire candidates and default candidates, this results in these beans no longer being matched.

Example

In this example testConditionalOnMissingBean() passes in both 3.4.0 and 3.3.5, but testConditionalOnMissingBean_Scoped() fails in 3.4.0 and passes in 3.3.5.

Note: In 3.3.5 the condition @ConditionalOnMissingBean(value = Void.class, annotation = AnnotationConditionQualifier.class) was used to effectively disable type based matching.

Source

@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@Qualifier
public @interface AnnotationConditionQualifier {
}


@AutoConfiguration
public class AnnotationConditionAutoConfiguration {

    @Bean
    @AnnotationConditionQualifier
    @ConditionalOnMissingBean(annotation = AnnotationConditionQualifier.class)
    public Supplier<String> supplier() {
        return () -> "auto-configuration-string";
    }
}

Test

class AnnotationConditionAutoConfigurationTest {

    WebApplicationContextRunner runner;

    @BeforeEach
    void setUp() {
        runner = new WebApplicationContextRunner()
                .withConfiguration(AutoConfigurations.of(AnnotationConditionAutoConfiguration.class));
    }

    @Test
    void testConditionalOnMissingBean_Scoped() {
        runner.withUserConfiguration(ScopedConfig.class)
                .run(context -> {
                    assertThat(context)
                            .getBeanNames(Supplier.class)
                            .containsExactlyInAnyOrder(
                                    "customSupplier",
                                    ScopedProxyUtils.getTargetBeanName("customSupplier")
                            );
                });
    }

    @Test
    void testConditionalOnMissingBean() {
        runner.withUserConfiguration(Config.class)
                .run(context -> {
                    assertThat(context)
                            .getBeanNames(Supplier.class)
                            .containsExactlyInAnyOrder(
                                    "customSupplier"
                            );
                });
    }

    @Configuration
    @Import(CustomSupplier.class)
    static class Config {
    }

    @Configuration
    @Import(ScopedCustomSupplier.class)
    static class ScopedConfig {
    }

    @Component("customSupplier")
    @AnnotationConditionQualifier
    static class CustomSupplier implements Supplier<String> {
        @Override
        public String get() {
            return "custom-string";
        }
    }

    @Component("customSupplier")
    @AnnotationConditionQualifier
    @Scope(scopeName = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.INTERFACES)
    static class ScopedCustomSupplier implements Supplier<String> {
        @Override
        public String get() {
            return "scoped-custom-string";
        }
    }
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 4, 2024
@philwebb philwebb self-assigned this Dec 5, 2024
@philwebb philwebb added type: regression A regression from a previous release and removed status: waiting-for-triage An issue we've not yet triaged labels Dec 5, 2024
@philwebb philwebb added this to the 3.4.1 milestone Dec 5, 2024
@philwebb philwebb changed the title OnBeanCondition Annotation Processing of Scoped Proxies OnBeanCondition fails to match on annotations when using Scoped Proxies Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: regression A regression from a previous release
Projects
None yet
Development

No branches or pull requests

3 participants