springboot初始化时执行方法

参考:http://www.360doc.com/content/23/0303/15/81790262_1070284511.shtml

监听容器刷新完成扩展点

ApplicationListener<ContextRefreshedEvent>
ApplicationContext事件机制是观察者设计模式实现的,通过ApplicationEvent和ApplicationListener这两个接口实现ApplicationContext的事件机制。

Spring中一些内置的事件如下:

  • ContextRefreshedEvent:
    ApplicationContext 被初始化或刷新时,该事件被发布。这也可以在 ConfigurableApplicationContext接口中使用 refresh() 方法来发生。此处的初始化是指:所有的Bean被成功装载,后处理Bean被检测并激活,所有Singleton Bean 被预实例化,ApplicationContext容器已就绪可用。
  • ContextStartedEvent:
    当使用 ConfigurableApplicationContext (ApplicationContext子接口)接口中的 start() 方法启动 ApplicationContext 时,该事件被发布。你可以调查你的数据库,或者你可以在接受到这个事件后重启任何停止的应用程序。
  • ContextStoppedEvent:
    当使用 ConfigurableApplicationContext 接口中的 stop() 停止 ApplicationContext 时,发布这个事件。你可以在接受到这个事件后做必要的清理的工作。
  • ContextClosedEvent:
    当使用 ConfigurableApplicationContext 接口中的 close() 方法关闭 ApplicationContext 时,该事件被发布。一个已关闭的上下文到达生命周期末端;它不能被刷新或重启。
  • RequestHandledEvent:
    这是一个 web-specific 事件,告诉所有 bean HTTP 请求已经被服务。只能应用于使用DispatcherServlet的Web应用。在使用Spring作为前端的MVC控制器时,当Spring处理用户请求结束后,系统会自动触发该事件。
    好了,了解上面这些内置事件后,我们可以监听ContextRefreshedEvent

在Spring Boot 启动时完成一些操作,代码如下:

@Component
public class TestApplicationListener implements ApplicationListener<ContextRefreshedEvent>{
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println(contextRefreshedEvent);
        System.out.println("TestApplicationListener............................");
    }
}

可以自定事件完成一些特定的需求,比如:邮件发送成功之后,做一些业务处理。

自定义EmailEvent,代码如下:

public class EmailEvent extends ApplicationEvent{
   private String address;
   private String text;
   public EmailEvent(Object source, String address, String text){
   super(source);
      this.address = address;
      this.text = text;
   }
   public EmailEvent(Object source) {
     super(source);
   }
   //......address和text的setter、getter
}

自定义监听器,代码如下:

public class EmailNotifier implements ApplicationListener{
   public void onApplicationEvent(ApplicationEvent event) {
     if (event instanceof EmailEvent) {
        EmailEvent emailEvent = (EmailEvent)event;
        System.out.println("邮件地址:" + emailEvent.getAddress());
        System.our.println("邮件内容:" + emailEvent.getText());
     } else {
        System.our.println("容器本身事件:" + event);
     }
   }
}

发送邮件后,触发事件,代码如下:

public class SpringTest {
   public static void main(String args[]){
     ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
     //创建一个ApplicationEvent对象
     EmailEvent event = new EmailEvent("hello","abc@163.com","This is a test");
     //主动触发该事件
     context.publishEvent(event);
   }
}

SpringBoot的CommandLineRunner接口

  • 当容器初始化完成之后会调用CommandLineRunner中的run()方法,同样能够达到容器启动之后完成一些事情。这种方式和ApplicationListener相比更加灵活,如下:

  • 不同的CommandLineRunner实现可以通过@Order()指定执行顺序

@Component
@Slf4j
public class CustomCommandLineRunner implements CommandLineRunner {

    /**
     * @param args 接收控制台传入的参数
     */
    @Override
    public void run(String... args) throws Exception {
        log.debug("从控制台接收参数>>>>"+ Arrays.asList(args));
    }
}

SpringBoot的ApplicationRunner接口

与CommandLineRunner类似

@PostConstruct注解

  • 前三种针对的是容器的初始化完成之后做的一些事情,@PostConstruct这个注解是针对Bean的初始化完成之后做一些事情,比如注册一些监听器...

  • @PostConstruct注解一般放在Bean的方法上,一旦Bean初始化完成之后,将会调用这个方法,代码如下:

@Component
@Slf4j
public class SimpleExampleBean {

    @PostConstruct
    public void init(){
        log.debug("Bean初始化完成,调用...........");
    }
}

@Bean注解中指定初始化方法

  • 这种方式和@PostConstruct比较类似,同样是指定一个方法在Bean初始化完成之后调用。
  • 新建一个Bean,代码如下:
@Slf4j
public class SimpleExampleBean {

    public void init(){
        log.debug("Bean初始化完成,调用...........");
    }
}
  • 在配置类中通过@Bean实例化这个Bean,不过@Bean中的initMethod这个属性需要指定初始化之后需要执行的方法,如下:
@Bean(initMethod = "init")
    public SimpleExampleBean simpleExampleBean(){
        return new SimpleExampleBean();
    }

InitializingBean接口

  • InitializingBean的用法基本上与@PostConstruct一致,只不过相应的Bean需要实现afterPropertiesSet方法,代码如下:
@Slf4j
@Component
public class SimpleExampleBean implements InitializingBean {

    @Override
    public void afterPropertiesSet()  {
        log.debug("Bean初始化完成,调用...........");
    }
}
posted @ 2024-03-11 11:33  hasome  阅读(19)  评论(0编辑  收藏  举报