Java 将内部bean添加到应用程序上下文不会抛出任何默认构造函数

Java 将内部bean添加到应用程序上下文不会抛出任何默认构造函数,java,spring,dynamic,dependency-injection,Java,Spring,Dynamic,Dependency Injection,我试图通过编程方式在jUnit测试中将内部bean添加到我的应用程序上下文中。我不想通过使用@Component注释bean来污染我的上下文,因为它会影响在同一上下文中运行的所有其他测试 public class PatchBaseImplTest extends TestBase{ /** * Sample test patch to modify the schema */ public class SchemaUpdatePatch extends P

我试图通过编程方式在jUnit测试中将内部bean添加到我的应用程序上下文中。我不想通过使用
@Component
注释bean来污染我的上下文,因为它会影响在同一上下文中运行的所有其他测试

public class PatchBaseImplTest extends TestBase{

    /**
     * Sample test patch to modify the schema
     */
    public class SchemaUpdatePatch extends PatchBaseImpl {
        public SchemaUpdatePatch(){
            super();
        }

        @Override
        public void applyPatch() throws Exception {
        }
    };

    @Before
    public void setUp(){
        // add patch to context
        beanRegistry.registerBeanDefinition("SchemaUpdatePatch",  SchemaUpdatePatch.class,  BeanDefinition.SCOPE_PROTOTYPE);
        schemaPatch = (Patch)applicationContext.getBean("SchemaUpdatePatch", SchemaUpdatePatch.class);

    }
}
其中,registerBeanDefinition定义为:

    public void registerBeanDefinition( String name, Class clazz, String scope){
        GenericBeanDefinition definition = new GenericBeanDefinition();
        definition.setBeanClass(clazz);
        definition.setScope(scope);
        definition.setAutowireCandidate(true);
        definition.setAutowireMode(GenericBeanDefinition.AUTOWIRE_BY_TYPE);

        registry.registerBeanDefinition(name,  definition);
    }
我可以看到bean defn已经添加到应用程序上下文中,但是当我尝试使用appContext.getBean()检索bean时,Spring抛出了类缺少构造函数的错误:

Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ia.system.patch.PatchBaseImplTest$SchemaUpdatePatch]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.ia.system.patch.PatchBaseImplTest$SchemaUpdatePatch.<init>()
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:83)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1000)
    ... 35 more
Caused by: java.lang.NoSuchMethodException: com.ia.system.patch.PatchBaseImplTest$SchemaUpdatePatch.<init>()
    at java.lang.Class.getConstructor0(Class.java:2800)
    at java.lang.Class.getDeclaredConstructor(Class.java:2043)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:78)
    ... 36 more
原因:org.springframework.beans.BeanInstantiationException:无法实例化bean类[com.ia.system.patch.patchBaseImplatest$SchemaUpdatePatch]:未找到默认构造函数;嵌套异常为java.lang.NoSuchMethodException:com.ia.system.patch.PatchBaseImpletst$SchemaUpdatePatch。()
位于org.springframework.beans.factory.support.SimpleInstallationStrategy.instantiate(SimpleInstallationStrategy.java:83)
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.InstanceBean(AbstractAutowireCapableBeanFactory.java:1000)
... 35多
原因:java.lang.NoSuchMethodException:com.ia.system.patch.PatchBaseImplTest$SchemaUpdatePatch。()
位于java.lang.Class.getConstructor0(Class.java:2800)
位于java.lang.Class.getDeclaredConstructor(Class.java:2043)
位于org.springframework.beans.factory.support.SimpleInstallationStrategy.instantiate(SimpleInstallationStrategy.java:78)
... 36多
我曾尝试向SchemaUpdatePatch类添加默认构造函数,但这似乎并不重要

但是,如果我用@Component注释它,而不是通过编程将它添加到上下文中,并尝试通过applicationContext.getBean()访问它,那么它就可以正常工作


以编程方式将此bean添加到applicationContext的正确方法是什么?我的一般定义错了吗?我是否遗漏了一些东西来说明构造函数是什么?

写这篇文章实际上是一种宣泄。帮助我找到我的错误。必须使内部类保持静态,否则Spring无法实例化它。希望这能在将来帮助其他人

即:

/**
 * Sample test patch to modify the schema
 */
static public class SchemaUpdatePatch extends PatchBaseImpl {
    public SchemaUpdatePatch(){
        super();
    }

    @Override
    public void applyPatch() throws Exception {
    }
};