After upgrading to springboot 3.5.0 from 3.4.6 we noticed a regression in bean creation in our projects. A bean, which does inherit from another bean with a generic (which itself is an extended class) type, now does not get setup properly, rather a default bean is initialized because the type matching behaves differently now.

We can workaround this issue by either declaring the @Bean as a raw-type or casting it to the proper generic super-class - which is not a recommended way by springboot, where we are supposed to always declare beans with the actual type. Is this an intentional behaviour now and we need to adapt, using again more general bean classes (away from recommended best practice)? The change happened in the ConditionalOnBean and got released in the v3.5.0 update.

I setup a simple example project. Changing the version to 3.4.6 and all tests run finely. bean init regression sample

Any help and clarification is appreciated. Thanks.

Comment From: philwebb

I think Spring is actually doing the correct thing here because it's matching Java's opinion on assignment.

For example, taking test_expectBeanWithInheritedClass(). If you do the following:

AbstractBean<Wrapper> abw = null;
BeanWithInheritedClass bwic = null;
abw = bwic;

You get a compile error on the last line.

To fix it, you need to do:

AbstractBean<? extends Wrapper> abw = null;
BeanWithInheritedClass bwic = null;
abw = bwic;

So I think your options are to either fix your generics in DefaultDemoAutoConfiguration:

@AutoConfiguration
public class DefaultDemoAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public AbstractBean<? extends Wrapper> defaultBeanWithDefaultClass() {
        return new DefaultBean() {
        };
    }

}

or if you don't want to do that, use the value on the annotation:

@AutoConfiguration
public class DefaultDemoAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(value = AbstractBean.class)
    public AbstractBean<Wrapper> defaultBeanWithDefaultClass() {
        return new DefaultBean() {
        };
    }

}

Comment From: philwebb

P.S. Generics are hard :)