サポートされているかどうかわかりませんが、偶然@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?
これは簡単な例です::BeanB
とBeanC
BeanA
とBeanA
に依存し、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
で設定をマークした場合アノテーションはエラーがなくなり、すべてが期待通りに機能します。ですから、インポートされた設定クラスに前述の注釈があるべきかどうかという疑問があります。もしそうなら、なぜそれが間違っているのではないのでしょうか?
が、私は、ユーザーではないよ、私はドンメソッドを参照するには、メソッドのパラメータに '@ Autowire'を使用してください。 – Katona