spring-projects / spring-projects/spring-framework

Enhancement: @UniqueBean on interface or unique-beans: Class[] in application.yml

Open
#35,972 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

in: core status: waiting-for-triage
Dominant language
Java
Stars
60.2k
Forks
38.8k
Avg merge
5d 2h
Merged PRs (30d)
27

Description

We like having interfaces define a contract for beans, and we also like having things like auto-configuration.

However, we can have situations where we know we only want there to ever be one implementation of an interface in the bean registry.

As a developer or perhaps even ops, I want to have duplicate bean definitions caught early instead of at the @Autowired site.

Would it work to either have something like:

package com.acme;

@UniqueBean
interface MyBean {
}

or

application.yml

spring:
  unique-beans:
    - com.acme.MyBean

We could then get a different exception thrown for this scenario, and and a far simpler exception report than the NoUniqueBeanDefinitionException report which is often not the information that is best to diagnose the cause of the duplication.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing Spring's bean registry and the existing @Autowired handling around NoUniqueBeanDefinitionException. Compare the proposed @UniqueBean interface annotation with the application.yml unique-beans configuration, then define the exception behavior and configuration scope before identifying implementation and test locations.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.