Skip to content

For @Bean method that returns null, @Autowired injects NullBean instead of null for cached arguments #30485

@qiangyt

Description

@qiangyt

Affects: 5.x


See below test case:

import org.testng.annotations.Test;

import org.testng.Assert;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

public class NullBeanIssue {

    @Test
    public void test_shouldGetNull() {
        var ctx = new AnnotationConfigApplicationContext();
        ctx.register(MyFactoryBean.class);
        ctx.refresh();

        var myBean = ctx.getBean("myBean");

        // expects null because MyFactoryBean.myBean returns null,
        // however, I got a NullBean instance here
        Assert.assertNull(myBean);

        ctx.close();
    }

}

class MyBean {
}

class MyFactoryBean {

    @Bean
    public MyBean myBean() {
        return null; // in real project cases, return value might be null or not null that depends.
    }

}

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: backportedAn issue that has been backported to maintenance branchestype: bugA general bugtype: documentationA documentation task

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions