一,循环依赖产生的原因
所谓的循环依赖是指,A 依赖 B,B 又依赖 A,它们之间形成了循环依赖。
@Component
public class A {
@Autowired
private B b;
}
2022/5/18大约 12 分钟
所谓的循环依赖是指,A 依赖 B,B 又依赖 A,它们之间形成了循环依赖。
@Component
public class A {
@Autowired
private B b;
}
来分析注解驱动事务的原理,同样的我们从@EnableTransactionManagement开始:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({TransactionManagementConfigurationSelector.class})
public @interface EnableTransactionManagement {
boolean proxyTargetClass() default false;
AdviceMode mode() default AdviceMode.PROXY; // 使用代理的方式,绝大部分情况下,我们都不会使用AspectJ的静态代理的
int order() default 2147483647;
}
加载spring的上下文:
public static void main(String[] args) {
// 加载spring上下文
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
}
目前 Spring AOP 一共有三种配置方式,Spring 做到了很好地向下兼容,所以大家可以放心使用。
@Conditional来源于spring-context包下的一个注解。Conditional中文是条件的意思,@Conditional注解它的作用是按照一定的条件进行判断,满足条件给容器注册bean:
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Conditional {
Class<? extends Condition>[] value();
}