gpt4 book ai didi

spring - 使用spring从内部类创建bean

转载 作者:行者123 更新时间:2023-12-05 00:57:57 25 4
gpt4 key购买 nike

我试图在下面定义一个类。 Spring 没有实例化 bean。

    @Component
public class A{
@Component
public class B{
}
}

有什么方法可以让 Spring 像上面那样创建 bean。

最佳答案

我不这么认为,因为内部类不能没有它的封闭类而存在。因此它可以只能注入(inject)到封闭的 Bean 中。

From Spring documentations:

An inner bean definition does not require a defined id or name; the container ignores these values. It also ignores the scope flag. Inner beans are always anonymous and they are always created with the outer bean. It is not possible to inject inner beans into collaborating beans other than into the enclosing bean.



我认为只有 static 才有可能内部类而不是 not-static inner类。

这应该有效:
    @Component
public class A{
@Component
public static class B{
}
}

关于spring - 使用spring从内部类创建bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33830159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com