且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法使用Spring创建Innerbean - BeanInstantiationException找不到默认构造函数

更新时间:2023-11-20 17:35:04

这是Java的一个警告 - 内部类默认构造函数不是无参数构造函数。它们的默认构造函数接受1个类型的参数 - 外部类。

That's one caveat of Java - inner classes default constructor isn't a no-arg constructor. Their default constructor takes 1 parameter of type - the outer class.

因此,使用< constructor-arg> 传递类型为的Bean> ExampleBean

So, use <constructor-arg> to pass a bean of type ExampleBean

当然,只有在必要时才使用非静态内部类。如果该类仅是您显示的类,则将其设为 static 。或者将其移动到新文件。那你就没有上述限制了。首选静态内部类不仅适用于spring bean,而且适用于Java。

Of course, use non-static inner classes only if this is necessary. If the class is only what you have shown, then make it static. Or move it to a new file. Then you won't have the above restriction. Preferring static inner classes is a good practice not only for spring beans, but in Java in general.