有哪些学做衣服的网站有哪些,网站框架怎么做的,2144网页游戏大厅,机关局域网网站建设1.基本介绍 文档#xff1a;SpringBoot中注入ServletFilterListener 考虑到实际开发业务非常复杂和兼容问题#xff0c;SpringBoot支持将Servlet、Filter、Listener注入spring容器中#xff0c;成为Spring Bean也就是说#xff0c;SpringBoot开放了和原生WEB组件…1.基本介绍 文档SpringBoot中注入ServletFilterListener 考虑到实际开发业务非常复杂和兼容问题SpringBoot支持将Servlet、Filter、Listener注入spring容器中成为Spring Bean也就是说SpringBoot开放了和原生WEB组件Servlet、Filter、Listener的兼容SpringBoot注入Servlet、Filter、Listener有两种方式 通过注解方式注入使用RegistrationBean方式注入
2.通过注解方式注入
2.1WebServlet
属性名对应标签描述nameservlet-name指定 Servlet 的 name 属性。 如果没有显式指定则取值为该 Servlet 的完全限定名即包名类名valueurl-pattern该属性等价于 urlPatterns 属性两者不能同时指定。 如果同时指定通常是忽略 value 的取值urlPatternsurl-pattern指定一组 Servlet 的 URL 匹配模式loadOnStartupload-on-startup指定 Servlet 的加载顺序initParamsinit-param指定一组 Servlet 初始化参数asyncSupportedasync-supported声明 Servlet 是否支持异步操作模式descriptiondescription指定该 Servlet 的描述信息displayNamedisplay-name指定该 Servlet 的显示名 例子--使用WebServlet注入Servlet 1MyServlet.java 通过继承HttpServlet来开发原生的Servlet 使用WebServlet表示将其标识的对象注入到Spring容器中 urlPatterns {servlet01,servlet02} 对此servlet配置了映射路径 对于开发的原生的Servlet需要使用ServletComponentScan在SpringBoot主程序中指定要扫描的原生Servlet这样该Servlet才能注入容器 package com.li.thymeleaf.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;/*** author 李* version 1.0*/WebServlet(urlPatterns {/servlet01, /servlet02})public class MyServlet extends HttpServlet {Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {resp.getWriter().write(Hello,MyServlet!);}}
2Application.java主程序 package com.li.thymeleaf;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.web.servlet.ServletComponentScan;/*** author 李* version 1.0*///指定扫描ServletServletComponentScan(basePackages com.li.thymeleaf)SpringBootApplicationpublic class Application {public static void main(String[] args) {SpringApplication.run(Application.class,args);}}
3浏览器访问地址http://localhost:8080/servlet01获者http://localhost:8080/servlet02返回如下 注意注入的Servlet不会被SpringBoot的拦截器拦截因为原生Servlet和前端控制器DispatcherServlet是统一级别的而拦截器在DispatcherServlet中 2.2WebFilter
属性名说 明description该过滤器的描述信息等价于 description标签。displayName该过滤器的显示名通常配合工具使用等价于 display-name 标签initParams指定一组过滤器初始化参数等价于 init-param 标签。filterName指定过滤器的 name 属性等价于 filter-nameservletNames指定过滤器将应用于哪些 Servlet。取值是 WebServlet 中的 name 属性的取值或者是 web.xml 中 servlet-name 的取值value/urlPatterns过滤器的 URL 匹配模式等价于url-pattern标签dispatcherTypes指定过滤器的转发模式。具体取值包括 ASYNC、ERROR、FORWARD、INCLUDE、REQUEST。asyncSupported声明过滤器是否支持异步操作模式 等价于async-supported标签 例子--使用WebFilter注入Filter WebFilter标识一个过滤器并注入spring容器 urlPatterns {/css/*, /images/*}表示请求/css/目录或者/images/目录下的资源时请求会经过这个过滤器 需要在主程序中指定要扫描的Filter这样该Filter才能注入容器 package com.li.thymeleaf.filter;import lombok.extern.slf4j.Slf4j;import javax.servlet.*;import javax.servlet.annotation.WebFilter;import javax.servlet.http.HttpServletRequest;import java.io.IOException;/*** author 李* version 1.0* 开发Filter并注入spring容器*/Slf4jWebFilter(urlPatterns {/css/*, /images/*})public class MyFilter implements Filter {Overridepublic void init(FilterConfig filterConfig) throws ServletException {log.info(MyFilter的init()方法被执行...);}Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {log.info(MyFilter的doFilter()方法被执行...);HttpServletRequest httpServletRequest (HttpServletRequest) request;log.info(过滤器处理的uri{}, httpServletRequest.getRequestURI());chain.doFilter(request, response);//放行}Overridepublic void destroy() {log.info(MyFilter的destroy()方法被执行...);}}
2在主程序中配置扫描该过滤器略
3在浏览器访问地址http://localhost:8080/images/login.jpg后台输出 2023-03-23 18:59:36.685 INFO 39228 --- [nio-8080-exec-6] com.li.thymeleaf.filter.MyFilter : MyFilter的doFilter()方法被执行...2023-03-23 18:59:36.685 INFO 39228 --- [nio-8080-exec-6] com.li.thymeleaf.filter.MyFilter : 过滤器处理的uri/images/login.jpg 有时候后台没有输出可能是浏览器缓存问题 2.3WebListener
1MyListener.java package com.li.thymeleaf.listener;import lombok.extern.slf4j.Slf4j;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import javax.servlet.annotation.WebListener;/*** author 李* version 1.0*/Slf4jWebListenerpublic class MyListener implements ServletContextListener {Overridepublic void contextInitialized(ServletContextEvent sce) {//可以加入项目初始化相关的业务log.info(MyListener-contextInitialized()-项目初始化OK~);}Overridepublic void contextDestroyed(ServletContextEvent sce) {//可以加入业务log.info(MyListener-contextDestroyed()-项目初销毁...);}}
2在主程序 Application.java配置扫描该监听器 package com.li.thymeleaf;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.web.servlet.ServletComponentScan;import org.springframework.context.ConfigurableApplicationContext;/*** author 李* version 1.0*///指定扫描监听器ServletComponentScan(basePackages com.li.thymeleaf)SpringBootApplicationpublic class Application {public static void main(String[] args) {ConfigurableApplicationContext ioc SpringApplication.run(Application.class, args);//监听器的contextDestroyed()方法在容器销毁时触发ioc.stop();}}
3启动项目控制台输出 3.使用RegistrationBean方式注入
RegistrationConfig.java package com.li.thymeleaf.config;import com.li.thymeleaf.filter.MyFilter;import com.li.thymeleaf.listener.MyListener;import com.li.thymeleaf.servlet.MyServlet;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;/*** author 李* version 1.0* RegistrationConfig是一个配置类* 默认为单实例模式 proxyBeanMethodstrue*/Configurationpublic class RegistrationConfig {//使用RegistrationBean方式注入ServletBeanpublic ServletRegistrationBean servlet_() {MyServlet myServlet new MyServlet();//将myServlet关联到ServletRegistrationBean对象//可以指定多个映射urlreturn new ServletRegistrationBean(myServlet, /servlet01, /servlet02);}//使用RegistrationBean方式注入FilterBeanpublic FilterRegistrationBean filter_() {MyFilter myFilter new MyFilter();//创建原生的Filter对象FilterRegistrationBean filterRegistrationBean new FilterRegistrationBean(myFilter);//设置filter的urlPatternfilterRegistrationBean.setUrlPatterns(Arrays.asList(/css/*, /images/*));return filterRegistrationBean;}//使用RegistrationBean方式注入ListenerBeanpublic ServletListenerRegistrationBean listener_() {MyListener myListener new MyListener();//创建原生的Listener对象return new ServletListenerRegistrationBean(myListener);}} 使用RegistrationBean的方式注入不必在主程序Application.java中配置扫描 运行程序可以看到三个组件都被注入到容器中 4.注意事项和细节
4.1请求自定义Servlet时为什么不会到达拦截器
原因分析
注入的Servlet会存在Spring容器DispatcherServlet也存在Spring容器。当多个Servlet都能处理到同一层路径时存在精确优先原则/最长前缀匹配原则**精准匹配 目录匹配 扩展名匹配 /* / **
如下图当浏览器请求路径为/servlet01时MyServlet的映射路径对与浏览器请求来说是精准匹配因此此时MyServlet的映射路径优先级高于前端控制器的 /请求路径会走tomcat流程不会到达前端控制器也就不会执行拦截器。 当然在SpringBoot中去调用Controller目标方法仍是按照DispatcherServlet分发匹配的机制 4.2DispatcherServlet在SpringBoot如何进行配置和注入
DispatcherServletAutoConfiguration 完成对 DispatcherServlet 的自动配置。
DispatcherServletAutoConfiguration 类有一个内部类 Configuration(proxyBeanMethods false)Conditional(DefaultDispatcherServletCondition.class)ConditionalOnClass(ServletRegistration.class)EnableConfigurationProperties(WebMvcProperties.class)protected static class DispatcherServletConfiguration {Bean(name DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)//创建了DispatcherServlet对象并进行一系列设置并返回。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;}BeanConditionalOnBean(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;}}
然后通过如下方法创建DispatcherServletRegistrationBean对象并将创建的DispatcherServlet对象关联到这个DispatcherServletRegistrationBean对象中将DispatcherServletRegistrationBean对象通过Bean注入到容器中。 Configuration(proxyBeanMethods false)Conditional(DispatcherServletRegistrationCondition.class)ConditionalOnClass(ServletRegistration.class)EnableConfigurationProperties(WebMvcProperties.class)Import(DispatcherServletConfiguration.class)protected static class DispatcherServletRegistrationConfiguration {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, ObjectProviderMultipartConfigElement 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;}}