Skip to content

Make use of bean definition overriding more visible #31288

Closed

Description

@snicoll

With errors that are hard to track, and with XML bean definitions taking always precedence over Java config (which can be surprising), we'd like to deprecate bean overriding wholesale with the idea of removing the feature at some point.

Spring Boot has already paved the way by disabling it by default and introducing a property to opt-in to the previous behavior. Us deprecating the feature in the core framework should send a strong signal to developers that they should not rely on that feature anymore.

Activity

  1. added
    in: core
    type: enhancement
    on Sep 21, 2023
  2. added this to the 6.x Backlog milestone on Sep 21, 2023
  3. changed the title [-]Deprecate bean overidding[/-] [+]Deprecate bean overriding[/+] on Sep 21, 2023
  4. dearoneesama commented on Sep 21, 2023

    @dearoneesama

    It scares me off, but It looks like we are not talking about overriding @Bean-decorataed methods right?

  5. snicoll commented on Sep 22, 2023

    @snicoll
    MemberAuthor

    registering twice a bean definition for the same key (i.e. bean name).

  6. changed the title [-]Deprecate bean overriding[/-] [+]Deprecate bean definition overriding[/+] on Sep 22, 2023
  7. modified the milestones: 6.x Backlog, 6.2.x on Oct 10, 2023
  8. modified the milestones: 6.2.x, 6.2.0-M1 on Jan 12, 2024
  9. mmoayyed commented on Jan 23, 2024

    @mmoayyed
    Contributor

    Us deprecating the feature in the core framework should send a strong signal to developers that they should not rely on that feature anymore.

    Could you please elaborate on how one might go about doing the same sort of thing when the particular bean is not marked as conditional? For example, what is the right strategy for overriding this bean?

    @Bean
    public MessageSource messageSource(MessageSourceProperties properties) {}
    • Should we exclude the configuration class?
    • Should we examine the conditions that trigger the creation of this bean and rework the app to disable those conditions? (In this case, rename the language bundle?)
    • Could we start sending pull requests that mark such beans with a @ConditionalOnMissingBean and family?
  10. snicoll commented on Feb 12, 2024

    @snicoll
    MemberAuthor

    So we're going to change this particular issue to log a warning that indicates why it is being logged and that a next release will make it more noisy. We will also update the reference guide to indicate that users should move away from that feature and how. Once that's done we will advocate the Spring Boot property to be deprecated.

    Deprecation of the feature proper is now scheduled for Spring Framework 7 to give enough room for users to notice and plan accordingly.

    @mmoayyed If you want to override it in your configuration, it won't be possible and the mitigation will happen on a case by case basis. For tests, we are working to give users a first class support for overriding specific beans.

  11. changed the title [-]Deprecate bean definition overriding[/-] [+]Log a warning when bean definition overriding is used[/+] on Feb 12, 2024
  12. snicoll commented on Mar 11, 2024

    @snicoll
    MemberAuthor

    We probably need to rephrase the note in https://docs.spring.io/spring-framework/reference/6.2-SNAPSHOT/testing/annotations/integration-spring/annotation-beanoverriding.html#spring-testing-annotation-beanoverriding-mockitobean.

    In the meantime support for bean overriding in tests has landed in 6.2.0-SNAPSHOT. The doc above provides an overview if you want to give the snapshots a try.

  13. snicoll commented on Mar 26, 2024

    @snicoll
    MemberAuthor

    Giving this a try on our own projects revealed quite a number of issues. If we pursue with this, we should probably introduce a flag that we can use to opt-in for an exception as the warning alone makes it harder to validate that things are working as expected.

  14. snicoll commented on Mar 27, 2024

    @snicoll
    MemberAuthor
  15. changed the title [-]Log a warning when bean definition overriding is used[/-] [+]Make use of bean definition overriding more visible[/+] on Apr 2, 2024
  16. added a commit that references this issue on Apr 2, 2024
    dd62224
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

in: coretype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions