Skip to content

Feature Request: Scoped Bean Injection Control by Bean Creators #33393

@yonyes

Description

@yonyes

I propose introducing a feature to Spring Framework that allows developers to specify the packages or classes where their bean can be injected. This feature aims to give bean creators more control over the injection scope, thereby enhancing application modularity and preventing misuse.

Problem Description:
Currently, all beans in an application are eligible for autowiring across the entire application. This can lead to beans being used in contexts not intended by their creators, which complicates maintenance and can lead to architectural inconsistencies.

Proposed Feature:
Implement a configuration mechanism, potentially through annotations, that enables bean creators to restrict where their beans can be injected. Unlike solutions like @Qualifier, which are controlled by the consumers, this feature would allow creators to define injection boundaries directly.

Use Cases:

  • Modular Libraries: For developers creating reusable modules intended for use in other applications, this feature would allow them to specify that certain beans should only be injectable within the module itself or in specific parts of a consuming application.

  • Large-Scale Applications: In large projects with multiple teams, it helps maintain clean boundaries and prevents architectural drift by ensuring that beans are only used in their intended contexts.

Benefits:

  • Enhanced Modularity: Provides developers with the tools to enforce modular design principles effectively.

  • Reduced Errors and Confusion: Helps prevent the misuse of components, which can reduce errors and streamline developer onboarding.

I believe this feature would significantly improve developers' control over their applications, making the Spring Framework even more robust for large-scale projects and modular library development.

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: coreIssues in core modules (aop, beans, core, context, expression)status: declinedA suggestion or change that we don't feel we should currently apply

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions