Adding inner bean to Application Context throws No default constructor found execption

Eric B.

I'm trying to programatically add a inner bean to my application context within a jUnit test. I do not want to pollute my context by having the bean annotated with @Component as it will affect all other tests that run within the same context.

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);

    }
}

where registerBeanDefinition is defined as:

    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);
    }

I can see that the bean defn has been added to the application context, but when I try to retrieve the bean using appContext.getBean() Spring throws errors that the class is missing a constructor:

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

I've tried adding a default constructor to the SchemaUpdatePatch class, but it does not seem to matter.

If however, I annotate it with @Component instead of adding it to the context programatically, and try to access it via applicationContext.getBean(), it works fine.

What is the correct way of adding this bean to the applicationContext programatically? Is my GenericBeanDefinition wrong? Am I missing something to specify what the constructor is?

Eric B.

Writing up this post was actually cathartic. Helped me find my bug/error. Have to make the inner class Static or Spring cannot instantiate it. Hopefully this may help someone else in the future.

ie:

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

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Autowire a default implementation if implemenation bean is not found

XML Context based bean cannot perform constructor injection : No default constructor found

Hibernate: No default constructor for entity Inner Class

Constructor required a bean type that could not be found

Adding a Pre-constructed Bean to a Spring Application Context

What is Application context and bean factory in spring framework

Spring MVC no default constructor found?

No default constructor for entity for inner class in Hibernate

How to create a Spring bean from a static inner class constructor?

Spring @Autowired constructor gives No default constructor found

Exclude EurekaClient Bean from Application Context in SpringBootApplicationTest

Adding a repository throws bean creation error

Parameter 0 of constructor required a bean that could not be found

Kotlin Spring cannot register bean in application context

Must a Bean have a default constructor?

Error creating bean with name 'application', No default constructor found; nested exception is java.lang.NoSuchMethodException

Get List of bean types from application context

Creating a "default constructor" for inner sub-interfaces

Default constructor not found

No Bean Found in Spring Boot application context

Adding Plugins to Application Context in Spring

Registered RendezvousChannel bean cannot be found within Spring Application Context

Unable to use @Autowired in @Configuration constructor, bean not found

Why Spring throws exceptions when No default constructor is found

Class bean in application context for Cacheable annotation

How to set default group in bean validation context

Spring Application context not found

Parameter 0 of constructor in 'Controller' required a bean of type 'service' that could not be found. Not working: adding annotations, componentscans

Spring bean dependencies in the application context form a cycle