當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Boot怎么样注册Servlet三大组件[Servlet、Filter、Listener]
生活随笔
收集整理的這篇文章主要介紹了
Spring Boot怎么样注册Servlet三大组件[Servlet、Filter、Listener]
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
由于SpringBoot默認是以jar包的方式啟動嵌入式的Servlet容器來啟動SpringBoot的web應用,沒有web.xml文件。
注冊三大組件用以下方式
一、ServletRegistrationBean
//注冊三大組件 @Bean public ServletRegistrationBean myServlet(){ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet");return registrationBean; }二、FilterRegistrationBean
@Bean public FilterRegistrationBean myFilter(){FilterRegistrationBean registrationBean = new FilterRegistrationBean();registrationBean.setFilter(new MyFilter());registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));return registrationBean; }三、ServletListenerRegistrationBean
@Bean public ServletListenerRegistrationBean myListener(){ServletListenerRegistrationBean<MyListener> registrationBean = new ServletListenerRegistrationBean<>(new MyListener());return registrationBean; }SpringBoot幫我們自動SpringMVC的時候,自動的注冊SpringMVC的前端控制器;DIspatcherServlet;
DispatcherServletAutoConfiguration中:
@Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME) @ConditionalOnBean(value = DispatcherServlet.class, name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME) public ServletRegistrationBean dispatcherServletRegistration(DispatcherServlet dispatcherServlet) {ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet, this.serverProperties.getServletMapping());//默認攔截: / 所有請求;包靜態資源,但是不攔截jsp請求; /*會攔截jsp//可以通過server.servletPath來修改SpringMVC前端控制器默認攔截的請求路徑registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);registration.setLoadOnStartup(this.webMvcProperties.getServlet().getLoadOnStartup());if (this.multipartConfig != null) {registration.setMultipartConfig(this.multipartConfig);}return registration; }學習內容來自尚硅谷
總結
以上是生活随笔為你收集整理的Spring Boot怎么样注册Servlet三大组件[Servlet、Filter、Listener]的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Boot配置嵌入式Serv
- 下一篇: Spring Boot切换其他嵌入式的S