在咱們用 springboot 搭建項目的時候,有時候會碰到在項目啓動時初始化一些操做的需求 ,針對這種需求 spring boot爲咱們提供瞭如下幾種方案供咱們選擇:html
ApplicationRunner
與 CommandLineRunner
接口java
Spring容器初始化時InitializingBean接口和@PostConstruct
web
Spring的事件機制spring
咱們能夠實現 ApplicationRunner
或 CommandLineRunner
接口, 這兩個接口工做方式相同,都只提供單一的run方法,該方法在SpringApplication.run(…)完成以前調用,不知道你們還對我上一篇文章結尾有沒有印象,咱們先來看看這兩個接口springboot
public interface ApplicationRunner { void run(ApplicationArguments var1) throws Exception; } public interface CommandLineRunner { void run(String... var1) throws Exception; }
都只提供單一的run方法,接下來咱們來看看具體的使用app
構造一個類實現ApplicationRunner接口框架
//須要加入到Spring容器中 @Component public class ApplicationRunnerTest implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { System.out.println("ApplicationRunner"); } }
很簡單,首先要使用@Component將實現類加入到Spring容器中,爲何要這樣作咱們待會再看,而後實現其run方法實現本身的初始化數據邏輯就能夠了ide
對於這兩個接口而言,咱們能夠經過Order註解或者使用Ordered接口來指定調用順序, @Order()
中的值越小,優先級越高源碼分析
//須要加入到Spring容器中 @Component @Order(1) public class CommandLineRunnerTest implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("CommandLineRunner..."); } }
一樣須要加入到Spring容器中,CommandLineRunner的參數是最原始的參數,沒有進行任何處理,ApplicationRunner的參數是ApplicationArguments,是對原始參數的進一步封裝post
你們回顧一下我上一篇文章,也就是SpringApplication.run方法的最後一步第八步:執行Runners,這裏我直接把代碼複製過來
private void callRunners(ApplicationContext context, ApplicationArguments args) { List<Object> runners = new ArrayList<Object>(); //獲取容器中全部的ApplicationRunner的Bean實例 runners.addAll(context.getBeansOfType(ApplicationRunner.class).values()); //獲取容器中全部的CommandLineRunner的Bean實例 runners.addAll(context.getBeansOfType(CommandLineRunner.class).values()); AnnotationAwareOrderComparator.sort(runners); for (Object runner : new LinkedHashSet<Object>(runners)) { if (runner instanceof ApplicationRunner) { //執行ApplicationRunner的run方法 callRunner((ApplicationRunner) runner, args); } if (runner instanceof CommandLineRunner) { //執行CommandLineRunner的run方法 callRunner((CommandLineRunner) runner, args); } } }
很明顯,是直接從Spring容器中獲取ApplicationRunner和CommandLineRunner的實例,並調用其run方法,這也就是爲何我要使用@Component將ApplicationRunner和CommandLineRunner接口的實現類加入到Spring容器中了。
在spring初始化bean的時候,若是bean實現了 InitializingBean
接口,在對象的全部屬性被初始化後以後纔會調用afterPropertiesSet()方法
@Component public class InitialingzingBeanTest implements InitializingBean { @Override public void afterPropertiesSet() throws Exception { System.out.println("InitializingBean.."); } }
咱們能夠看出spring初始化bean確定會在 ApplicationRunner和CommandLineRunner接口調用以前。
@Component public class PostConstructTest { @PostConstruct public void postConstruct() { System.out.println("init..."); } }
咱們能夠看到,只用在方法上添加@PostConstruct註解,並將類注入到Spring容器中就能夠了。咱們來看看@PostConstruct註解的方法是什麼時候執行的
在Spring初始化bean時,對bean的實例賦值時,populateBean方法下面有一個initializeBean(beanName, exposedObject, mbd)方法,這個就是用來執行用戶設定的初始化操做。咱們看下方法體:
protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) { if (System.getSecurityManager() != null) { AccessController.doPrivileged((PrivilegedAction<Object>) () -> { // 激活 Aware 方法 invokeAwareMethods(beanName, bean); return null; }, getAccessControlContext()); } else { // 對特殊的 bean 處理:Aware、BeanClassLoaderAware、BeanFactoryAware invokeAwareMethods(beanName, bean); } Object wrappedBean = bean; if (mbd == null || !mbd.isSynthetic()) { // 後處理器 wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName); } try { // 激活用戶自定義的 init 方法 invokeInitMethods(beanName, wrappedBean, mbd); } catch (Throwable ex) { throw new BeanCreationException( (mbd != null ? mbd.getResourceDescription() : null), beanName, "Invocation of init method failed", ex); } if (mbd == null || !mbd.isSynthetic()) { // 後處理器 wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName); } return wrappedBean; }
咱們看到會先執行後處理器而後執行invokeInitMethods方法,咱們來看下applyBeanPostProcessorsBeforeInitialization
public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) throws BeansException { Object result = existingBean; for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) { result = beanProcessor.postProcessBeforeInitialization(result, beanName); if (result == null) { return result; } } return result; } public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) throws BeansException { Object result = existingBean; for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) { result = beanProcessor.postProcessAfterInitialization(result, beanName); if (result == null) { return result; } } return result; }
獲取容器中全部的後置處理器,循環調用後置處理器的postProcessBeforeInitialization方法,這裏咱們來看一個BeanPostProcessor
public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBeanPostProcessor implements InstantiationAwareBeanPostProcessor, BeanFactoryAware, Serializable { public CommonAnnotationBeanPostProcessor() { this.setOrder(2147483644); //設置初始化參數爲PostConstruct.class this.setInitAnnotationType(PostConstruct.class); this.setDestroyAnnotationType(PreDestroy.class); this.ignoreResourceType("javax.xml.ws.WebServiceContext"); } //略... }
在構造器中設置了一個屬性爲PostConstruct.class,再次觀察CommonAnnotationBeanPostProcessor這個類,它繼承自InitDestroyAnnotationBeanPostProcessor。InitDestroyAnnotationBeanPostProcessor顧名思義,就是在Bean初始化和銷燬的時候所做的一個前置/後置處理器。查看InitDestroyAnnotationBeanPostProcessor類下的postProcessBeforeInitialization方法:
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { LifecycleMetadata metadata = findLifecycleMetadata(bean.getClass()); try { metadata.invokeInitMethods(bean, beanName); } catch (InvocationTargetException ex) { throw new BeanCreationException(beanName, "Invocation of init method failed", ex.getTargetException()); } catch (Throwable ex) { throw new BeanCreationException(beanName, "Couldn't invoke init method", ex); } return bean; } private LifecycleMetadata buildLifecycleMetadata(final Class clazz) { final LifecycleMetadata newMetadata = new LifecycleMetadata(); final boolean debug = logger.isDebugEnabled(); ReflectionUtils.doWithMethods(clazz, new ReflectionUtils.MethodCallback() { public void doWith(Method method) { if (initAnnotationType != null) { //判斷clazz中的methon是否有initAnnotationType註解,也就是PostConstruct.class註解 if (method.getAnnotation(initAnnotationType) != null) { //若是有就將方法添加進LifecycleMetadata中 newMetadata.addInitMethod(method); if (debug) { logger.debug("Found init method on class [" + clazz.getName() + "]: " + method); } } } if (destroyAnnotationType != null) { //判斷clazz中的methon是否有destroyAnnotationType註解 if (method.getAnnotation(destroyAnnotationType) != null) { newMetadata.addDestroyMethod(method); if (debug) { logger.debug("Found destroy method on class [" + clazz.getName() + "]: " + method); } } } } }); return newMetadata; }
在這裏會去判斷某方法是否有PostConstruct.class註解,若是有,則添加到init/destroy隊列中,後續一一執行。@PostConstruct註解的方法會在此時執行,咱們接着來看invokeInitMethods
protected void invokeInitMethods(String beanName, final Object bean, @Nullable RootBeanDefinition mbd) throws Throwable { // 是否實現 InitializingBean // 若是實現了 InitializingBean 接口,則只掉調用bean的 afterPropertiesSet() boolean isInitializingBean = (bean instanceof InitializingBean); if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) { if (logger.isDebugEnabled()) { logger.debug("Invoking afterPropertiesSet() on bean with name '" + beanName + "'"); } if (System.getSecurityManager() != null) { try { AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> { ((InitializingBean) bean).afterPropertiesSet(); return null; }, getAccessControlContext()); } catch (PrivilegedActionException pae) { throw pae.getException(); } } else { // 直接調用 afterPropertiesSet() ((InitializingBean) bean).afterPropertiesSet(); } } if (mbd != null && bean.getClass() != NullBean.class) { // 判斷是否指定了 init-method(), // 若是指定了 init-method(),則再調用制定的init-method String initMethodName = mbd.getInitMethodName(); if (StringUtils.hasLength(initMethodName) && !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) && !mbd.isExternallyManagedInitMethod(initMethodName)) { // 利用反射機制執行 invokeCustomInitMethod(beanName, bean, mbd); } } }
首先檢測當前 bean 是否實現了 InitializingBean 接口,若是實現了則調用其 afterPropertiesSet()
,而後再檢查是否也指定了 init-method()
,若是指定了則經過反射機制調用指定的 init-method()
。
咱們也能夠發現@PostConstruct會在實現 InitializingBean 接口的afterPropertiesSet()方法以前執行
Spring的事件驅動模型由三部分組成
ApplicationEvent
,繼承自JDK的 EventObject
,全部事件都要繼承它,也就是被觀察者ApplicationEventPublisher
及 ApplicationEventMulticaster
接口,使用這個接口,就能夠發佈事件了ApplicationListener
,繼承JDK的 EventListener
,全部監聽者都繼承它,也就是咱們所說的觀察者,固然咱們也可使用註解 @EventListener
,效果是同樣的在Spring框架中,默認對ApplicationEvent事件提供了以下支持:
構造一個類繼承ApplicationEvent
public class TestEvent extends ApplicationEvent { private String message; public TestEvent(Object source) { super(source); } public void getMessage() { System.out.println(message); } public void setMessage(String message) { this.message = message; } }
有兩種方法能夠建立監聽者,一種是直接實現ApplicationListener的接口,一種是使用註解 @EventListener
, 註解是添加在監聽方法上的 ,下面的例子是直接實現的接口
@Component public class ApplicationListenerTest implements ApplicationListener<TestEvent> { @Override public void onApplicationEvent(TestEvent testEvent) { testEvent.getMessage(); } }
對於事件發佈,表明者是 ApplicationEventPublisher
和 ApplicationEventMulticaster
,ApplicationContext接口繼承了ApplicationEventPublisher,並在AbstractApplicationContext實現了具體代碼,實際執行是委託給ApplicationEventMulticaster(能夠認爲是多播)
下面是一個事件發佈者的測試實例:
@RunWith(SpringRunner.class) @SpringBootTest public class EventTest { @Autowired private ApplicationContext applicationContext; @Test public void publishTest() { TestEvent testEvent = new TestEvent(""); testEvent.setMessage("hello world"); applicationContext.publishEvent(testEvent); } }
利用 ContextRefreshedEvent
事件進行初始化,該事件是 ApplicationContext
初始化完成後調用的事件,因此咱們能夠利用這個事件,對應實現一個 監聽器 ,在其 onApplicationEvent()
方法裏初始化操做
@Component public class ApplicationListenerTest implements ApplicationListener<ContextRefreshedEvent> { @Override public void onApplicationEvent(ContextRefreshedEvent event) { System.out.println("容器刷新完成後,我被調用了.."); } }