国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

通過(guò)實(shí)例解析spring對(duì)象生命周期

瀏覽:35日期:2023-09-14 08:52:18

1、生命周期-@Bean指定初始化和銷(xiāo)毀方法

配置時(shí)指定初始化及銷(xiāo)毀方法:

通過(guò)實(shí)例解析spring對(duì)象生命周期

Bean中提供對(duì)應(yīng)的初始化及銷(xiāo)毀方法:

package com.atguigu.bean; import org.springframework.stereotype.Component; @Componentpublic class Car { public Car(){ System.out.println('car constructor...'); } public void init(){ System.out.println('car ... init...'); } public void detory(){ System.out.println('car ... detory...'); } }

2、生命周期-InitializingBean和DisposableBean

通過(guò)讓Bean實(shí)現(xiàn)InitializingBean(定義初始化邏輯),DisposableBean(定義銷(xiāo)毀邏輯);

package com.atguigu.bean; import org.springframework.beans.factory.DisposableBean;import org.springframework.beans.factory.InitializingBean;import org.springframework.stereotype.Component; @Componentpublic class Cat implements InitializingBean,DisposableBean { public Cat(){ System.out.println('cat constructor...'); } public void destroy() throws Exception { // TODO Auto-generated method stub } public void afterPropertiesSet() throws Exception { // TODO Auto-generated method stub }}

3、生命周期-@PostConstruct&@PreDestroy

可以使用JSR250:

@PostConstruct:在bean創(chuàng)建完成并且屬性賦值完成;來(lái)執(zhí)行初始化方法

@PreDestroy:在容器銷(xiāo)毀bean之前通知我們進(jìn)行清理工作

package com.atguigu.bean; import javax.annotation.PostConstruct;import javax.annotation.PreDestroy; import org.springframework.stereotype.Component; @Componentpublic class Dog{ public Dog(){ System.out.println('dog constructor...'); } //對(duì)象創(chuàng)建并賦值之后調(diào)用 @PostConstruct public void init(){ System.out.println('Dog....@PostConstruct...'); } //容器移除對(duì)象之前 @PreDestroy public void detory(){ System.out.println('Dog....@PreDestroy...'); }}

4、生命周期-BeanPostProcessor-后置處理器

BeanPostProcessor【interface】:bean的后置處理器:

在bean初始化前后進(jìn)行一些處理工作;

postProcessBeforeInitialization:在初始化之前工作

postProcessAfterInitialization:在初始化之后工作

package com.atguigu.bean; import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.BeanPostProcessor;import org.springframework.stereotype.Component; /** * 后置處理器:初始化前后進(jìn)行處理工作 * 將后置處理器加入到容器中 * @author lfy */@Componentpublic class MyBeanPostProcessor implements BeanPostProcessor { public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println('postProcessBeforeInitialization...'+beanName+'=>'+bean); return bean; } public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println('postProcessAfterInitialization...'+beanName+'=>'+bean); return bean; } }

5、生命周期-BeanPostProcessor

原理

5.1 調(diào)用鏈 

1、AnnotationConfigApplicationContext --> AnnotationConfigApplicationContext(Class<?>... componentClasses)()方法的 --> refresh()方法;2、AbstractApplicationContext類(lèi) --> refresh()方法的 --> finishBeanFactoryInitialization(beanFactory);方法3、AbstractApplicationContext類(lèi) --> finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory)方法的 --> beanFactory.preInstantiateSingletons();方法;4、DefaultListableBeanFactory類(lèi) --> preInstantiateSingletons()方法的 --> getBean(beanName);方法;5、AbstractBeanFactory類(lèi) --> getBean(String name)方法的 --> doGetBean(name, null, null, false);6、AbstractBeanFactory類(lèi) --> doGetBean(final String name, @Nullable final Class<T> requiredType,@Nullable final Object[] args, boolean typeCheckOnly)方法的 --> createBean(beanName, mbd, args); L3207、AbstractAutowireCapableBeanFactory類(lèi) --> createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)方法的 --> doCreateBean(beanName, mbdToUse, args);8、AbstractAutowireCapableBeanFactory類(lèi) --> doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args)方法。目標(biāo)執(zhí)行方法

通過(guò)實(shí)例解析spring對(duì)象生命周期

通過(guò)實(shí)例解析spring對(duì)象生命周期

5.2目標(biāo)執(zhí)行方法

如下圖所示:

1、applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);方法為對(duì)象初始化前的執(zhí)行操作;

2、exposedObject = initializeBean(beanName, exposedObject, mbd);方法為對(duì)象執(zhí)行初始化操作;

3、initializeBean(beanName, exposedObject, mbd); -->wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);方法為對(duì)象初始化后的操作。

通過(guò)實(shí)例解析spring對(duì)象生命周期

通過(guò)實(shí)例解析spring對(duì)象生命周期

5.3具體執(zhí)行

遍歷得到容器中所有的BeanPostProcessor;挨個(gè)執(zhí)行beforeInitialization,一但返回null,跳出for循環(huán),不會(huì)執(zhí)行后面的BeanPostProcessor.postProcessorsBeforeInitialization

通過(guò)實(shí)例解析spring對(duì)象生命周期

6、生命周期-BeanPostProcessor在Spring底層的使用

例:

bean賦值,注入其他組件,@Autowired,生命周期注解功能,@Async,xxx BeanPostProcessor;

所有實(shí)現(xiàn)了BeanPostProcessor接口的類(lèi)或接口都有此功能。

通過(guò)實(shí)例解析spring對(duì)象生命周期

7、生命周期-總結(jié)

/*** bean的生命周期:* bean創(chuàng)建---初始化----銷(xiāo)毀的過(guò)程* 容器管理bean的生命周期;* 我們可以自定義初始化和銷(xiāo)毀方法;容器在bean進(jìn)行到當(dāng)前生命周期的時(shí)候來(lái)調(diào)用我們自定義的初始化和銷(xiāo)毀方法** 構(gòu)造(對(duì)象創(chuàng)建)* 單實(shí)例:在容器啟動(dòng)的時(shí)候創(chuàng)建對(duì)象* 多實(shí)例:在每次獲取的時(shí)候創(chuàng)建對(duì)象** BeanPostProcessor.postProcessBeforeInitialization* 初始化:* 對(duì)象創(chuàng)建完成,并賦值好,調(diào)用初始化方法。。。* BeanPostProcessor.postProcessAfterInitialization* 銷(xiāo)毀:* 單實(shí)例:容器關(guān)閉的時(shí)候* 多實(shí)例:容器不會(huì)管理這個(gè)bean;容器不會(huì)調(diào)用銷(xiāo)毀方法;*** 遍歷得到容器中所有的BeanPostProcessor;挨個(gè)執(zhí)行beforeInitialization,* 一但返回null,跳出for循環(huán),不會(huì)執(zhí)行后面的BeanPostProcessor.postProcessorsBeforeInitialization** BeanPostProcessor原理* populateBean(beanName, mbd, instanceWrapper);給bean進(jìn)行屬性賦值* initializeBean* {* applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);* invokeInitMethods(beanName, wrappedBean, mbd);執(zhí)行自定義初始化* applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);*}**** 1)、指定初始化和銷(xiāo)毀方法;* 通過(guò)@Bean指定init-method和destroy-method;* 2)、通過(guò)讓Bean實(shí)現(xiàn)InitializingBean(定義初始化邏輯),* DisposableBean(定義銷(xiāo)毀邏輯);* 3)、可以使用JSR250;* @PostConstruct:在bean創(chuàng)建完成并且屬性賦值完成;來(lái)執(zhí)行初始化方法* @PreDestroy:在容器銷(xiāo)毀bean之前通知我們進(jìn)行清理工作* 4)、BeanPostProcessor【interface】:bean的后置處理器;* 在bean初始化前后進(jìn)行一些處理工作;* postProcessBeforeInitialization:在初始化之前工作* postProcessAfterInitialization:在初始化之后工作** Spring底層對(duì) BeanPostProcessor 的使用;* bean賦值,注入其他組件,@Autowired,生命周期注解功能,@Async,xxx BeanPostProcessor;** @author lfy**/

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 正定县| 抚州市| 高雄县| 三明市| 左权县| 象州县| 安平县| 陆河县| 霸州市| 珠海市| 牙克石市| 五华县| 云浮市| 宿迁市| 龙南县| 神池县| 岑巩县| 松滋市| 高碑店市| 澳门| 墨江| 潜江市| 绍兴县| 平泉县| 宁都县| 沁阳市| 津市市| 迁西县| 福安市| 星子县| 仙桃市| 嘉荫县| 静宁县| 西和县| 平罗县| 邓州市| 连城县| 定边县| 新化县| 西青区| 德江县|