SpringBoot初始教程之Servlet、Filter、Listener配置詳解
1.介紹
通過之前的文章來看,SpringBoot涵蓋了很多配置,但是往往一些配置是采用原生的Servlet進(jìn)行的,但是在SpringBoot中不需要配置web.xml的
因為有可能打包之后是一個jar包的形式,這種情況下如何解決?SpringBoot 提供了兩種方案進(jìn)行解決
2.快速開始
2.1 方案一
方案一采用原生Servlet3.0的注解進(jìn)行配置、@WebServlet 、@WebListener、@WebFilter是Servlet3.0 api中提供的注解
通過注解可以完全代替web.xml中的配置,下面是一個簡單的配置
IndexServlet
@WebServlet(name = "IndexServlet",urlPatterns = "/hello") public class IndexServlet extends HttpServlet { @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().print("hello word"); resp.getWriter().flush(); resp.getWriter().close(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }
IndexListener
@WebListener public class IndexListener implements ServletContextListener { private Log log = LogFactory.getLog(IndexListener.class); @Override public void contextInitialized(ServletContextEvent servletContextEvent) { log.info("IndexListener contextInitialized"); } @Override public void contextDestroyed(ServletContextEvent servletContextEvent) { } }
IndexFilter
@WebFilter(urlPatterns = "/*", filterName = "indexFilter") public class IndexFilter implements Filter { Log log = LogFactory.getLog(IndexFilter.class); @Override public void init(FilterConfig filterConfig) throws ServletException { log.info("init IndexFilter"); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { log.info("doFilter IndexFilter"); filterChain.doFilter(servletRequest,servletResponse); } @Override public void destroy() { } }
上面配置完了,需要配置一個核心的注解@ServletComponentScan,具體配置項如下,可以配置掃描的路徑
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Import(ServletComponentScanRegistrar.class) public @interface ServletComponentScan { @AliasFor("basePackages") String[] value() default {}; @AliasFor("value") String[] basePackages() default {}; Class<?>[] basePackageClasses() default {}; }
把注解加到入口處啟動即可
@SpringBootApplication @ServletComponentScan public class AppApplication { public static void main(String[] args) throws Exception { SpringApplication.run(AppApplication.class, args); } }
2.2 方案二
方案二是采用自己SpringBoot 配置bean的方式進(jìn)行配置的,SpringBoot提供了三種BeanFilterRegistrationBean、ServletRegistrationBean、ServletListenerRegistrationBean 分別對應(yīng)配置原生的Filter、Servlet、Listener,下面提供的三個配置和方案一采用的方式能夠達(dá)到統(tǒng)一的效果
@Bean public ServletRegistrationBean indexServletRegistration() { ServletRegistrationBean registration = new ServletRegistrationBean(new IndexServlet()); registration.addUrlMappings("/hello"); return registration; } @Bean public FilterRegistrationBean indexFilterRegistration() { FilterRegistrationBean registration = new FilterRegistrationBean(new IndexFilter()); registration.addUrlPatterns("/"); return registration; } @Bean public ServletListenerRegistrationBean servletListenerRegistrationBean(){ ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean(); servletListenerRegistrationBean.setListener(new IndexListener()); return servletListenerRegistrationBean; }
3.總結(jié)
兩種方案在使用上有差別,但是在內(nèi)部SpringBoot的實現(xiàn)上是無差別的,即使使用的是Servlet3.0注解,也是通過掃描注解
轉(zhuǎn)換成這三種bean的FilterRegistrationBean、ServletRegistrationBean、ServletListenerRegistrationBean
4.擴(kuò)展
大家在使用的時候有沒有發(fā)覺,其實SpringBoot在使用SpringMvc的時候不需要配置DispatcherServlet的,因為已經(jīng)自動配置了, 但是如果想要加一些初始配置參數(shù)如何解決,方案如下:
@Bean public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) { ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet); registration.addUrlMappings("*.do"); registration.addUrlMappings("*.json"); return registration; }
可以通過注入DispatcherServlet 然后用ServletRegistrationBean包裹一層 動態(tài)的加上一些初始參數(shù)
本文代碼:springboot-Servlet-Filter-Listener_jb51.rar
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Netty分布式NioSocketChannel注冊到selector方法解析
這篇文章主要為大家介紹了Netty分布式源碼分析NioSocketChannel注冊到selector方法的解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03Spring Boot Actuator未授權(quán)訪問漏洞的問題解決
Spring Boot Actuator 端點的未授權(quán)訪問漏洞是一個安全性問題,可能會導(dǎo)致未經(jīng)授權(quán)的用戶訪問敏感的應(yīng)用程序信息,本文就來介紹一下解決方法,感興趣的可以了解一下2023-09-09詳解mybatis-plus配置找不到Mapper接口路徑的坑
這篇文章主要介紹了詳解mybatis-plus配置找不到Mapper接口路徑的坑,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10SpringBoot使用AOP實現(xiàn)日志記錄功能詳解
這篇文章主要為大家介紹了SpringBoot使用AOP實現(xiàn)日志記錄功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07Java使用設(shè)計模式中迭代器模式構(gòu)建項目的代碼結(jié)構(gòu)示例
這篇文章主要介紹了Java使用設(shè)計模式中迭代器模式構(gòu)建項目的代碼結(jié)構(gòu)示例,迭代器模式能夠?qū)υL問者隱藏對象的內(nèi)部細(xì)節(jié),需要的朋友可以參考下2016-05-05