Vincent Rischmann
Vincent Rischmann

Reputation: 122

Spring wants to call default constructor with @Resource

so I'm having trouble using a Bean with Spring.

This is how I configure the Bean.

@Configuration
@ComponentScan("com.mypackage")
public class BeanConfig {
    @Bean
    public Redis redisService() {
        return new Redis(
            config().getString("redis.master.host"),
            config().getInt("redis.master.port")
        );
    }
}

This is how I use it in my main app class:

@Component
public class App {
    @Resource
    private Redis redisService;

    public static void main(String args[]) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanConfig.class);

        App app = applicationContext.getBean(App.class);
        app.start();
    }
}

And this is the exception I get when a start my program

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redis' defined in file [Redis.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [Redis]: No default constructor found; nested exception is java.lang.NoSuchMethodException: Redis.<init>()
    at   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1007)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:953)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:487)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:73)
    at App.main(App.java:33)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [Redis]: No default constructor found; nested exception is java.lang.NoSuchMethodException: Redis.<init>()
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:83)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1000)
    ... 17 more
Caused by: java.lang.NoSuchMethodException: Redis.<init>()
    at java.lang.Class.getConstructor0(Class.java:2730)
    at java.lang.Class.getDeclaredConstructor(Class.java:2004)
    at   org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:78)
    ... 18 more

If I read the documentation correctly, the @Resource will use the name of the field to determine which Bean to load, so it should find my bean "redisService", no ?

Am I missing something obvious ?

Thanks.

Upvotes: 1

Views: 1864

Answers (2)

Harish Kumar
Harish Kumar

Reputation: 528

In the Exception it is clearly saying it needs either default constructor or init(). Please try to add that and it should work.

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redis' defined in file [Redis.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [Redis]: No default constructor found; nested exception is java.lang.NoSuchMethodException: Redis.()

Upvotes: 0

Vincent Rischmann
Vincent Rischmann

Reputation: 122

Ok, I got it. I removed the @Component annotation on my Redis class, and it works. I think I need to reread about that annotation to better understand what it does.

Upvotes: 1

Related Questions