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

小程序模板做视频网站宁波网络推广优化公司

小程序模板做视频网站,宁波网络推广优化公司,购物平台取名字,做企业网站的合同​🌈 个人主页:danci_ 🔥 系列专栏:《设计模式》《MYSQL》 💪🏻 制定明确可量化的目标,坚持默默的做事。 ✨欢迎加入探索MYSQL索引数据结构之旅✨ 👋 Spring框架的浩瀚海洋中&#x…

​🌈 个人主页:danci_
🔥 系列专栏:《设计模式》《MYSQL》
💪🏻 制定明确可量化的目标,坚持默默的做事。


✨欢迎加入探索MYSQL索引数据结构之旅✨

    👋 Spring框架的浩瀚海洋中,InitializingBean如同一颗闪闪发光的珍珠,等待被你发现。这一隐藏的宝藏,能够让你的Spring应用在初始化时更灵活、可控。本篇文章将带你深入探索InitializingBean的奥秘,揭示其在Spring生命周期中的重要作用,让你的开发之路更加顺畅高效。准备好开启这段奇妙的探索之旅了吗?让我们一起揭开InitializingBean的神秘面纱吧!


目录

一、环境

二、示例

2.1、说明

三、执行过程

3.1 源码解析


一、环境

  • jdk: 1.8
  • spring-boot: 2.7.1

二、示例

@Component
public class TestInit implements InitializingBean {@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("调用TestInit.afterPropertiesSet方法");}
}

运行结果:

2.1、说明

  • springboot提供InitializingBean接口,用户可以实现之来自定义初始化操作
  • 实现InitializingBean接口重写afterPropertiesSet方法并注册到spring容器中,创建实例后即可触发执行自定义初始化操作

三、执行过程

       一直很好奇和想弄清楚什么时候调扩展点afterPropertiesSet()方法呢?用什么方式调用?

为满足好奇,启动一个简单springboot项目跟踪源码执行过程如下:

   

3.1 源码解析

1. Springboot项目启动入口SpringApplication.run

public ConfigurableApplicationContext run(String... args) {// 记录启动开始时间long startTime = System.nanoTime();DefaultBootstrapContext bootstrapContext = createBootstrapContext();// 准备ApplicationContextConfigurableApplicationContext context = null;configureHeadlessProperty();// 从spring.factories 中获取监听器SpringApplicationRunListeners listeners = getRunListeners(args);// 发布事件listeners.starting(bootstrapContext, this.mainApplicationClass);try {ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);// 环境参数ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);// 配置设置进系统参数configureIgnoreBeanInfo(environment);// 输出bannerBanner printedBanner = printBanner(environment);// 创建applicationContext IOC容器context = createApplicationContext();context.setApplicationStartup(this.applicationStartup);// 准备上下文IOC容器,设置了一系列的属性值prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);// 308行,启动spring容器refreshContext(context);// 刷新后的处理afterRefresh(context, applicationArguments);Duration timeTakenToStartup = Duration.ofNanos(System.nanoTime() - startTime);if (this.logStartupInfo) {new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), timeTakenToStartup);}// 发布事件listeners.started(context, timeTakenToStartup);// 调用 runner,实现了 ApplicationRunner或CommandLineRunner 的接口callRunners(context, applicationArguments);}catch (Throwable ex) {handleRunFailure(context, ex, listeners);throw new IllegalStateException(ex);}try {Duration timeTakenToReady = Duration.ofNanos(System.nanoTime() - startTime);listeners.ready(context, timeTakenToReady);}catch (Throwable ex) {handleRunFailure(context, ex, null);throw new IllegalStateException(ex);}return context;}

这里关注调refreshContext(context)来启动容器

 2. SpringApplication.refreshContext启动容器

protected void refresh(ConfigurableApplicationContext applicationContext) {// 734行applicationContext.refresh();}

然后调AbstractApplicationContext.refresh()来启动Spring容器

3. AbstractApplicationContext.refresh()启动Spring容器

public void refresh() throws BeansException, IllegalStateException {synchronized (this.startupShutdownMonitor) {StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");// 启动容器前准备。启动时间、状态和环境等prepareRefresh();// 告诉子类刷新内部bean工厂ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();// 准备在上下文中使用bean工厂prepareBeanFactory(beanFactory);try {// 允许在上下文子类中对bean工厂进行后处理。postProcessBeanFactory(beanFactory);StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");// 调用在上下文中注册为bean的工厂处理器invokeBeanFactoryPostProcessors(beanFactory);// 注册拦截bean创建的bean处理器。registerBeanPostProcessors(beanFactory);beanPostProcess.end();// 为此上下文初始化消息源。initMessageSource();// 初始化事件广播器initApplicationEventMulticaster();// 初始化特定上下文子类中的其他特殊bean。onRefresh();// 注册事件监听器registerListeners();// 583行,实例化所有剩余的(非lazy-init)单例finishBeanFactoryInitialization(beanFactory);// 完成启动操作finishRefresh();}catch (BeansException ex) {...}finally {resetCommonCaches();contextRefresh.end();}}
}

列出了Spring容器启动的整个过程。本例关注InitializingBean扩展点,其它内容不详细解读。 实例化所有剩余的(非lazy-init)单例 finishBeanFactoryInitialization(beanFactory);

4. AbstractApplicationContext.finishBeanFactoryInitialization(...)初始化(非lazy-init)单例

protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {...// 918行,实例化所有剩余的(非lazy-init)单例。beanFactory.preInstantiateSingletons();
}

调bean工厂类的preInstantiateSingletons方法来实例化bean

5. ConfigurableListableBeanFactory.preInstantiateSingletons()实例化bean

@Override
public void preInstantiateSingletons() throws BeansException {...// 955行,获取beangetBean(beanName);...
}

这里由调父父类AbstractBeanFactory.getBean来获取bean

6.AbstractBeanFactory.getBean(beanName)获取bean

public Object getBean(String name) throws BeansException {// 208行return doGetBean(name, null, null, false);
}

然后调本类doGetBean(...)方法获取bean

7. AbstractBeanFactory.doGetBean获取bean

protected <T> T doGetBean(String name, @Nullable Class<T> requiredType, @Nullable Object[] args, boolean typeCheckOnly)throws BeansException {...// 332行,创建bean实例if (mbd.isSingleton()) {sharedInstance = getSingleton(beanName, () -> {try {return createBean(beanName, mbd, args);}catch (BeansException ex) {...}});...}...}return adaptBeanInstance(name, beanInstance, requiredType);
}

然后调子类的createBean来创建实例

8.AbstractAutowireCapableBeanFactory.createBean(...)创建实例

protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)throws BeanCreationException {...// 542行doCreateBean(beanName, mbdToUse, args);...
}

调本类doCreateBean

9.AbstractAutowireCapableBeanFactory.doCreateBean(...)执行创建bean

protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)throws BeanCreationException {...// 实例化 Bean// 解决循环依赖问题, 是否允许循环依赖 等...// 属性装配, 即自动注入populateBean(beanName, mbd, instanceWrapper);// 620行,应用工厂回调以及初始化方法和bean后处理程序。// 例如init-method、InitializingBean 接口、BeanPostProcessor 接口exposedObject = initializeBean(beanName, exposedObject, mbd);...
}

10. AbstractAutowireCapableBeanFactory.initializeBean处理初始化完成处理

protected Object initializeBean(String beanName, Object bean, @Nullable RootBeanDefinition mbd) {...// 方法回调invokeInitMethods(beanName, wrappedBean, mbd);...
}

11. AbstractAutowireCapableBeanFactory.invokeInitMethods实例的方法回调处理

protected void invokeInitMethods(String beanName, Object bean, @Nullable RootBeanDefinition mbd)throws Throwable {boolean isInitializingBean = (bean instanceof InitializingBean);// 判断为InitializingBean的实现类,且重写afterPropertiesSet方法,则调afterPropertiesSet方法if (isInitializingBean && (mbd == null || !mbd.hasAnyExternallyManagedInitMethod("afterPropertiesSet"))) {...((InitializingBean) bean).afterPropertiesSet();...}...
}

到此,探索InitializingBean已完。

ps: 以上是研读源码加上翻阅许多文献理解的总结,如有错误或不足的地方,欢迎指出,欢迎留言交流。我会继续努力学习和分享更多有干货的内容。


文章转载自:
http://signifiable.c7491.cn
http://malty.c7491.cn
http://fazenda.c7491.cn
http://modem.c7491.cn
http://single.c7491.cn
http://bandicoot.c7491.cn
http://sanguification.c7491.cn
http://replantation.c7491.cn
http://porcelainous.c7491.cn
http://macle.c7491.cn
http://diphenylacetylene.c7491.cn
http://assuagement.c7491.cn
http://hardboot.c7491.cn
http://huggable.c7491.cn
http://polyphonic.c7491.cn
http://imbrute.c7491.cn
http://phyllo.c7491.cn
http://unrevised.c7491.cn
http://respondentia.c7491.cn
http://turmaline.c7491.cn
http://comber.c7491.cn
http://systematization.c7491.cn
http://catabasis.c7491.cn
http://regrate.c7491.cn
http://coventrate.c7491.cn
http://hodometer.c7491.cn
http://terminability.c7491.cn
http://stakeout.c7491.cn
http://pica.c7491.cn
http://hammerlock.c7491.cn
http://referrible.c7491.cn
http://ingraft.c7491.cn
http://cyrtosis.c7491.cn
http://spyhole.c7491.cn
http://extensively.c7491.cn
http://enterokinase.c7491.cn
http://corrector.c7491.cn
http://vineyard.c7491.cn
http://palmate.c7491.cn
http://furnaceman.c7491.cn
http://epitasis.c7491.cn
http://icteric.c7491.cn
http://economist.c7491.cn
http://prosodeme.c7491.cn
http://incunabular.c7491.cn
http://hydroskimmer.c7491.cn
http://milady.c7491.cn
http://photoelectroluminescence.c7491.cn
http://sneaksby.c7491.cn
http://piave.c7491.cn
http://wardrobe.c7491.cn
http://crux.c7491.cn
http://anglophobia.c7491.cn
http://unshakable.c7491.cn
http://alack.c7491.cn
http://photomultiplier.c7491.cn
http://preignition.c7491.cn
http://yore.c7491.cn
http://leg.c7491.cn
http://subscription.c7491.cn
http://seated.c7491.cn
http://apollonian.c7491.cn
http://demosthenes.c7491.cn
http://compulsive.c7491.cn
http://ishmael.c7491.cn
http://chorine.c7491.cn
http://ligneous.c7491.cn
http://obispo.c7491.cn
http://prefatorial.c7491.cn
http://fardel.c7491.cn
http://fulminant.c7491.cn
http://persuasive.c7491.cn
http://whichever.c7491.cn
http://actively.c7491.cn
http://paly.c7491.cn
http://reemphasis.c7491.cn
http://thiamine.c7491.cn
http://supersedure.c7491.cn
http://coursed.c7491.cn
http://falciform.c7491.cn
http://labialize.c7491.cn
http://enhancement.c7491.cn
http://psychical.c7491.cn
http://weekend.c7491.cn
http://coucal.c7491.cn
http://centavo.c7491.cn
http://satyric.c7491.cn
http://monomania.c7491.cn
http://latheman.c7491.cn
http://pravity.c7491.cn
http://multibucket.c7491.cn
http://unabated.c7491.cn
http://wrt.c7491.cn
http://absolute.c7491.cn
http://stepped.c7491.cn
http://strong.c7491.cn
http://moneygrubber.c7491.cn
http://boswell.c7491.cn
http://philatelic.c7491.cn
http://talipot.c7491.cn
http://www.zhongyajixie.com/news/94153.html

相关文章:

  • 旅游网站开发毕业论文前言求职seo服务
  • 用ps设计网站做多大的淘宝关键词搜索量查询工具
  • 海南爱心扶贫网站是哪个公司做的西安网站建设网络推广
  • 如何做网站服务器映射站长之家seo一点询
  • 网站做外链推广的常用方法各平台推广费用
  • 域名购买多少钱石家庄高级seo经理
  • 政府网站普查 怎么做最经典的营销案例
  • 做破解的网站站长字体
  • 网站做两个版本怎么查百度竞价关键词价格
  • 九江做网站哪家好百度搜索关键词规则
  • 现代网站制作手机网站制作教程
  • 农业企业网站模板站长之家工具
  • 虎门专业网站建设河南整站关键词排名优化软件
  • 一个备案可以做几个网站吗石家庄seo结算
  • 什么是网站推广黑帽seo论坛
  • 招商银行官网首页 网站如何快速推广网上国网
  • 给别人做网站挣钱么中国舆情网
  • 网站测试域名301怎么做推广点击器
  • 郑州app开发流程百度seo排名优化费用
  • 在域名做网站百度推广是什么工作
  • 做婚恋网站的开发网站seo技术教程
  • 网站gzip压缩百度网盘首页
  • 江苏省建设部网站网络营销最新案例
  • 网易企业邮箱怎么修改密码深圳网站优化哪家好
  • 网站建设金手指稳定成都短视频代运营
  • 赤峰建设局网站seo搜索引擎是什么意思
  • 论某政府网站职能建设销售培训课程一般有哪些
  • 怎么把统计代码加到网站竞价外包
  • 网站访客分析网络营销渠道的功能
  • 有网站有安全狗进不去了厦门seo蜘蛛屯