Springboot中的原生servlet和RegistrationBean(将servlet注入到ioc)

  • Post author:
  • Post category:其他


原生的servlet,还是和原来一样,继承HttpServlet接口,添加@WebServlet注解

在springboot的主配置类中添加servlet的扫描即可

package com.example.springbootdemo3.servlet;

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;
import java.io.PrintWriter;

@WebServlet("/yuansheng_servlet")
public class Yuansheng_Servlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        this.doPost(req, resp);
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out = resp.getWriter();
        out.print("访问了原生的servlet");
    }
}
package com.example.springbootdemo3;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@ServletComponentScan(basePackages = "com.example.springbootdemo3.servlet")
@SpringBootApplication
public class SpringBootDemo3Application {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootDemo3Application.class, args);
	}

}

在springboot中,默认给controller配置了编码,所以不需要设置编码。

在原生的servlet中,不会经过springmvc,就需要配置编码了。

原生的filter(拦截器) 和原生的linstener(监听器)

不需要在主类中添加servlet的扫描了  和原生的一摸一样

这是原生的servlet /filter  所以要使用  /* 表示拦截所有的请求,在springmvc中/**表示拦截所有请求

package com.example.springbootdemo3.servlet;


import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@Slf4j
@WebFilter(urlPatterns = "/*")
public class YuanSheng_filter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
       log.info("filter初始化");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        log.info("filter拦截");
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {
        log.info("filter销毁");
        Filter.super.destroy();
    }
}
package com.example.springbootdemo3.servlet;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.ApplicationListenerMethodAdapter;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.lang.reflect.Method;

@Slf4j
@WebListener
public class YuanSheng_linstener implements ServletContextListener {
    public void contextInitialized(ServletContextEvent sce) {
        log.info("监听到了applicationcontext创建");
    }

    public void contextDestroyed(ServletContextEvent sce) {
        log.info("applicationcontext销毁");
    }
}

RegistrationBean(将servlet注入到ioc)

和上面一样,都要写servelt类,实现对应的接口,重写方法,只是不用再写

@WebServlet("/yuansheng_servlet")
@WebFilter(urlPatterns = "/*")
@WebListener

而是写一个控制类,在控制类中统一注册进ioc中

package com.example.springbootdemo3.servlet;

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 java.util.Arrays;

@Configuration(proxyBeanMethods = true)
public class RegistrationBean {
    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new Yuansheng_Servlet(), "/*");
        return servletRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new YuanSheng_filter(), servletRegistrationBean());
//两种写法,可以直接调用servletRegistrationBean设置和servelt相同的拦截路径
//或者直接设置拦截路径
//        filterRegistrationBean.setUrlPatterns(Arrays.asList("/index"));
        return filterRegistrationBean;
    }

    @Bean
    public ServletListenerRegistrationBean mylisnstener() {
        ServletListenerRegistrationBean servletListenerRegistrationBean=new ServletListenerRegistrationBean(new YuanSheng_linstener());
        return servletListenerRegistrationBean;
    }

}
@Configuration(proxyBeanMethods = true)
如果是false,则会filetrRegistrationbean方法调用了servletRegistationBean,新建一个新的 ServletRegistrationBean给 filetrRegistrationbean使用,这样容器中就有两个servletRegistationBean了,虽然不会影响功能,但是不太好。
默认这个属性是true的,即filetrRegistrationbean方法调用了servletRegistationBean,获得到的ioc是不会新建的,容器中只有一个servletRegistationBean。



版权声明:本文为sharesb原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。