你能告诉我为什么在我的案例中
"simple"
bean是否有循环依赖关系?在我看来,必须抛出一个关于循环依赖性的异常!
配置类:
package config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "service")
public class AppConfig {
}
简单类:
package service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Simple {
@Autowired
private Simple simple;
public Simple getSimple() {
return simple;
}
}
发射器:
import config.AppConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import service.Simple;
public class Launcher {
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
Simple simple1 = ctx.getBean("simple", Simple.class);
System.out.println(simple1.getSimple());
}
}
应用程序的输出是“service.simple@6b53e23f”。如果我添加构造函数
@Autowired
public Simple(Simple simple) {
this.simple = simple;
}
那是个例外
"Error creating bean with name 'simple': Requested bean is currently in creation: Is there an unresolvable circular reference?"
发生。
为什么我把豆子放在
@Autowired
在战场上?