spring-framework icon indicating copy to clipboard operation
spring-framework copied to clipboard

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

Open nealeu opened this issue 6 days ago • 0 comments

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.

nealeu avatar Dec 05 '25 14:12 nealeu