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

はい、分かりました。@ComponentRedis クラスの注釈を削除しましたが、機能します。その注釈が何をするのかをよりよく理解するために、その注釈について読み直す必要があると思います。

于 2013-06-07T10:21:29.360 に答える
0

例外では、デフォルトのコンストラクターまたは init() のいずれかが必要であることが明確に示されています。それを追加してみてください。うまくいくはずです。

スレッド「メイン」での例外 org.springframework.beans.factory.BeanCreationException: ファイルで定義された名前 'redis' を持つ Bean の作成中にエラーが発生しました [Redis.class]: Bean のインスタンス化に失敗しました。ネストされた例外は org.springframework.beans.BeanInstantiationException: Bean クラスをインスタンス化できませんでした [Redis]:デフォルトのコンストラクターが見つかりません。ネストされた例外は java.lang.NoSuchMethodException: Redis.() です

于 2013-06-07T10:26:34.497 に答える