Java框架_SpringBoot整合Listener

本文介绍两种在Spring Boot中注册Listener的方法:一种是使用@WebListener注解进行自动扫描;另一种是在启动类中通过@Bean方法手动注册。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,通过注解扫描完成Listener组件的注册

  • 编写Listener类
    package com.lxp.Listener;
    
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    import javax.servlet.annotation.WebListener;
    
    @WebListener
    public class FristListener implements ServletContextListener {
    
    	@Override
    	public void contextDestroyed(ServletContextEvent arg0) {
    	
    	}
    	@Override
    	public void contextInitialized(ServletContextEvent arg0) {
    		System.out.println("Listener...Init....");
    	}
    
    }
    
    编写启动类
    package com.lxp;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.servlet.FilterRegistrationBean;
    import org.springframework.boot.web.servlet.ServletComponentScan;
    import org.springframework.boot.web.servlet.ServletRegistrationBean;
    import org.springframework.context.annotation.Bean;
    
    import com.lxp.filter.SecondFilter;
    import com.lxp.servlet.SecondServlet;
    
    /**
     * @author lxp
     * @date 2018年6月19日 上午11:33:15
     * @parameter
     * @return
     */
    
    @SpringBootApplication
    @ServletComponentScan // 方法一:在springBoot启动时会扫描@WebListener,并将该类实例化
    public class AppStart {
    
    	public static void main(String[] args) {
    		SpringApplication.run(AppStart.class, args);
    	}
    }
    

2,通过方法完成Listener组件的注册
  • 编写Listener
package com.lxp.Listener;

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

public class SecondListener implements ServletContextListener {

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {

	}

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

}

  • 编写启动类
package com.lxp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

import com.lxp.Listener.SecondListener;

@SpringBootApplication
public class AppStart {

	public static void main(String[] args) {
		SpringApplication.run(AppStart.class, args);
	}

	// 方法二:通过方法完成Listener组件的注册
	@Bean
	public ServletListenerRegistrationBean<SecondListener> getServletListenerRegistrationBean() {
		ServletListenerRegistrationBean<SecondListener> bean = new ServletListenerRegistrationBean(new SecondListener());
		return bean;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值