springboot使用原生servlet、filter、listener
生活随笔
收集整理的這篇文章主要介紹了
springboot使用原生servlet、filter、listener
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
目錄
一、使用注解注冊原生web組件
1.使用原生servlet
2.使用原生filter
3.使用原生listener
二、使用RegistrationBean注冊原生web組件
三、發(fā)現(xiàn)一個問題,使用原生的servlet,并不會觸發(fā)spring的攔截器。
四、DispatcherServlet的自動配置
1.springmvc處理邏輯參考博文
2.spring主要的servlet-DispatcherServlet的自動配置
一、使用注解注冊原生web組件
1.使用原生servlet
(1)啟動類
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan;// 掃描servlet,指定原生Servlet組件都放在那里 @ServletComponentScan("com.cxf.servlet") @SpringBootApplication public class WebApplicationCxf {public static void main(String[] args) {SpringApplication.run(WebApplicationCxf.class, args);} }(2)自定義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;// 使用原生servlet API,會直接響應(yīng),不會經(jīng)過spring的攔截器等 @WebServlet(urlPatterns = "/my") public class MyServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {resp.getWriter().write("66666");} }2.使用原生filter
import lombok.extern.slf4j.Slf4j; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException;@Slf4j @WebFilter(urlPatterns={"/my","/images/*"}) //攔截的路徑 public class MyFilter implements Filter {@Overridepublic void init(FilterConfig filterConfig) throws ServletException {log.info("MyFilter初始化完成");}@Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {log.info("MyFilter工作");chain.doFilter(request,response);}@Overridepublic void destroy() {log.info("MyFilter銷毀");} }3.使用原生listener
import lombok.extern.slf4j.Slf4j; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener;// 監(jiān)聽context初始化 @Slf4j @WebListener public class MySwervletContextListener implements ServletContextListener {@Overridepublic void contextInitialized(ServletContextEvent sce) {log.info("MySwervletContextListener監(jiān)聽到項目初始化完成");}@Overridepublic void contextDestroyed(ServletContextEvent sce) {log.info("MySwervletContextListener監(jiān)聽到項目銷毀");} }二、使用RegistrationBean注冊原生web組件
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;// (proxyBeanMethods = true):保證依賴的組件始終是單實例的,默認就是true @Configuration(proxyBeanMethods = true) public class MyRegistConfig {// 注冊servlet@Beanpublic ServletRegistrationBean myServlet(){MyServlet myServlet = new MyServlet();// servlet + url映射return new ServletRegistrationBean(myServlet,"/my","/my02");}@Beanpublic FilterRegistrationBean myFilter(){MyFilter myFilter = new MyFilter(); // return new FilterRegistrationBean(myFilter,myServlet());FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(myFilter);filterRegistrationBean.setUrlPatterns(Arrays.asList("/my","/css/*")); // 可以攔截的路徑return filterRegistrationBean;}@Beanpublic ServletListenerRegistrationBean myListener(){MySwervletContextListener mySwervletContextListener = new MySwervletContextListener();return new ServletListenerRegistrationBean(mySwervletContextListener);} }三、發(fā)現(xiàn)一個問題,使用原生的servlet,并不會觸發(fā)spring的攔截器。
這是為什么呢?
原因:spring默認是注冊一個DispatcherServlet,而我們再注冊一個原生的servlet,這樣容器中就會有兩個servlet,而DispatcherServlet匹配的根路徑是 / ,原生的servlet匹配的路徑是/my,根據(jù)最優(yōu)匹配,請求/my的時候會走原生的servlet,不會走spring的那一套,所以不會觸發(fā)spring的攔截器,而spring的請求也不會觸發(fā)原生的攔截器。
四、DispatcherServlet的自動配置
1.springmvc處理邏輯參考博文
springBoot-springMVC請求處理原理_私人博客,有需要請聯(lián)系17854238061(vx同號)-CSDN博客
2.spring主要的servlet-DispatcherServlet的自動配置
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE) @Configuration(proxyBeanMethods = false) @ConditionalOnWebApplication(type = Type.SERVLET) @ConditionalOnClass(DispatcherServlet.class) @AutoConfigureAfter(ServletWebServerFactoryAutoConfiguration.class) public class DispatcherServletAutoConfiguration {/** The bean name for a DispatcherServlet that will be mapped to the root URL "/"*/public static final String DEFAULT_DISPATCHER_SERVLET_BEAN_NAME = "dispatcherServlet";/** The bean name for a ServletRegistrationBean for the DispatcherServlet "/"*/public static final String DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME = "dispatcherServletRegistration";@Configuration(proxyBeanMethods = false)@Conditional(DefaultDispatcherServletCondition.class)@ConditionalOnClass(ServletRegistration.class)@EnableConfigurationProperties(WebMvcProperties.class)protected static class DispatcherServletConfiguration {// 注冊DispatcherServlet ,id就是dispatcherServlet。屬性綁定到 WebMvcProperties;對應(yīng)的配置文件配置項是 spring.mvc@Bean(name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)public DispatcherServlet dispatcherServlet(WebMvcProperties webMvcProperties) {DispatcherServlet dispatcherServlet = new DispatcherServlet();dispatcherServlet.setDispatchOptionsRequest(webMvcProperties.isDispatchOptionsRequest());dispatcherServlet.setDispatchTraceRequest(webMvcProperties.isDispatchTraceRequest());dispatcherServlet.setThrowExceptionIfNoHandlerFound(webMvcProperties.isThrowExceptionIfNoHandlerFound());dispatcherServlet.setPublishEvents(webMvcProperties.isPublishRequestHandledEvents());dispatcherServlet.setEnableLoggingRequestDetails(webMvcProperties.isLogRequestDetails());return dispatcherServlet;}// 注冊文件上傳解析器@Bean@ConditionalOnBean(MultipartResolver.class)@ConditionalOnMissingBean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME)public MultipartResolver multipartResolver(MultipartResolver resolver) {// Detect if the user has created a MultipartResolver but named it incorrectlyreturn resolver;}}@Configuration(proxyBeanMethods = false)@Conditional(DispatcherServletRegistrationCondition.class)@ConditionalOnClass(ServletRegistration.class)@EnableConfigurationProperties(WebMvcProperties.class)@Import(DispatcherServletConfiguration.class)protected static class DispatcherServletRegistrationConfiguration {// 通過 ServletRegistrationBean<DispatcherServlet> 把 DispatcherServlet 配置進來@Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)@ConditionalOnBean(value = DispatcherServlet.class, name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)public DispatcherServletRegistrationBean dispatcherServletRegistration(DispatcherServlet dispatcherServlet,WebMvcProperties webMvcProperties, ObjectProvider<MultipartConfigElement> multipartConfig) {DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(dispatcherServlet,webMvcProperties.getServlet().getPath()); // 映射的是 / 路徑registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);registration.setLoadOnStartup(webMvcProperties.getServlet().getLoadOnStartup());multipartConfig.ifAvailable(registration::setMultipartConfig);return registration;}}…… }總結(jié)
以上是生活随笔為你收集整理的springboot使用原生servlet、filter、listener的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springboot-嵌入式Servle
- 下一篇: springboot2使用JUnit5单