springboot 监听器

1系统监听器

和过滤器差不多就不详细说了
类 ListenerConfig

@Configuration
public class ListenerConfig {

    @Bean
    public EventListener demoListener(){
        return new DemoListener();
    }

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


}

类 DemoListener

/**
 * 系统的监听器
 * 1.ServletContextListener -- 监听servletContext对象的创建以及销毁
 * 2.HttpSessionListener  -- 监听session对象的创建以及销毁
 * 3.ServletRequestListener -- 监听request对象的创建以及销毁
 * 4.ServletContextAttributeListener  -- 监听servletContext对象中属性的改变
 * 5.HttpSessionAttributeListener  --监听session对象中属性的改变
 * 6.ServletRequestAttributeListener  --监听request对象中属性的改变
 * 7.自定义监听器,需要配置事件,监听器类
 */
@Slf4j
public class DemoListener implements ServletContextListener, HttpSessionAttributeListener {
    /**
     * * Notification that the web application initialization process is starting.
     * All ServletContextListeners are notified of context initialization before
     * any filter or servlet in the web application is initialized.
     * The default implementation is a NO-OP.
     *
     * @param sce Information about the ServletContext that was initialized
     */
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        log.info("上下文初始化监听{}",sce);
    }

    /**
     * * Notification that the servlet context is about to be shut down. All
     * servlets and filters have been destroyed before any
     * ServletContextListeners are notified of context destruction.
     * The default implementation is a NO-OP.
     *
     * @param sce Information about the ServletContext that was destroyed
     */
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        log.info("上下文销毁{}",sce);

    }

    /**
     * Notification that an attribute has been added to a session. Called after
     * the attribute is added.
     * The default implementation is a NO-OP.
     *
     * @param se Information about the added attribute
     */
    @Override
    public void attributeAdded(HttpSessionBindingEvent se) {
        log.info("session添加{}",se.getValue());
    }

    /**
     * Notification that an attribute has been removed from a session. Called
     * after the attribute is removed.
     * The default implementation is a NO-OP.
     *
     * @param se Information about the removed attribute
     */
    @Override
    public void attributeRemoved(HttpSessionBindingEvent se) {
        log.info("session删除{}",se.getValue());
    }

    /**
     * Notification that an attribute has been replaced in a session. Called
     * after the attribute is replaced.
     * The default implementation is a NO-OP.
     *
     * @param se Information about the replaced attribute
     */
    @Override
    public void attributeReplaced(HttpSessionBindingEvent se) {
        log.info("session替换{}",se.getValue());
    }
}

然后操作会有监听,

2自定义监听器

1.创建事件

/**
 * 自定义
 * 监听事件
 */
public class DemoEvent extends ApplicationEvent {

    public String mas;

    public DemoEvent(Object source,String mas) {
        super(source);
        this.mas=mas;
    }

    /**
     * Create a new {@code ApplicationEvent}.
     *
     * @param source the object on which the event initially occurred or with
     *               which the event is associated (never {@code null})
     */
    public DemoEvent(Object source) {
        super(source);
    }
}

2注入自定义监听器

/**
 * 注入自定义监听器
 */
@Slf4j
@Component
public class CustomizeListener implements ApplicationListener<DemoEvent> {

    /**
     * Handle an application event.
     *
     * @param event the event to respond to
     */
    @Override
    public void onApplicationEvent(DemoEvent event) {
        log.info("DemoListener:"+event.mas);
    }
}

3. 发布事件

    @Autowired
    private ApplicationContext applicationContext;
    /**
     * 自定义监听器
     */
    @GetMapping("listener")
    public void listener() {
    applicationContext.publishEvent(new DemoEvent(this,"哈哈"));
    }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值