一、@Bean 、@Component 、@Configuration 等 表明办法初始化
在日常开拓中,这种办法是我们常常利用的,采取该表明,在系统启动的时候,spring容器会自动扫描,并初始化各个Bean工具,进而干系业务的初始化,详细利用如下:
@Slf4jpublic class DemoBean { // 此处即可完成初始化动作 public void init() { log.info("DemoBean 初始化完成!
") } @Bean(initMethod = "init") public DemoBean demoBean() { return new DemoBean(); }}
上面的办法,可以创造,在利用@Bean表明的时候,需将initMethod指向init方法!
@Component表明(图片来自网络侵删)在Springboot中,我们常用@Component表明,来创建一个单例(Singleton)。即:容器初始化时,创建Bean,容器关闭时,将Bean销毁掉。我们可以利用getBean(className)获取bean工具(该工具为同一个实例!
)
@Slf4j@Componentpublic class DemoBean { public DemoBean() { log.info("DemoBean 初始化完成!
") }}当利用@Component表明后,利用ComponentScan指定类的路径,即可在容器启动的时候,初始化,并调用其布局方法,完成初始化!
这里扩展一下:通过合营@Scope 指定为prototype时(默认为singleton),则每次getBean(className)时,都会新天生一个工具!
@Configuration表明该表明紧张用来初始化读取配置文件中的配置参数,并将参数注入到工具中来。详细利用与@Component类似,这里不再赘述!
@Slf4j@Configurationpublic class DemoBean { public DemoBean() { log.info("DemoBean 初始化完成!
") }}二、@PostConstruct表明初始化
PostConstrust在初始化过程中,常用的一个方法上的表明。但是须要把稳的是,初始化存在先后顺序关系(把稳顺序,防止利用过程中涌现空指针问题)!
布局方法 -> @Autowired -> @PostConstruct ;常常用来在bean初始化之后,实行指定操作,常用来布局方法中的延时操作。
@Slf4j@Configurationpublic class DemoBean { public DemoBean() { log.info("DemoBean 初始化完成!
") } @PostConstruct public init() { log.info("初始化完成后,调用。。。。"); }}三、继续CommandLineRunner 接口完成初始化
在容器初始化完成后,会调用CommandLineRunner中的run()方法。可以利用该方法达到初始化的目的。
其余,这里须要把稳的时候,该接口利用比ApplicationLister更加灵巧。
可以捕获赖在掌握台的输入参数通过@Order指定实行顺序
@Slf4j@Componentpublic class DemoBeanCommandLineRunner implements CommandLineRunner { @Override public run(String... args) throws Exception() { log.info("初始化完成。。。入参: {}。", Arrays.asList(args)); }}
启动时,可以传入参数:
jar -jar demo.jar test 1111 222
四、继续ApplicationRunner接口完成初始化
与上面类似,ApplicationRunner也是Springboot供应的。
但有不同的是,支持入参为键值对传入!
@Slf4j@Componentpublic class DemoBeanCommandLineRunner implements CommandLineRunner { @Override public run(ApplicationArguments args) throws Exception() { log.info("初始化完成。。。入参: {} - {} - {}。", args.getOptionNames("isSkip"),args.getNonOptionArgs(),args.getSourceArgs()); }}
可以这样通报参数:
java -jar demo.jar --isSkip=true aaa bb
实行结果如下:
初始化完成。。。入参:[true]-[aaa,bb]-[--isSkip=true,aaa,bb]
五、继续InitializingBean接口完成初始化
利用与@PostConstruct 类似。 但是须要实现afterPropertiesSet方法。
@Slf4j@Componentpublic class DemoBeanCommandLineRunner implements InitializingBean { @Override public afterPropertiesSet() { log.info("初始化完成。。。"); }}
六、利用ApplicationListener<ContextRefreshedEvent>内置事宜完成初始化
该办法利用了Spring事宜机制,实现初始化。
spring事宜驱动模型紧张分为三个部分:
事宜:ApplicationEvent,其继续自EventObject,所有的事宜类型都继续自它!
发布者:ApplicationEventPublisher、ApplicationEventMulticaster,利用这两个接口,可以发布事宜监听者:ApplicationListener 用于吸收关注的事宜上面ContextRefreshedEvent,为build-in事宜,即spring内置事宜:
ContextStaredEvent: 当利用ApplicationContext中的start()方法,事宜就会被发布;ContextStoppedEvent:当利用ApplicationContext中的stop()方法,事宜就会被发布;吸收到该事宜后,可以做一些必要的清理事情!
ContextClosedEvent:当利用ApplicationContext中的close()方法,事宜就会被发布;ContextRefreshedEvent:当利用ApplicationContext被初始化或刷新时(实行refresh()方法),事宜就会被发布;发布该事宜,意味着所有的Bean都被初始化并实例化完成,ApplicatContext容器已经就绪。详细代码如下:
@Slf4j@Componentpublic class DemoBeanCommandLineRunner implements ApplicationListener<ContextRefreshedEvent> { @Override public onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) { log.info("初始化完成。。。"); }}
考虑篇幅,这里不再过多列举~
干系问题,欢迎留言提问;欢迎大家点赞、关注、收藏~