java完结--springIOC循环引用----2-1-011328

spring的循环引用?

     循环引用问题:https://blog.csdn.net/chen2526264/article/details/80673598

循环引用的简单的笔记:

-----当循环依赖的bean都是通过构造器注入依赖的时候,无论这些bean是singleton还是prototype,在获取bean的时候都会失败

-----当循环依赖的bean都是通过属性注入的时候单例作为先获取的bean的话成功

            1.都是单例的时候成功。

            2.非单例失败。

            3.先获取singleton的情况:成功;先获取非singleton的情况:失败。

-----如果多个bean存在循环依赖,在Spring容器启动后,只有当获取的第一个bean是通过属性注入依赖的singleton时,才会成功,别的情况都会失败

           1.该例中singletonA和singletonB都是单例,但是singletonA是通过构造器注入的singletonB,而singletonB是通过属性注入的singletonA。当Spring容器启动后,如果先获取singletonB就会成功。

           2.该例中mixPrototypeA是prototype,mixSingletonB都是singleton,但是mixPrototypeA是通过构造器注入的mixSingletonB,而mixSingletonB是通过属性注入的mixPrototypeA。当Spring容器启动后,如果先获取mixSingletonB就会成功。

原理:那为什么prototype不能成为打破这个环的一个点呢?原因就在于Spring容器只会对singleton保存引用,而对于prototype,并不会对其保存引用,这就导致在第3步中并不能获得之前创建的bean(因为引用不到它)。

顺序问题:当Spring容器遍历那些循环依赖的bean时,只要遍历到那种已经遍历过一次的bean,并且它们不是通过属性注入依赖的singleton时,就会直接抛出BeanCurrentlyInCreationException异常。
--------------------------------------------------------------------------------------------------------------------------------------------------------

profile:

   

  用法在bean上定义 ,bean可以是配置文件的。

  代码1:

       测试:

public class Test {
    public static void main(String[] args) {
//        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(Appconfig.class);
        annotationConfigApplicationContext.getBean(IndexDaoImpl2.class);
//        System.out.println(annotationConfigApplicationContext.getBean(IndexService.class).getClass().getSimpleName());


        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
//        annotationConfigApplicationContext.getBean(IndexDaoImpl2.class);
        annotationConfigApplicationContext.getEnvironment().setActiveProfiles("dao1");
        annotationConfigApplicationContext.register(Appconfig.class);
        annotationConfigApplicationContext.refresh();
        System.out.println(annotationConfigApplicationContext.getBean(IndexDao3.class).getClass().getSimpleName());
    }
}

切换环境。

切换配置文件:

 同理配置文件也是这么设置的。

public class Test {

    public static void main(String[] args) {
        //AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(Appconfigdao.class);
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
        annotationConfigApplicationContext.getEnvironment().setActiveProfiles("dao1");
        annotationConfigApplicationContext.register(Appconfigdao3.class);
        annotationConfigApplicationContext.refresh();
        //IndexDao dao = annotationConfigApplicationContext.getBean(IndexDao.class);
        //annotationConfigApplicationContext.getBean(IndexDaoImpl2.class);
    }

}

--------------

代码1:mvc2/dao3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值