1

所以我在使用带有 Spring 的 Bean 时遇到了麻烦。

这就是我配置 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”,不是吗?

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

谢谢。

4

2 回答 2

1

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

于 2013-06-07T10:21:29.360 回答
0

在异常中,它清楚地表明它需要默认构造函数或 init()。请尝试添加它,它应该可以工作。

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

于 2013-06-07T10:26:34.497 回答