package com.itheima.servletComponent;

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

import org.springframework.stereotype.Component;

@Component
public class MyListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        System.out.println("contextInitialized ...");
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        System.out.println("contextDestroyed ...");
    }
}
package com.itheima.config;

import java.util.Arrays;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.itheima.servletComponent.MyFilter;
import com.itheima.servletComponent.MyListener;
import com.itheima.servletComponent.MyServlet;

@Configuration
public class ServletConfig {
//    注册Servlet组件
    @Bean
    public ServletRegistrationBean<MyServlet> getServlet(MyServlet myServlet) {
        ServletRegistrationBean<MyServlet> registrationBean = new ServletRegistrationBean<MyServlet>(myServlet, "/myServlet");
        return registrationBean;
    }
    
    // 注册Filter组件
    @Bean
    public FilterRegistrationBean<MyFilter> getFilter(MyFilter filter) {
        FilterRegistrationBean<MyFilter> registrationBean = new FilterRegistrationBean<MyFilter>(filter);
        registrationBean.setUrlPatterns(Arrays.asList("/toLoginPage", "/myFilter"));
        return registrationBean;
    }
    
    //注册Listener组件
    @Bean
    public ServletListenerRegistrationBean<MyListener> getServletListener(MyListener myListener) {
        ServletListenerRegistrationBean<MyListener> registrationBean = new ServletListenerRegistrationBean<MyListener>(myListener);
        return registrationBean;
    }
}