Springboot中Web原生组件注入(Servlet、Filter、Listener)

一、Servlet

1.新建一个类继承HttpServlet,并且使用@WebServlet注解

@WebServlet(urlPatterns = "/myservlet")//拦截的servlet路径
public class MyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("myServlet");
    }
}

2.在main类上方加入扫描

@ServletComponentScan(basePackages = "com.boot.servlet")//指定扫描哪个包下的servlet
@SpringBootApplication
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class,args);
    }
}

效果:直接响应,不通过Spring的拦截器

二、Filter

1.实现Filter接口

@WebFilter(urlPatterns = {"/css/*","/images/*"})//过滤的路径
public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("myfilter初始化完成");
    }

    @Override
    public void destroy() {
        System.out.println("myfilter销毁");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        filterChain.doFilter(servletRequest,servletResponse);
    }
}

 2.在main类上方加入扫描

@ServletComponentScan(basePackages = "com.boot.servlet")//指定扫描哪个包下的servlet
@SpringBootApplication
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class,args);
    }
}

三、Listener

1.实现ServletContextListener接口

@WebListener
public class MyservletContextListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("监听到项目初始化完成");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("监听到项目销毁");
    }
}

 2.在main类上方加入扫描

@ServletComponentScan(basePackages = "com.boot.servlet")//指定扫描哪个包下的servlet
@SpringBootApplication
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class,args);
    }
}

四、使用RegistrationBean

@Configuration
public class MyRegistConfig {

    @Bean
    public ServletRegistrationBean myserlvet()
    {
        MyServlet myServlet = new MyServlet();
        return new ServletRegistrationBean(myServlet,"/servlet01","/servlet02");
    }

    @Bean
    public FilterRegistrationBean myFilter()
    {
        MyFilter myFilter = new MyFilter();
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(myFilter);
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/css/*","/js/*"));
        return filterRegistrationBean;
    }

    @Bean
    public ServletListenerRegistrationBean myListener(){
        MyservletContextListener myservletContextListener = new MyservletContextListener();
        return new ServletListenerRegistrationBean(myservletContextListener);
    }
}

你可能感兴趣的:(spring-boot,java,spring,boot,springmvc,ssm,spring)