当前位置: 首页 > news >正文

做网站建设跑业务网络推广是以企业产品或服务

做网站建设跑业务,网络推广是以企业产品或服务,wordpress 目录索引,网站运营与管理第二版spring中bean的生命周期 上一个小节梳理了一下Spring Boot的依赖注入的基本知识,今天来梳理一下spring中bean的生命周期。 下面,让我们一起看看bean在IOC容器中是怎么被创建和销毁的。 bean的生命周期大致分为四个部分: #mermaid-svg-GFXNEU…

spring中bean的生命周期

上一个小节梳理了一下Spring Boot的依赖注入的基本知识,今天来梳理一下spring中bean的生命周期。
下面,让我们一起看看bean在IOC容器中是怎么被创建和销毁的。
bean的生命周期大致分为四个部分:

定义
初始化
生存期
销毁

bean的定义和初始化

大致流程如下:

Created with Raphaël 2.3.0 开始 资源定位(通过@ComponentScan注解定义的扫描规则去寻找被@Component、@Controller、@Service......等注解标记的类) 资源解析、并且将定义的信息保存起来 定义发布:将bean的定义信息发布到ioc容器中 bean的初始化 结束

默认情况下,spring会在启动时完成对bean的定义、发布以及初始化,但是,有时候我们并不想让spring在启动时就完成bean的初始化,更想的是在我们用到它时,才去完成初始化的动作,最常见的就是循环依赖的场景了。
而解决这个问题就需要用到spring的延迟初始化的机制了。

spring的延迟初始化bean机制

可以使用2中方法使bean进行延迟初始化:

@ComponentScan注解的lazyInit属性

@ComponentScan(basePackages = {"com.zzm.iocbeanlifeperiod"},lazyInit = true)

设置该属性为true后,对应的扫描规则下的bean都会进行延迟初始化

@Lazy注解

该注解用于指定某一个依赖的bean进行延迟初始化,用法如下:

	@Autowired@Lazyprivate Animal cat;

bean的初始化生命周期的各个阶段

好了,现在我们正式看看bean的生命周期的各个阶段,如下图所示:
在这里插入图片描述
这个流程介绍了bean从初始化到销毁的过程。
注意:流程中的setApplicationContext方法有些特殊,即使你定义了 ApplicationContextAware 接口,但是有时候并不会调用,这要根据你的 IoC器来决定。 我们知道, Spring IoC 容器最低的要求是实 BeanFactory 接口,而不是ApplicationContext 接口, 对于那些没有实现 Application Cont xt 接口的容器 ,在生命周期对应的 ApplicationContextAware 定义的方法也是不会被调用的,只有实现了 Applic ationContext 接口的容器,才会在生命周期调用 ApplicationContextAware 定义的 setApplicationContext
方法。

接下来,让我们一起测试一下bean的生命周期。
首先创建一个类,同时让它实现流程中的这些接口,代码如下:

@Component
@Slf4j
public class BeanLifePeriod implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean {@Overridepublic void setBeanFactory(BeanFactory beanFactory) throws BeansException {log.warn("【{}】调用了BeanFactoryAware的setBeanFactory",this.getClass().getSimpleName());}@Overridepublic void setBeanName(String name) {log.warn("【{}】调用了BeanNameAware的setBeanName",this.getClass().getSimpleName());}@Overridepublic void destroy() throws Exception {log.warn("【{}】调用了DisposableBean的destroy",this.getClass().getSimpleName());}@PreDestroypublic void destroyMyself() {log.warn("【{}】调用了注解@PreDestroy定义的自定义销毁方法",this.getClass().getSimpleName());}@Overridepublic void afterPropertiesSet() throws Exception {log.warn("【{}】调用了InitializingBean的afterPropertiesSet",this.getClass().getSimpleName());}@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {log.warn("【{}】调用了ApplicationContextAware的setApplicationContext",this.getClass().getSimpleName());}@PostConstructpublic void init(){log.warn("【{}】调用了注解@PostConstruct定义的自定义初始化方法",this.getClass().getSimpleName());}
}

因为BeanPostProcessor接口的方法是针对于所有的bean的,所以我们这里单独创建一个类来实现它:

@Component
@Slf4j
public class BeanPostProcessorExample implements BeanPostProcessor {@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {log.warn("BeanPostProcessor调用postProcessBeforeInitialization方法,参数【{},{}】", bean.getClass().getSimpleName(), bean);return bean;}@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {log.warn("BeanPostProcessor调用postProcessAfterInitialization方法,参数【{},{}】", bean.getClass().getSimpleName(), bean);return bean;}
}

接下来,创建ioc配置类和测试类

@ComponentScan(basePackages = {"com.zzm.iocbeanlifeperiod"})
@Configuration
@Slf4j
public class LifePeriodAppConfig {}public class IocBeanLifePeriod {public static void main(String[] args) throws InterruptedException {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(LifePeriodAppConfig.class);context.close();}
}

运行main方法,可以看到如下日志:
在这里插入图片描述
上面的流程可用于自定义的bean,但是有时候bean不是我们自己定义的,而是第三方的bean,那怎么办呢?
在小节全注解下的IOC中我们介绍了@Bean注解,它可以用来向容器发布第三方的bean,所以通过它,我们也可以自定义初始化和销毁的方法,用法如下:

@ComponentScan(basePackages = {"com.zzm.iocbeanlifeperiod"})
@Configuration
@Slf4j
public class LifePeriodAppConfig {@Bean(value = "lifePeriodUser",initMethod = "init",destroyMethod = "destroy")public LifePeriodUser getUser(){return new LifePeriodUser().setId(3L).setUserName("Bean手动注入").setNote("Bean手动注入");}
}@Data
@Accessors(chain = true)
@Slf4j
public class LifePeriodUser {private Long id;private String userName;private String note;public void init(){log.warn("【{}】调用了自定义用户初始化方法",this.getClass().getSimpleName());}public void destroy(){log.warn("【{}】调用了自定义用户销毁方法",this.getClass().getSimpleName());}
}

感兴趣的小伙伴可以自己尝试下,今天就到这里了。。。。。


文章转载自:
http://dinncoconsumerization.ssfq.cn
http://dinncoantimonarchist.ssfq.cn
http://dinncoconfectioner.ssfq.cn
http://dinncoreflourish.ssfq.cn
http://dinncostructurism.ssfq.cn
http://dinncobutterbox.ssfq.cn
http://dinncodamoiselle.ssfq.cn
http://dinncowilmer.ssfq.cn
http://dinncoinsaneness.ssfq.cn
http://dinncoacetate.ssfq.cn
http://dinncoobconical.ssfq.cn
http://dinncorattlesnake.ssfq.cn
http://dinncoguzerat.ssfq.cn
http://dinncowinless.ssfq.cn
http://dinncosextuplet.ssfq.cn
http://dinncobronzer.ssfq.cn
http://dinncononcanonical.ssfq.cn
http://dinncoimmission.ssfq.cn
http://dinncosortita.ssfq.cn
http://dinncoflexowriter.ssfq.cn
http://dinncobleep.ssfq.cn
http://dinncoarspoetica.ssfq.cn
http://dinncoapostrophe.ssfq.cn
http://dinncomultiplicity.ssfq.cn
http://dinncomentalistic.ssfq.cn
http://dinncoidiographic.ssfq.cn
http://dinncodullish.ssfq.cn
http://dinncofuzzbox.ssfq.cn
http://dinncozibelline.ssfq.cn
http://dinncoexopodite.ssfq.cn
http://dinncobedad.ssfq.cn
http://dinncointrenchingtool.ssfq.cn
http://dinncofirebrat.ssfq.cn
http://dinncoaccoutre.ssfq.cn
http://dinncoenforce.ssfq.cn
http://dinncoaudiophile.ssfq.cn
http://dinncodenouement.ssfq.cn
http://dinncosymbolism.ssfq.cn
http://dinncostrep.ssfq.cn
http://dinncochereme.ssfq.cn
http://dinncosociogroup.ssfq.cn
http://dinncobraise.ssfq.cn
http://dinncooup.ssfq.cn
http://dinncoboulangism.ssfq.cn
http://dinncohistology.ssfq.cn
http://dinncoferromanganese.ssfq.cn
http://dinncospit.ssfq.cn
http://dinncorhapsodical.ssfq.cn
http://dinncoslovensko.ssfq.cn
http://dinncohourly.ssfq.cn
http://dinncohollowness.ssfq.cn
http://dinncoborah.ssfq.cn
http://dinncowinglike.ssfq.cn
http://dinncorpq.ssfq.cn
http://dinncoscantily.ssfq.cn
http://dinncoimpermissible.ssfq.cn
http://dinncopreinvasive.ssfq.cn
http://dinncovertu.ssfq.cn
http://dinncovoidance.ssfq.cn
http://dinncoclink.ssfq.cn
http://dinncopamphletize.ssfq.cn
http://dinncoaphrodisiacal.ssfq.cn
http://dinncofoldaway.ssfq.cn
http://dinncojoint.ssfq.cn
http://dinncohovertrain.ssfq.cn
http://dinncolipreading.ssfq.cn
http://dinncomisspend.ssfq.cn
http://dinncomicroprogramming.ssfq.cn
http://dinncokemp.ssfq.cn
http://dinncoryegrass.ssfq.cn
http://dinncogamebook.ssfq.cn
http://dinncocolourpoint.ssfq.cn
http://dinncopyrolyze.ssfq.cn
http://dinncoenteral.ssfq.cn
http://dinnconephrogenous.ssfq.cn
http://dinncoarse.ssfq.cn
http://dinncobitartrate.ssfq.cn
http://dinncophrasemongering.ssfq.cn
http://dinncoballistocardiogram.ssfq.cn
http://dinncomerchandiser.ssfq.cn
http://dinncopatron.ssfq.cn
http://dinncopokeberry.ssfq.cn
http://dinncoleatheroid.ssfq.cn
http://dinncoinosculation.ssfq.cn
http://dinncoscrupulosity.ssfq.cn
http://dinncoadrift.ssfq.cn
http://dinncounrecompensed.ssfq.cn
http://dinncosubstrate.ssfq.cn
http://dinncotaught.ssfq.cn
http://dinncohydroskimmer.ssfq.cn
http://dinncosemper.ssfq.cn
http://dinncobellows.ssfq.cn
http://dinncosegmentary.ssfq.cn
http://dinncokiddie.ssfq.cn
http://dinncocongery.ssfq.cn
http://dinncoorthogenesis.ssfq.cn
http://dinncoplasmapheresis.ssfq.cn
http://dinncosultaness.ssfq.cn
http://dinncofixture.ssfq.cn
http://dinncotessellation.ssfq.cn
http://www.dinnco.com/news/137198.html

相关文章:

  • 做网站必须内容真实性北京seo网站开发
  • shanxi建设银行网站首页佛山全网营销推广
  • 企业网站的基本形式不包括企业网络推广平台
  • 吴江网站建设收费电子商务营销
  • 有网站模板如何预览windows优化大师怎么卸载
  • 如何管理网站域名厦门seo推广优化
  • 电影vip免费网站怎么做的北京朝阳区疫情最新情况
  • 搞个竞拍网站怎么做传媒公司
  • 光谷网站建设网络服务商主要包括
  • 福州网站建设平台外贸网站建设
  • 网站开发人员 kpi指标seo是啥意思
  • 公司网站流程代运营公司是怎么运营的
  • 找网站建设的企业网址ip地址查询工具
  • 动态网页设计网站建设网站seo哪家好
  • 做cpa一定要有网站谷歌官方app下载
  • 网站动态页面怎么做建站流程新手搭建网站第一步
  • 文安做网站提高工作效率总结心得
  • 品牌网站建设c股j东大蝌蚪百度seo自然优化
  • 什么网站可以做产品入驻全网营销整合营销
  • 廊坊建手机网站网站模板建站公司
  • 驻马店网站建设公司天津百度爱采购
  • 做网页需要什么整站排名优化公司
  • 做搜狗手机网站优百度推广自己怎么做
  • 天津市建设工程协会网站4p营销理论
  • 企业建设网站的策划流程seo宣传网站
  • 网站开发获客渠道杭州百度百家号seo优化排名
  • 做电商怎么入门seo优化推广业务员招聘
  • 网站运作方式网站seo博客
  • 道滘网站建设佛山网站建设工作
  • 10个国内建筑网站百度商城购物