mui的实例项目源码_从源码分析Spring项目启动后加载数据的几种方式

Spring项目启动后加载数据的几种方式

做项目过程中,有时候难免会遇到一些需求需要项目启动完毕时初始化一些数据或执行一些特定的操作。

Spring启动后加载数据的几种方式:

项目基于SpringBoot开发的

1. org.springframework.boot.CommandLineRuner

CommandLineRuner的定义为一个接口。

033ae8718fcc913c6471e59fac42a9d6.png

CommandLineRuner接口

在项目中只需定义一个类,实现CommandLineRuner接口,然后在覆盖的run方法里进行自己的业务处理就可以了。

@Componentpublic class MyCommandLineRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { // do something here }}

2. org.springframework.boot.ApplicationRunner

ApplicationRunner与CommandLineRunner 一样,也是一个接口类型

dbcf7734f9982a640d95544287ca92ba.png

CommandLineRunner 接口

项目中使用也是定义一个类实现ApplicationRunner,然后覆盖run方法进行自己的业务处理。

@Componentpublic class MyApplicationRunner implements ApplicationRunner { @Override public void run(ApplicationArguments applicationArguments) throws Exception { // do something here }}

ApplicationRunner与CommandLineRunner都是覆盖接口的run方法,在run方法里进行相应的业务处理。不同之处是CommandLineRunner的run方法参数为启动类main方法传进来原封不动的参数args,ApplicationRunner的run方法是对args 参数进行了多一层的封装ApplicationArguments args,可以获取更多的项目相关信息。

public interface ApplicationArguments { // 返回传递给应用程序的原始未处理参数。 String[] getSourceArgs(); // 返回所有选项参数的名称。 Set getOptionNames(); // 返回从参数中解析的选项参数集是否包含具有给定名称的选项。 boolean containsOption(String name); // 返回与具有给定名称的arguments选项关联的值的集合。 List getOptionValues(String name); // 返回已解析的非选项参数的集合。 List getNonOptionArgs();}

ApplicationRunner与CommandLineRunner都是在容器加载完毕后执行

源码分析:SpringApplicaton.run(String... args)方法

0e3ee096411d3640f5b4e4c534acaf6a.png

callRunners(context,applicationArguments);方法

64995a2a461f3c3a82fc31edb28e7eea.png

从callRunners方法源码看得出,ApplicationRunner比CommandLineRunner先执行,但一般项目里实现其中一个就行。如果有多个ApplicationRunner或CommandLineRunner的实现类,又需要按顺序执行的话,可以在实现类头上加注解@Order(int),数值越小越早执行。

3. PostConstruct

PostConstruct是JDK提供的一个注解,只需在类成员方法上添加@PostConstruct注解,在执行Spring bean初始时就会执行该成员方法。

注:由于是在容器加载Bean的时候执行,有可能执行该方法时一些其他的Bean还没加载完,所有在注解PostConstruct的成员方法里不能依赖其他的Bean。

项目不是基于SpringBoot的,可以使用以下方法:

1. 实现org.springframework.beans.factory.InitializingBean接口,在afterPropertiesSet方法处理自己的业务。

a23a749bb878a05995009ecc22cc7077.png
@Componentpublic class MyInitializingBean implements InitializingBean { @Override public void afterPropertiesSet() throws Exception { // do something here }}

从afterPropertiesSet的方法名可以看得出来,Spring容器在加载实例的时候设置实体属性后执行该方法。

源码分析:

在上一遍Spring的启动过程文章里有提到过Spring启动时会加载所有的Bean到容器

在解析XML配置文件定义的实例时是通过类DefaultBeanDefinitionDocumentReader的

void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate)方法。

b5f35eb7de3107c93c7c2161c222bc1b.png
3b2b1886353112076195efc9a03bfc27.png

最后实际是通过BeanDefinitionReaderUtils工具类调用到具体的register的registerBeanDefinition。

30022ed53f9b6155dc0be9e110772e27.png

这里的register具体实现类为DefaultLisableBeanFactory。

兜兜转转还是回到了DefaultLisableBeanFactory。

DefaultLisableBeanFactory是Spring容器BeanFactory的核心基础。

DefaultLisableBeanFactory.registerBeanDefinition()方法将实例加入到beanDefinitionNames中。

AbstractApplicationContext类的refresh()方法

// Instantiate all remaining (non-lazy-init) singletons.

finishBeanFactoryInitialization(beanFactory)实例剩下所有的非懒加载的实例。

82197f1bfbc03378f77168fc1b8b7b98.png

在这里调用DefaultListableBeanFactory类的preInstantiateSingletons()方法

48a0fb13a2f13a82ed6b36826b13cbc7.png

根据beanName调用getBean()实例化对象

getBean() -> AbstractBeanFactory.getBean(String naem) ->doGetBean();

最终还是回到AbstractAutowireCapableBeanFactory.createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)方法

在doCreateBean()方法里调用initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd)方法

然后在invokeInitMethods(String beanName, final Object bean, @Nullable RootBeanDefinition mbd)回调实现了InitializeBean接口的afterPropertiesSet方法

05876e8a1d9366528fc0ac848617cf30.png

2. 实现ApplicationListener接口,监听ContextRefreshedEvent事件。

@Componentpublic class MyApplicationListener implements ApplicationListener { @Override public void onApplicationEvent(ContextRefreshedEvent event) { //保证在web容器中只执行一次该事件 if (event.getApplicationContext().getParent() == null) {  // do something here } }}

注意:在Spring mvc web项目中,系统会存在两个容器,一个是root application context,另一个是自己项目的projectName-servlet context,是作为root aplication context的子容器,所以会触发两次ContextRefreshedEvent事件,我们只有 root aplication context 初始化完成后进行相应的业务处理就可以了,即event.getApplicationContext().getParent() == null。

源码分析:

AbstractApplicationContext.refresh()方法,在初始化完所有的非懒加载的实例后调用finishRefresh()方法;

868a82fa6a613fd693df4dbc8bdc90f8.png

在这里会触发一个ContextRefreshedEvent事件。监听器监听到事件就会调用所有实现了ApplicationListener监听ContextRefreshedEvent的方法。

3. 在配置文件里标签里指定init-method方法

主要也是在DefaultBeanDefinitionDocumentReader里将bean标签定义的name解析成BeanDefinition,然后在initializeBean()的时候调用定义的init-method。这里就不细贴源码了。

总结:ApplicationRunner,CommandLineRunner,ApplicationListener是在容器初始化完成后调用,因此可以依懒于其他实例。PostConstruct注解是实例构造函数执行后调用,InitializingBean的afterPropertiesSet是在实例所有属性赋值后执行,两者都在容器初始化完成前执行,故不能依懒其他实例。

执行顺序:PostConstruct --> afterPropertiesSet --> ApplicationListener --> ApplicationRunner --> CommandLineRunner

以上是学习Spring源码的一个总结,由于个人能力有限,存在错误的地方欢迎评论指出,大家一起学习,谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值