当前位置: 首页 > 后端技术 > Java

SpringBoot初始化资源的方式你知道多少?

时间:2023-04-01 23:14:20 Java

送你以下java学习资料。有一种方法可以在文章末尾获取它们。假设有这样一个需求,需要在项目启动过程中完成线程池初始化、加密证书加载等功能。你会怎么做?如果您不知道答案,请继续阅读。今天就给大家介绍一下SpringBoot中初始化资源的几种方式,帮助大家解决和回答这个问题。CommandLineRunner定义了初始化类MyCommandLineRunner实现了CommandLineRunner接口,并实现了其run()方法,在该方法中编写初始化逻辑并注册为Bean,并添加@Component注解。示例代码如下:packagecn.zh.controller;importorg.springframework.boot.CommandLineRunner;importorg.springframework.core.annotation.Order;importorg.springframework.stereotype.Component;@ComponentpublicclassMyCommandLineRunnerimplementsCommandLineRunner{@Overridepublicvoidrun(String.argsout)SystemExceptionthrows.println("项目初始化----------------11");}}实现了CommandLineRunner接口的Component会在所有的SpringBeans初始化之后,SpringApplication.run()执行完成之前执行。让我们通过添加两行打印来验证我们的测试。packagecn.zh;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublicclassProcApplication{publicstaticvoidmain(String[]args){System.out.println("...startSpringApplication.run");SpringApplication.run(ProcApplication.class,args);System.out.println("...endSpringApplication.run");}}ApplicationRunner定义了初始化类MyApplicationRunner实现了ApplicationRunner接口并实现了其run()方法,其中编写了初始化逻辑并注册为Bean,可以添加@Component注解。示例代码如下:packagecn.zh.controller;importorg.springframework.boot.ApplicationArguments;importorg.springframework.boot.ApplicationRunner;importorg.springframework.core.annotation.Order;importorg.springframework.stereotype。Component;importjavax.annotation.PostConstruct;@ComponentpublicclassMyApplicationRunnerimplementsApplicationRunner{@Overridepublicvoidrun(ApplicationArgumentsargs)throwsException{System.out.println("项目初始化二--------");可以看出,可以通过实现ApplicationRunner接口和CommandLineRunner接口来完成项目的初始化,达到同样的效果,两者唯一的区别是run()方法中的形参不同。在CommandLineRunner中,它只是一个简单的String...args形式参数,而ApplicationRunner中包含了ApplicationArguments对象,可以帮助获取更丰富的项目信息。@Order如果项目中既有实现ApplicationRunner接口的初始化类,又有实现CommandLineRunner接口的初始化类,先执行哪个?测试告诉我们,答案是先执行实现了ApplicationRunner接口的初始化类。我认为您不需要过多关注原因。但是如果你需要改变两个初始化类之间的默认执行顺序,那么使用@Order注解可以帮助我们解决这个问题。@Component@Order(1)publicclassMyCommandLineRunner实现CommandLineRunner{/***用于运行bean的回调。**@paramargs传入的main方法参数*@throws出错时发生异常*/@Overridepublicvoidrun(String...args)throwsException{System.out.println("项目初始化--------------11");}}@Component@Order(2)publicclassMyApplicationRunnerimplementsApplicationRunner{@Overridepublicvoidrun(ApplicationArgumentsargs)throwsException{System.out.println("项目初始化二--------");}@PostConstructpublicvoidinit(){System.out.println("@PostConstruct初始化");@PostConstruct使用@PostConstruct注解也可以帮助我们完成资源的初始化,前提是这些初始化操作不需要依赖其他Springbean的初始化。可以看到方法上使用了@PostConstruct注解,写一个方法测试一下。@PostConstructpublicvoidinit(){System.out.println("@PostConstruct初始化");注意:只有一个非静态方法可以使用这个注解被注解的方法不能有任何参数被注解的方法必须返回void被注解的方法不能抛出检查异常。该方法只会执行一次。综上所述,使用@PostConstruct注解的初始化操作顺序是最快的,前提是这些操作不能依赖于其他bean的初始化完成。通过添加@Order注解,我们可以改变同一层级之间不同bean的加载顺序。InitializingBeanInitializingBean是Spring提供的接口,只包含一个afterPropertiesSet()方法。对于任何一个实现了这个接口的类,当它对应的Bean被Spring管理,并且设置了所有必要的属性后,Spring就会调用这个Bean的afterPropertiesSet()。@ComponentpublicclassMyListener1implementsInitializingBean{@AutowiredprivateShopInfoMappershopInfoMapper;@OverridepublicvoidafterPropertiesSet(){//使用spring容器中的bean//System.out.println(shopInfoMapper.selectById("1").getShopName());System.out.println("项目启动成功");}}ApplicationListenerApplicationListener是spring的监听器,可以用来监听事件,典型的观察者模式。如果容器中有一个ApplicationListenerBean,那么每当ApplicationContext发布一个ApplicationEvent时,这个ApplicationListenerBean就会被自动触发。这种事件机制必须由程序显示来触发。其中,spring内置了一些事件,当某些操作完成时会发出一定的事件动作。例如,侦听ContextRefreshedEvent事件。当所有bean都初始化加载成功后,就会触发该事件。实现ApplicationListener接口接收监听动作,然后自己写逻辑。同样,事件可以自定义,监听器也可以自定义,完全按照自己的业务逻辑来处理。所以资源的初始加载也可以完成。@ComponentpublicclassMyListener1implementsApplicationListener{@OverridepublicvoidonApplicationEvent(ApplicationEventapplicationEvent){//打印出每次事件的名称System.out.println(applicationEvent.toString());if(applicationEventinstanceofApplicationReadyEvent){System.out。println("项目启动成功");}}}