阅读 81

Spring 事件机制(ApplicationListener)

1. 事件类型

A. ContextRefreshedEvent —— ApplicationContext容器初始化或者刷新时触发该事件;

    B. ContextStartedEvent —— 当使用ConfigurableApplicationContext接口的start()方法启动ApplicationContext容器时触发该事件;

    C. ContextClosedEvent —— 当使用ConfigurableApplicationContext接口的close()方法关闭ApplicationContext容器时触发该事件;

    D. ContextStopedEvent —— 当使用ConfigurableApplicationContext接口的stop()方法停止ApplicationContext容器时触发该事件。

2. 举例

package com.ruhuanxingyun;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.nsac.business.listener.ApplicationStartup;

@SpringBootApplication
public class SpApp {
    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(SpApp.class);
        springApplication.addListeners(new ApplicationStartup());
        springApplication.run(args);
    }
}
package com.nsac.ruhuanxingyun;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

public class ApplicationStartup implements ApplicationListener {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        ApplicationContext ac = event.getApplicationContext();
        StepExecutor StepExecutor = new StepExecutor(ac.getEnvironment().getProperty("project-id"), ac.getBean("businessSingleJedisPool",redis.clients.jedis.JedisPool.class), 
                                ac.getBean("redisCluster", redis.clients.jedis.JedisCluster.class));
        Thread thread = new Thread(StepExecutor);
        thread.start();
    }

}

 

原文:https://www.cnblogs.com/ruhuanxingyun/p/14652902.html

文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 gxwowoo@163.com 举报,一经查实,本站将立刻删除。
相关推荐