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
ndwlocatieservices added a commit to ndwnu/nls-geometry that referenced this issue Jan 13, 2025
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [org.assertj:assertj-core](https://assertj.github.io/doc/#assertj-core) ([source](https://github.com/assertj/assertj)) | test | minor | `3.26.3` -> `3.27.2` |
| [org.springframework.boot:spring-boot-starter-parent](https://spring.io/projects/spring-boot) ([source](https://github.com/spring-projects/spring-boot)) | parent | patch | `3.4.0` -> `3.4.1` |

---

### Release Notes

<details>
<summary>spring-projects/spring-boot (org.springframework.boot:spring-boot-starter-parent)</summary>

### [`v3.4.1`](https://github.com/spring-projects/spring-boot/releases/tag/v3.4.1)

[Compare Source](spring-projects/spring-boot@v3.4.0...v3.4.1)

##### 🐞 Bug Fixes

-   KafkaProperties fail to build SSL properties when the bundle name is an empty string [#&#8203;43563](spring-projects/spring-boot#43563)
-   Diagnostics are poor when property resolution throws a ConversionFailedException [#&#8203;43559](spring-projects/spring-boot#43559)
-   SpringApplicationShutdownHandlers do not run in deterministic order [#&#8203;43536](spring-projects/spring-boot#43536)
-   Unable to find a `@SpringBootConfiguration` results in misleading error message [#&#8203;43507](spring-projects/spring-boot#43507)
-   With multiple ResourceHandlerRegistrationCustomizer beans in the context, only one of them is used [#&#8203;43497](spring-projects/spring-boot#43497)
-   Unable to use Docker Compose support when mixing dedicated and shared services [#&#8203;43472](spring-projects/spring-boot#43472)
-   Kafka dependency management does not include the kafka-server module [#&#8203;43454](spring-projects/spring-boot#43454)
-   Docker API version is incorrectly reported when '/\_ping\` calls fail and version should be fixed [#&#8203;43452](spring-projects/spring-boot#43452)
-   Methods to build producer / consumer properties from KafkaProperties are inconvienenent to use without an SSL bundle [#&#8203;43448](spring-projects/spring-boot#43448)
-   Failures in -Djarmode=tools do not consistently return a non-zero exit [#&#8203;43436](spring-projects/spring-boot#43436)
-   HttpComponentsClientHttpRequestFactoryBuilder replaces the existing defaultRequestConfigCustomizer rather than adding to it [#&#8203;43429](spring-projects/spring-boot#43429)
-   spring-boot-maven-plugin sets imagePlatform even if it's empty [#&#8203;43424](spring-projects/spring-boot#43424)
-   OnBeanCondition fails to match on annotations when using Scoped Proxies [#&#8203;43423](spring-projects/spring-boot#43423)
-   Failure analysis for InvalidConfigurationProper...
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