高频:spring知识

1、bean的生命周期?

主要阶段

初始化

org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh

信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@67424e82: startup date []; root of context hierarchy

org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

信息: Loading XML bean definitions from class path resource [spring-bean.xml]

{BeanFactoryPostProcessor} 【Constructor】 Method

{BeanFactoryPostProcessor} 【postProcessBeanFactory】 Method

{InstantiationAwareBeanPostProcessor} 【Constructor】 Method

{BeanPostProcessor} 【Constructor】 Method

{InstantiationAwareBeanPostProcessor} 【postProcess[Before]Instantiation】 Method

{AccountBean} 【Constructor】 Method

{InstantiationAwareBeanPostProcessor} 【postProcess[After]Instantiation】 Method

{InstantiationAwareBeanPostProcessor} 【postProcessPropertyValues】 Method,[Bean] = AccountBean{accountNo='null', openDate='null'}, [BeanName] = accountBean

{AccountBean} 的【setAccountNo】方法

{AccountBean} 的【setOpenDate】方法

{BeanNameAware} 的【setBeanName】方法

{BeanFactoryAware} 的【setBeanFactory】方法

{InstantiationAwareBeanPostProcessor} 【postProcess[Before]Initialization】 Method

{BeanPostProcessor} 的【postProcess[Before]Initialization】方法【beanName】: accountBean

{InitializingBean} 的【afterPropertiesSet】方法

{AccountBean} 的【init-method】方法

{InstantiationAwareBeanPostProcessor} 【postProcess[After]Initialization】Method

{BeanPostProcessor} 的【postProcess[After]Initialization】方法【beanName】: accountBean

AccountBean{accountNo='00001', openDate='2017-12-05'}

关闭

org.springframework.context.support.ClassPathXmlApplicationContext doClose

信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@67424e82: startup date [Sun Dec 10 15:35:01 CST 2017]; root of context hierarchy

{DisposableBean} 的【destroy】方法

{AccountBean} 的【destroy-method】方法

2、三级缓存是如何解决循环依赖的?

三级缓存

Spring 三级缓存指的是 Spring 在解决循环依赖问题时所采用的三级缓存机制,分别是:

1. 一级缓存:单例对象缓存(Singleton Objects Cache),用于存储已经创建好的单例 Bean 实例。缓存键为 Bean 名称,值为 Bean 实例。

2. 二级缓存:早期单例对象缓存(Early Singleton Objects Cache),用于存储已经实例化但尚未完成初始化的早期 Bean 实例。缓存键为 Bean 名称,值为 Bean 实例。

3. 三级缓存:单例对象工厂缓存(Singleton Objects Factory Cache),用于存储创建单例 Bean 实例的工厂对象。缓存键为 Bean 名称,值为 ObjectFactory 实例。

这三级缓存协同工作,解决了 Spring 容器中单例 Bean 的循环依赖问题。

执行流程

在 Spring 创建 Bean 的过程中,会在以下阶段将 Bean 放入三级缓存:

1. 一级缓存:当 Bean 完全创建好并完成初始化后,Spring 会将其放入一级缓存。这意味着 Bean 已经被填充了属性值,执行了初始化方法(如 `@PostConstruct` 注解的方法或 `afterPropertiesSet()` 方法),并且应用了相关的 Bean 后置处理器。

2. 二级缓存:当 Bean 实例化完成(通过构造器创建实例)但尚未完成初始化时,Spring 会将其放入二级缓存。这意味着 Bean 已经创建了实例,但尚未填充属性值和执行初始化方法。

3. 三级缓存:在 Bean 实例化之前,Spring 会将创建 Bean 实例的工厂对象(`ObjectFactory`)放入三级缓存。这个工厂对象负责创建和初始化 Bean 实例。

3、AOP实现原理?

什么是AOP?

  1. AOP 是 Aspect Oriented Programming(面向切面编程) 的简称,和OOP(面向对象编程)一样是一种编程思想,是对OOP的一种补充。

  2. AOP旨在将横切关注点(crosscutting concern)从业务主体逻辑中进行剥离,实现关注点分离,以提高程序的模块化程度(及业务模块只需关注业务逻辑,无需关注日志、安全、事务等通用逻辑)

Spring AOP的动态代理有2种实现方式

  1. JDK动态代理(JDKDynamicAopProxy):基于接口实现,底层基于反射

  2. CGLIB动态代理(CGLIBAopProxy):基于继承实现

Spring 如何选择动态代理方式 ?(以Spring 4.3.17为例)

源码见:DefaultAopProxyFactory - > createAopProxy

  • 当代理对象实现了接口时,Spring默认使用JDK的动态代理。

  • 当Bean没有实现接口时,Spring默认使用CGLIB动态代理。

  • 当代理对象实现了接口时,也可以强制使用CGLIB(在spring配置中加入<aop:aspectj-autoproxy proxy-target-class="true"/>)

  • 18
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值