Spring Boot的listener简单使用

监听器(Listener)的注册方法和 Servlet 一样,有两种方式:代码注册或者注解注册

1.代码注册方式

通过代码方式注入过滤器

    @Bean
    public ServletListenerRegistrationBean servletListenerRegistrationBean(){
        ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean();
        servletListenerRegistrationBean.setListener(new IndexListener());
        return servletListenerRegistrationBean;
    }

IndexListener.Java类:

package com.example.Listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class IndexListener implements ServletContextListener{

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        System.out.println("IndexListener contextDestroyed method");
        
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        System.out.println("IndexListener contextInitialized method");
        
    }

}

2.注解方式

通过注解方式注入过滤器

IndexListener2.Java

package com.example.Listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class IndexListener2 implements ServletContextListener{

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        System.out.println("IndexListener2 contextDestroyed method");
        
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        System.out.println("IndexListener2 contextInitialized method");
        
    }

}

把注解加到入口处启动即可

@SpringBootApplication
@ServletComponentScan
public class SpringBootSimpleApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(SpringBootSimpleApplication.class, args);
    }
}

转载于:https://www.cnblogs.com/web424/p/6755963.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot的监听器是用于监听应用程序中特定事件的组件,当这些事件发生时,监听器会执行相应的逻辑。 下面是一个简单使用Spring Boot监听器的示例: 1. 创建一个自定义的监听器类,实现Spring的ApplicationListener接口。例如: ```java public class MyApplicationListener implements ApplicationListener<ApplicationReadyEvent> { @Override public void onApplicationEvent(ApplicationReadyEvent event) { // 处理应用程序启动完成事件 System.out.println("应用程序已启动!"); } } ``` 2. 在Spring Boot应用程序的入口类中,通过注解@EnableAutoConfiguration或@SpringBootApplication启用自动配置,并通过@ComponentScan扫描自定义监听器类。例如: ```java @EnableAutoConfiguration @ComponentScan public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 3. 运行应用程序时,自定义监听器的onApplicationEvent方法将会在应用程序启动完成时被调用。例如,当应用程序启动完成时,控制台将会打印出"应用程序已启动!"。 此外,Spring Boot还提供了其他类型的监听器,用于监听不同类型的事件,如应用程序启动前事件、应用程序关闭事件等。你可以根据需要实现不同的监听器,并注册到Spring Boot应用程序中。 总结:Spring Boot的监听器用于监听特定事件,并在事件发生时执行相应的逻辑。通过自定义监听器类和在入口类中注册监听器,我们可以方便地使用监听器来处理应用程序中的各种事件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值