• spring2


    循环依赖、循环调用
    循环依赖是针对成员变量----单例才可以解决setter方法循环依赖,多例是无法解决循环依赖。
    构造方法循环依赖--------无法解决,只能将构造依赖改为setter方法依赖
    setter方法循环依赖------可以解决
    循环调用是针对方法---无法解决的
    结论:
    循环调用就是A方法调用B方法,B方法调用A方法,这是一个闭环,是死循环,只能规避,无法解
    决。
     
     
    什么是循环依赖
    循环依赖-->循环引用。--->即2个或以上bean 互相持有对方,最终形成闭环。
    eg:A依赖B,B依赖C,C又依赖A。
    【注意:这里不是函数的循环调用【是个死循环,除非有终结条件】,是对象相互依赖关系】
     
    Spring中循环依赖的场景
    构造器的循环依赖
    这个Spring解决不了,只能调整配置文件,将构造函数注入方式改为 属性注入方式。
     
    循环依赖示例:
     
    public class StudentA {
    private StudentB studentB ;
    //set循环依赖
    public void setStudentB(StudentB studentB) {
    this.studentB = studentB;
    }
    public StudentA() {
    }
    //构造器循环依赖
    public StudentA(StudentB studentB) {
    this.studentB = studentB;
    }
    }
     
    public class StudentB {
    private StudentA studentA ;
    public void setStudentA(StudentA studentA) {
    this.studentA = studentA;
    }
    public StudentB() {
    }
    public StudentB(StudentA studentA) {
    this.studentA = studentA;
    }
    }
    <bean id="a" class="com.kkb.student.StudentA">
    <constructor-arg index="0" ref="b"></constructor-arg>
    </bean>
    <bean id="b" class="com.kkb.student.StudentB">
    <constructor-arg index="0" ref="a"></constructor-arg>
    </bean>
    public class Test {
    public static void main(String[] args) {
    ApplicationContext context = new
    ClassPathXmlApplicationContext("com/kkb/student/applicationContext.xml");
    //System.out.println(context.getBean("a", StudentA.class));
    }
    }
     
    singletonFactories : 单例对象工厂的cache
    earlySingletonObjects :提前暴光的单例对象的Cache 。【用于检测循环引用,与
    singletonFactories互斥】
    singletonObjects:单例对象的cache
     
    Aware接口
    Aware这个单词翻译过来就是知道,感知的意思。在spring中,它的常见的子接口,比如
    BeanNameAware、BeanFactoryAware、ApplicationContextAware接口。假设我们的类继承了
    BeanNameAware这个接口,对应这个接口有一个方法setBeanName的方法,spring在依赖注入的初
    始化阶段会调用生成对象的这个方法,把beanName传为入参传进来。一般我们在会自己写的类里面定
    义一个属性来接收这个beanName,然后这个beanName我们就可以在开发中使用了。
    这个手段的实现上spring上有两种
    bean命名空间下的Aware子接口,在依赖注入的初始化阶段,会调用invokeAwareMethods去实
    非bean命名空间下的Aware子接口,通过各自模块代码的BeanPostProcessor接口的实现类来实
     
    总结
    Aware接口的很简单,但很实用
    BeanFactory和FactoryBean的区别
    BeanFactory:工厂,是ioc容器的基础。可以管理和创建任意类型的对象。
    FactoryBean:特殊的Bean,存在于ioc容器中,也就是存在于BeanFactory。FactoryBean只能针对
    某一类bean进行创建。
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    免装版tomcat注册成windows系统服务方法
    IE下判断IE版本的语句...[if lte IE 8]……[endif]
    HTTP 消息结构
    RESTful API 设计指南
    Spring Cloud Alibaba Sentinel 流量卫兵
    SpringBoot 整合 ES 实现 CRUD 操作
    SpringBoot项目创建与第一个SSM项目示例
    SpringBoot整合邮件发送
    SpringBoot日志处理之Logback
    Java实现AES加密解密
  • 原文地址:https://www.cnblogs.com/zhouyideboke/p/16435388.html
Copyright © 2020-2023  润新知