Skip to content

Accept empty Collection<Component> injection for single constructor scenarios [SPR-15338] #19901

Closed
@spring-projects-issues

Description

@spring-projects-issues

Caleb Cushing opened SPR-15338 and commented

So I was going to do this

	private Collection<JChannel> jChannels;

	public HealthCheckService( Collection<JChannel> jChannels )
	{
	     this.jChannels = jChannels;	
	}

with the production profile this should have a bean, but in development it does not have any. I was expecting Spring to hand me an empty collection, but instead it handed me an exception.

	private Collection<JChannel> jChannels = Collections.emptySet();

	public HealthCheckService(
		final ServletContext context,
		final OrganizationDao organizationDao )
	{
		this.context = Objects.requireNonNull( context );
		this.organizationDao = Objects.requireNonNull( organizationDao );
	}

        @Autowired( required = false )
	void setjChannels( final Collection<JChannel> jChannels ) {
		this.jChannels = jChannels;
	}

I'm processing it as such, or maybe a stream later, we'll see.

private Optional<State> jgroupsAreOnline() {
     for ( JChannel jChannel : jChannels )
     {
          String state = jChannel.getState();
     }
     return Optional.empty();
}

Maybe this behavior could change in Spring 5, I could see if you annotated the collection with @Required to still require a bean, but to me in general, a collection should not be null, and generally should not be required to have elements. Your opinion may differ.


Affects: 4.3.7

Issue Links:

Referenced from: commits spring-projects/spring-boot@c98bb40

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions