先看看Spring MVC启动时加载
SpringMVC的应用中,要实现类似的功能,主要是通过实现下面这些接口(任选一,至少一个即可)
一.ApplicationContextAware接口
- package org.springframework.context;
- import org.springframework.beans.BeansException;
- import org.springframework.beans.factory.Aware;
- import org.springframework.context.ApplicationContext;
- public interface ApplicationContextAware extends Aware {
- void setApplicationContext(ApplicationContext var1) throws BeansException;
- }
二.ServletContextAware 接口
- package org.springframework.web.context;
- import javax.servlet.ServletContext;
- import org.springframework.beans.factory.Aware;
- public interface ServletContextAware extends Aware {
- void setServletContext(ServletContext var1);
- }
三.InitializingBean接口
- package org.springframework.beans.factory;
- public interface InitializingBean {
- void afterPropertiesSet() throws Exception;
- }
四.ApplicationListener<ApplicationEvent> 接口
- package org.springframework.context;
- import java.util.EventListener;
- import org.springframework.context.ApplicationEvent;
- public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
- void onApplicationEvent(E var1);
- }
看看他们的启动顺序:
- package com.bijian.study.controller;
- import javax.servlet.ServletContext;
- import org.apache.logging.log4j.LogManager;
- import org.apache.logging.log4j.Logger;
- import org.springframework.beans.BeansException;
- import org.springframework.beans.factory.InitializingBean;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.ApplicationContextAware;
- import org.springframework.context.ApplicationListener;
- import org.springframework.context.event.ContextRefreshedEvent;
- import org.springframework.stereotype.Component;
- import org.springframework.web.context.ServletContextAware;
- @Component
- public class StartupListener implements ApplicationContextAware, ServletContextAware,
- InitializingBean, ApplicationListener<ContextRefreshedEvent> {
- private Logger logger = LogManager.getLogger();
- @Override
- public void setApplicationContext(ApplicationContext ctx) throws BeansException {
- logger.info("1 => StartupListener.setApplicationContext");
- }
- @Override
- public void setServletContext(ServletContext context) {
- logger.info("2 => StartupListener.setServletContext");
- }
- @Override
- public void afterPropertiesSet() throws Exception {
- logger.info("3 => StartupListener.afterPropertiesSet");
- }
- @Override
- public void onApplicationEvent(ContextRefreshedEvent evt) {
- logger.info("4.1 => MyApplicationListener.onApplicationEvent");
- if (evt.getApplicationContext().getParent() == null) {
- logger.info("4.2 => MyApplicationListener.onApplicationEvent");
- }
- }
- }
服务启动时,输出的顺序如下:
- 22:54:44.433 [localhost-startStop-1] INFO com.bijian.study.controller.StartupListener - 1 => StartupListener.setApplicationContext
- 22:54:44.435 [localhost-startStop-1] INFO com.bijian.study.controller.StartupListener - 2 => StartupListener.setServletContext
- 22:54:44.435 [localhost-startStop-1] INFO com.bijian.study.controller.StartupListener - 3 => StartupListener.afterPropertiesSet
- 22:54:44.931 [localhost-startStop-1] INFO com.bijian.study.controller.StartupListener - 4.1 => MyApplicationListener.onApplicationEvent
- 22:54:44.931 [localhost-startStop-1] INFO com.bijian.study.controller.StartupListener - 4.2 => MyApplicationListener.onApplicationEvent
注意:onApplicationEvent方法会触发多次,初始化这种事情,越早越好,建议在setApplicationContext方法中处理,完整示例见附件。
此外还有一种方法:写个BEAN,将要执行的操作写在构造函数里,再将这个类配置到XML里,比如配置到SPRING-MVC.XML里 <bean class="com.test.xx"> 自己的类。不过不推荐使用这种方法。
再看看Spring-boot 启动时执行加载资源
实现CommandLineRunner接口(可以设置启动顺序)
@Order(value=2)
@Component
public class CommandLineRunnerListenerImpl implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
// 执行操作
}
}
- 可以通过指定
@Order
的值来控制启动的顺序,值越小表示越先执行
@Component
public class CommandLineRunnerListenerImpl implements ApplicationListener {
@Override
public void onApplicationEvent(ApplicationEvent event) {
// 执行操作
}
}