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

1、使用Servlet API 注入(Servlet、Filter、Listener)

目录结构

在这里插入图片描述

1.Servlet组件注入

步骤:
1.必须继承httpservlert
2.要加@WebServlet(urlpatterns=“/my”),注解设置访问路径
3.使用Servlet Api都要在Appliocation启动类中添加扫包

@ServletComponentScan(basePackages = "com.yan")

application启动类,下1.2.3.相同

package com.yan;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
//对于SpringBoot中使用原生的组件注入,servlet要配置扫描器,@ServletComponentScan(basePackages = "com.yan")
@ServletComponentScan(basePackages = "com.yan")
@SpringBootApplication
public class Application {

    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);
    }

}

示例:Myservlet.class

package com.yan.servlet;

import javax.jws.WebService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//1.必须继承httpservlert
//2.要加@WebServlet(urlpatterns="/my"),注解设置访问路径

@WebServlet(urlPatterns = "/my")
public class Myservlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("66666");
    }
}

2.Filter组件注入

步骤:
1.自定义的Filter(过滤器)必须实现,Javax。下Servlet的Filter类,然后重写方法。
2.最后在类上标注@WebFilter注解,注意要在启动类上配置@ServletComponentScan(basePackages = “com.yan”)注解,用以扫描包

示例:MyServletContextListener.class

package com.yan.servlet;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
//自定义的Filter(过滤器)必须实现,Javax。下Servlet的Filter类,然后重写方法。最后在类上标注@WebFilter注解,注意要在
//启动类上配置@ServletComponentScan(basePackages = "com.yan")注解,用以扫描包

package com.yan.servlet;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
//自定义的Filter(过滤器)必须实现,Javax。下Servlet的Filter类,然后重写方法。最后在类上标注@WebFilter注解,注意要在
//启动类上配置@ServletComponentScan(basePackages = "com.yan")注解,用以扫描包

@WebFilter(urlPatterns = {"/css/*","/images/*"})//拦截css下所有路径
@Slf4j
public class MyFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.info("MyFilter初始化完成");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        log.info("MyFilter工作");
        filterChain.doFilter(servletRequest,servletResponse);

    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}

@Slf4j
public class MyFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.info("MyFilter初始化完成");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        log.info("MyFilter工作");
        filterChain.doFilter(servletRequest,servletResponse);

    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}

3.Listener组件注入

步骤:
1.实现自定义监听器,需要实现 ServletContextListener接口,然后添加@WebListener注解,重写方法,
2.在启动类上配置@ServletComponentScan(basePackages = “com.yan”)注解,用以扫描包

package com.yan.servlet;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

//实现自定义监听器,需要实现 ServletContextListener接口,然后添加@WebListener注解,重写方法,在
//启动类上配置@ServletComponentScan(basePackages = "com.yan")注解,用以扫描包

@WebListener
@Slf4j
public class MyServletContextListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        log.info("MyServletContextListener监听到项目初始化完成");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        log.info("MyServletContextListener监听到项目销毁");
    }
}

2、使用RegistrationBean 注入(比较简便,只需要在配置类中注入即可生效)

包括下面3个:

  1. ServletRegistrationBean
  2. FilterRegistrationBean
  3. ServletListenerRegistrationBean
package com.yan.servlet;

import jdk.nashorn.internal.objects.NativeReferenceError;
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 javax.servlet.ServletContextListener;
import java.util.Arrays;

@Configuration
public class MyRegistConfig {
    @Bean
    public ServletRegistrationBean myservlet(){
        Myservlet myservlet = new Myservlet();
        return new ServletRegistrationBean(myservlet,"/my","/my02");

    }
    @Bean
    //拦截器
    public FilterRegistrationBean myFilter(){
        MyFilter myFilter = new MyFilter();
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(myFilter);
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/my","/css/*"));//

        return filterRegistrationBean;
    }
    @Bean
    //
    public ServletListenerRegistrationBean myListener(){
        MyServletContextListener myServletContextListener = new MyServletContextListener();
        return new ServletListenerRegistrationBean(myServletContextListener);
    }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值