启动生命周期
一.Spring boot初始化器扩展
package com.lx.conmon.extend.intializer;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
/**
* Spring boot初始化器扩展
* @author liu wei ping
* @version 1.0
* @date 2023-07-02 10:10:04
*/
public class PeopleInitializer implements ApplicationContextInitializer {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
System.out.println("PeopleInitializer#initialize....");
}
}
二.Spring boot监听器扩展 容器启动中
package com.lx.conmon.extend.listener;
import org.springframework.boot.context.event.ApplicationStartingEvent;
import org.springframework.context.ApplicationListener;
/**
* Spring boot监听器扩展 容器启动中
* @author liu wei ping
* @version 1.0
* @date 2023-07-02 10:15:03
*/
public class PeopleStartingListener implements ApplicationListener<ApplicationStartingEvent> {
@Override
public void onApplicationEvent(ApplicationStartingEvent event) {
System.out.println("PeopleStartingListener#PeopleStartingListener....");
}
}
三.Spring boot监听器扩展 容器启动完成
package com.lx.conmon.extend.listener;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
/**
* Spring boot监听器扩展 容器启动完成
* @author liu wei ping
* @version 1.0
* @date 2023-07-02 10:17:10
*/
public class PeopleStartedListener implements ApplicationListener<ApplicationStartedEvent> {
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
System.out.println("PeopleStartedListener#onApplicationEvent....");
}
}
四.执行BeanFactory的后置处理器
package com.lx.conmon.extend.beanFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;
/**
* 执行BeanFactory的后置处理器
* @author liu wei ping
* @version 1.0
* @date 2023-07-02 10:29:24
*/
@Component
public class PeopleBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
Arrays.asList(beanFactory.getBeanDefinitionNames()).forEach(beanDefinitionName -> System.out.println(beanDefinitionName));
System.out.println("PeopleBeanFactoryPostProcessor...");
}
}
五.向BeanFactory中注册bean的postprocessor,用于后续bean创建的拦截操作
package com.lx.conmon.extend.beanPostProcessor;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
/**
* 向BeanFactory中注册bean的postprocessor,用于后续bean创建的拦截操作
* @author liu wei ping
* @version 1.0
* @date 2023-07-02 10:40:20
*/
@Component
public class PeopleBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (beanName.equals("userApiServiceImpl")) {
System.out.println("找到了userApiServiceImpl: " + bean);
}
return null;
}
}
六.容器启动完成的回调
package com.lx.conmon.extend.runner;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
/**
* 容器启动完成的回调
* @author liu wei ping
* @version 1.0
* @date 2023-07-02 10:26:31
*/
@Component
public class PeopleApplicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("PeopleApplicationRunner#run.....");
}
}
七. 上面初始化器和监听器一.二.三基于spi机制扩展
1.在resources/META-INF创建spring.factories文件
org.springframework.context.ApplicationContextInitializer=com.lx.conmon.extend.intializer.PeopleInitializer
org.springframework.context.ApplicationListener=com.lx.conmon.extend.listener.PeopleStartedListener, com.lx.conmon.extend.listener.PeopleStartingListener
2.如图: