1.ApplicationContextInitializer接口(接口ApplicationContextInitializer是在spring容器执行refreshed之前得一个回调)
使用步骤:
1.写一个类实现ApplicationContextInitializer
2.注册ApplicationContextInitializer
三种注册方法:
1. springApplication(new MyApplicationContextInitializer());
2.配置文件里面配置context.initializer.classes=com.zxf.lean11.MyApplicationContextInitializer,多个 , 隔开
3.通过classpath:META-INF/spring.factories配置org.springframework.context.ApplicationContextInitializer=com.zxf.lean11.MyApplicationContextInitializer2
3.CommandLineRunner接口
使用步骤
1.写一个类,实现CommandLineRunner接口
2.把该类纳入spring容器中
注意 : 多个现实类的时候可以通过@Order(1)控制执行顺序,从小到大执行
3.ApplicationContextInitializer和CommandLineRunner接口说明
- ApplicationRunner接口与CommandLineRunner作用一样,@Order也一样作用,两种一起用@Order一样奏效。
- CommandLineRunner、ApplicationRunner接口实在容器启动成功后的最后一步的回调。
- ApplicationRunner接口的方法参数与CommandLineRunner不一样,CommandLineRunner为原始参数;而ApplicationRunner是spring封装过的–ApplicationArguments(是对参数(main方法)做了进一步的封装,
可以解析–name=value的),我们可以通过name获取value。
注意:–name=value这种传参的时候,必须把main方法的参数传给run方法。