Spring希望用@Resource调用默认构造函数

时间:2013-06-07 08:59:38

标签: java spring

所以我在使用Bean with Spring时遇到了麻烦。

这是我配置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")
        );
    }
}

这就是我在主应用类中使用它的方式:

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

这是我启动程序时得到的例外

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

如果我正确阅读文档,@ Resource会使用该字段的名称来确定要加载哪个Bean,所以它应该找到我的bean“redisService”,不是吗?

我错过了一些明显的东西吗?

感谢。

2 个答案:

答案 0 :(得分:1)

好的,我明白了。我删除了Redis类上的@Component注释,它可以正常工作。 我想我需要重新阅读该注释以更好地理解它的作用。

答案 1 :(得分:0)

在Exception中,显然它需要默认构造函数或init()。请尝试添加它,它应该工作。

线程“main”中的异常org.springframework.beans.factory.BeanCreationException:创建文件[Redis.class]中定义名为'redis'的bean时出错:bean的实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化bean类[Redis]:找不到默认构造函数;嵌套异常是java.lang.NoSuchMethodException:Redis。()