springboot的4种监听器方式

1. 这里的事件是什么意思:实际上就是一个指定类型的对象实例。

2. 事件监听器能做什么:只要发布了事件,就会触发监听器的执行。

定义事件:

public class MyEvent extends ApplicationEvent {
    // 第一个参数是不可缺少的,后面的参数可以是任意多个(或零个,自定义)
    public MyEvent(Object source, String info) { 
        super(source);
        //内容是自定义的
        System.out.println("事件已经发生了:" + info);
    }
}

定义监听器,有4种方法,第一种(纯java方式):

public class MyListener implements ApplicationListener<MyEvent> {
    //一旦springboot 发布了MyEvent事件,就会触发执行此方法,方法内容自定义
    @Override
    public void onApplicationEvent(MyEvent myEvent) {
        System.out.println("触发执行了监听器");
    }
}

@SpringBootApplication
public class Application {
    public static void main(String[] args){
        SpringApplication springApplication = new SpringApplication(Application.class);
        springApplication.addListeners(new MyListener());
        ConfigurableApplicationContext context = springApplication.run(args);
        //发布事件
        context.publishEvent(new MyEvent(new Object(), "hello"));
    }
}

第二种(注解方式):

@Component
public class MyListener2 implements ApplicationListener<MyEvent> {
    @Override
    public void onApplicationEvent(MyEvent myEvent) {
        System.out.println("触发执行了监听器");
    }
}

@SpringBootApplication
public class Application {
    public static void main(String[] args){
        SpringApplication springApplication = new SpringApplication(Application.class);
        //用Component注入了监听器后,就不用再addListener了
        ConfigurableApplicationContext context = springApplication.run(args);
        context.publishEvent(new MyEvent(new Object(), "hello"));
    }
}

第三种(配置文件形式),在application.yml中配置:

context:
  listener:
    classes: com.dangh.test.listener.MyListener
@SpringBootApplication
public class Application {
    public static void main(String[] args){
        SpringApplication springApplication = new SpringApplication(Application.class);
        //在配置文件中配置了监听器,就不用再addListener了
        ConfigurableApplicationContext context = springApplication.run(args);
        context.publishEvent(new MyEvent(new Object(), "hello"));
    }
}

第四种(@EventListener注解,不用实现ApplicationListener接口了):

@Component
public class MyListener3 {
    @EventListener
    public void listenerMethod(MyEvent myEvent){
        System.out.println("监听器执行了");
    }
}

@SpringBootApplication
public class Application {
    public static void main(String[] args){
        SpringApplication springApplication = new SpringApplication(Application.class);
        //在配置文件中配置了监听器,就不用再addListener了
        ConfigurableApplicationContext context = springApplication.run(args);
        context.publishEvent(new MyEvent(new Object(), "hello"));
    }
}

个人觉得第二种和第四种方式要好一些,因为简单明了,代码可读性好,对于第二种,一看实现了ApplicationListener接口就知道是个监听器,对于第四种,一看@EventListener注解就知道是个监听器。

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于SpringBoot实现Redis监听器的问题,可以参考以下步骤: 1. 添加相关依赖 在pom.xml文件中添加如下依赖: ``` <dependency>     <groupId>org.springframework.boot</groupId>     <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2. 创建监听器 创建一个类,实现org.springframework.data.redis.connection.MessageListener接口,并在该类中重写onMessage方法,监听Redis中指定key的变化。 ``` @Component public class RedisMessageListener implements MessageListener { @Override public void onMessage(Message message, byte[] pattern) { // 监听到key的变化后,在这里实现具体的操作 } } ``` 3. 配置监听器 在Spring Boot的配置类中,注入RedisMessageListener,并通过RedisConnectionFactory创建监听器容器,并指定需要监听的key。 ``` @Configuration public class RedisConfig { @Autowired private RedisMessageListener redisMessageListener; @Bean MessageListenerAdapter listenerAdapter() { return new MessageListenerAdapter(redisMessageListener); } @Bean RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) { RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(connectionFactory); container.addMessageListener(listenerAdapter(), new PatternTopic("your_key_pattern")); return container; } } ``` 4. 测试 监听器配置完成后,可以通过RedisTemplate向指定key写入数据,验证是否能够监听到key的变化。 以上就是SpringBoot实现Redis监听器的基本步骤。如有疑问,请随时向我提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值