2017-12-19 3 views
1

サポートされているかどうかわかりませんが、偶然@Import@Configurationを持たないスプリングコンフィグレーションになっています。結果として、私は明らかに事件ではない(少なくとも私はそれを認識することができません)謎の循環依存性のエラーが発生しました。@Configurationを持たないバネでコンフィグレーションクラスをインポートする

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'beanC' defined in URL[jar:file:/Users/ghornyak/work/dev/sandbox/spring-boot-tutorial/build/libs/gs-spring-boot-0.1.0.jar!/BOOT-INF/classes!/hello/BeanC.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is 
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'hello.config.TestConfig': Unsatisfied dependency expressed through method 'beanB' parameter 0; nested exception is 
org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'beanA': Requested bean is currently in creation: Is there an unresolvable circular reference? 

これは簡単な例です::BeanBBeanCBeanABeanAに依存し、BeanB両方が@Configuration注釈が欠落TestConfigクラスで定義されていますこれは私が実際に取得しない、エラーメッセージ、です。

設定のソース:

package hello.config; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Bean; 

import hello.BeanA; 
import hello.BeanB; 

public class TestConfig { 

    @Bean 
    public BeanA beanA() { 
     System.out.println("Creating beanA"); 
     return new BeanA(); 
    } 

    @Bean 
    @Autowired 
    public BeanB beanB(BeanA beanA) { 
     System.out.println("Creating beanB: " + beanA); 
     return new BeanB(); 
    } 
} 

BeanCの定義:

package hello; 

import org.springframework.stereotype.Component; 

@Component 
public class BeanC { 

    public BeanC(BeanA beanA) { 
     System.out.println("Creating BeanC: " + beanA); 
    } 
} 

テストアプリケーション:今すぐ

package hello; 


import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Import; 

import hello.config.TestConfig; 

@SpringBootApplication 
@Import(TestConfig.class) 
@ComponentScan(basePackageClasses = Application.class) 
public class Application { 

    public static void main(String[] args) { 
     SpringApplication.run(Application.class, args); 
    } 
} 

、私は@Configurationで設定をマークした場合アノテーションはエラーがなくなり、すべてが期待通りに機能します。ですから、インポートされた設定クラスに前述の注釈があるべきかどうかという疑問があります。もしそうなら、なぜそれが間違っているのではないのでしょうか?

答えて

1

はい、あなたは@Configurationでそのクラスに注釈を付けない限り、同じクラスの別のBean定義の中でBeanを呼び出すことはできません。その理由は、春が参照してBeanを呼び出す必要があるAOPプロキシを作成するcglibです

通常、@Beanメソッドは@Configurationクラス内で宣言されます。 この場合、Beanメソッドは同じクラスの他の@Beanメソッドを同じクラスで参照する可能性があります。これにより、豆の間の参照 が強く型付けされ、ナビゲート可能であることが保証される。このようないわゆる の 'Bean間参照'は、getBean()ルックアップのように、スコープとAOP セマンティクスを保証することが保証されています。これらは、オリジナルの 'Spring JavaConfig'プロジェクトから知られているセマンティクス です。実行時には、それぞれのコンフィグレーションクラスのCGLIBサブクラス化が 必要です。 その結果、@Configurationクラスとそのファクトリメソッドは、最終的またはプライベートとしてマークされていない 必要があり、このモードでは

チェックそれは私の問題に関連している場合、このlink

+0

が、私は、ユーザーではないよ、私はドンメソッドを参照するには、メソッドのパラメータに '@ Autowire'を使用してください。 – Katona

関連する問題