[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
bean的实例化是spring中重要的一环,涉及了bean的生命周期。
实例化方法主要在preInstantiateSingletons中
首先获取beanName
遍历beanName,根据beanName查出beanDefinition
如果bean是单例,并且未设置懒加载,并且是不能被实例化的,则继续下面的判断:
如果bean是factoryBean,则通过&+beanName 实例化并获取bean。
如果不是,则通过beanName 实例化并获取bean
实际获取bean的方法
如果name以&开头,也就是通过factorybean定义bean,去掉开头的&
查询bean缓存中是否存在该bean,
首先查询singletonObjects中是否存在该bean
如果singletonObjects中不存在,并且该bean正在处于实例化状态中,则查询earlySingletonObjects,
如果earlySingletonObjects中没有该bean,并且允许提前暴露引用,加锁再次判断singletonObjects、earlySingletonObjects中是否存在bean,如果没有则singletonFactory获取。
这里如果bean中存在循环引用,会在singletonFactory获取到实例化的bean缓存,
父容器中查找该bean
如果设置了依赖bean,也就是dependsOn,先初始化依赖bean
创建单例bean,传入了一个ObjectFactory,里面定义了createbean方法
先查询singletonObjects缓存中是否存在bean,存在则直接返回
执行beforeSingletonCreation方法,检查bean是否已在创建,避免循环依赖,并将bean标识为创建中
singlefactory中获取bean
确定是带参数的构造器,还是默认的无参数构造器
创建bean实例,使用无参数构造器
创建完bean对象,将其装为beanwrapper
将beanName作为key,添加factory缓存,用作循环引用时的处理。
解析bean中的属性依赖,会触发bean的初始化。
解析的属性,放入deepCopy中
为依赖属性赋值
回调bean生命周期中设置的各个方法
通过判定是否是接口实例,回调顺序:
1.beanNameAware
2.beanClassLoaderAware
3.BeanFactoryAware
4.挨个调用beanPostProcess的postProcessBeforeInitialization方法
5.调用InitializingBean的afterPropertiesSet方法
6.调用自定义的initMethod方法
7.调用BeanPostProcessor的postProcessAfterInitialization方法
将beanName从singletonsCurrentlyInCreation移除,说明bean已创建完成
将bean实例缓存,以后就可以从缓存中取出单例bean 了。