Closed
Description
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:
- ObjectProvider iterable/stream access for "beans of type" resolution in @Bean methods [SPR-11419] #16046 ObjectProvider iterable/stream access for "beans of type" resolution in
@Bean
methods - Honor @Autowired(required=false) at parameter level, as an alternative to java.util.Optional [SPR-15268] #19833 Honor
@Autowired
(required=false) at parameter level, as an alternative to java.util.Optional - Autowiring inconsistency: @Qualifier works with HashMap but not with Map [SPR-17197] #21731 Autowiring inconsistency:
@Qualifier
works with HashMap but not with Map
Referenced from: commits spring-projects/spring-boot@c98bb40