Skip to content

Unexpected empty collection on Named collection type #27949

Closed as not planned
Closed as not planned
@pault-t-canva

Description

@pault-t-canva

Affects: 5.3.15

As introduced by #19901 and mentioned in #22735 it is expected that a collection type in a factory constructor provides an empty collection. Thus the following will occur

@Configuration 
class SampleConfiguration {
   @Bean 
   public TestClass testClass(Set<String> sampleSet) {
      return new TestClass(sampleSet); // In this case sampleSet will be an empty set if not found
  }
}

The suggested workaround if that was not desirable, was to check for empty methods and fail out.

In the following - more explicit - situation

@Configuration 
class SampleConfiguration {
   @Bean 
   public TestClass testClass(@Named("explicitName") Set<String> sampleSet) {
      return new TestClass(sampleSet); // In this case sampleSet will be an empty set if not found
  }
}

the sampleSet has been explicitly named. In this case, we will still receive an empty collection in the case that the bean cannot be found. This is surprising as at this point there is an explicit ask for a specific bean name. This makes a possible spelling mistake in the named bean have a high impact - because if an empty collection is valid, there's no way to tell if the bean was wired via the spring context, or if the default bean took its place.

An expected behaviour could be that if a bean was explicitly named - that it would fail in the event that the bean name was not found. Or that there would be some way to make the fallback passed though to https://github.com/spring-projects/spring-framework/blob/main/spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java#L852 false if explicitly desireable (such as via another annotation).

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