• 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);
        }
    }
    

    未经作者同意请勿转载

    本文来自博客园作者:aixueforever,原文链接:https://www.cnblogs.com/aslanvon/p/15770086.html

  • 相关阅读:
    Django——Model
    正则表达式
    day03数据类型
    创建一个 Django 项目
    JAVA获取MYSQL数据库表、字段、字段类型、字段注释
    Django
    取splist的某个字段的值
    如何获取列表项的创建者的邮件地址
    使用 HTML 表单 Web 部件筛选并显示另一 Web 部件中的数据
    vs2010+Aspx进行sharepoint2010工作流开发(1)
  • 原文地址:https://www.cnblogs.com/aslanvon/p/15770086.html
Copyright © 2020-2023  润新知