Springboot笔记<9>使用Javaweb原生组件

使用Javaweb原生组件

Javaweb的三大组件:servlet,Filter,Listener,Servlet 3.0 提供了以下 3 个注解:

  • @WebServlet:用于声明一个 Servlet;
  • @WebFilter:用于声明一个 Filter;
  • @WebListener:用于声明一个 Listener。

原生组件注册

注解方法

在主类上添加@ServletComponentScan(“具体的包”)『@ServletComponentScan(basePackages = "com.top.springbootreview.webComponent")』可以不用在原生组件上面添加原生的组件注释(@WebServlet(),@WebFilter(),@WebListener)。如果只主类上添加@ServletComponentScan需要在原生组件类上添加原生组件注解@WebServlet(),@WebFilter(),@WebListener。**

使用 RegistrationBean 注册

我们可以在配置类中,使用 @Bean 注解将 ServletRegistrationBean、FilterRegistrationBean 和 ServletListenerRegistrationBean 添加 Spring 容器中,并通过它们将我们自定义的 Servlet、Filter 和 Listener 组件注册到容器中使用。使用方式见下文。

servlet

@Slf4j
@WebServlet("/sayHello")
public class SayHelloHttpServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        log.info("SayHelloHttpServlet -> doGet");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("SayHelloHttpServlet -> doPost");
    }
}

Filter

@WebFilter(urlPatterns="/*", filterName = "myFilterByAnnotation")
@Order(value = 3)
public class MyFilterByAnnotation implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("myFilterByAnnotation提示:服务启动..........");
    }

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

        System.out.println("myFilterByAnnotation提示:调用过滤器Filter的doFilter()方法..........");
        System.out.println("ip地址为"+servletRequest.getRemoteAddr());
        //放行通过
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

        System.out.println("myFilterByAnnotation提示:服务关闭..........");
    }
}

Listener

@WebListener
public class MyListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("MyListener 监听到 ServletContext 初始化");
    }
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("MyListener 监听到 ServletContext 销毁");
    }
}
//项目启动时输出了MyListener 监听到 ServletContext 初始化

不使用@WebListener @WebServlet("/sayHello")注解,使用 RegistrationBean 注册:

启动后访问/sayHello2,控制台打印webComponent.SayHelloHttpServlet2 : SayHelloHttpServlet -> doGet

同时会有两个MyListener ,输出为:

​ MyListener 监听到 ServletContext 初始化
​ MyListener 监听到 ServletContext 初始化

@Slf4j
public class SayHelloHttpServlet2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        log.info("SayHelloHttpServlet -> doGet");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("SayHelloHttpServlet -> doPost");
    }
}


public class MyListener2 implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("MyListener 监听到 ServletContext 初始化");
    }
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("MyListener 监听到 ServletContext 销毁");
    }
}



@Controller
@Slf4j
public class WebComponentToIOC {
    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        SayHelloHttpServlet2 myServlet = new SayHelloHttpServlet2();
        return new ServletRegistrationBean(myServlet, "/sayHello2");
    }

    @Bean
    public ServletListenerRegistrationBean servletListenerRegistrationBean() {
        MyListener myListener = new MyListener();
        return new ServletListenerRegistrationBean(myListener);
    }
}
posted @ 2022-01-06 10:43  aixueforever  阅读(241)  评论(0编辑  收藏  举报