最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot初始教程之Servlet、Filter、Listener配置詳解

 更新時間:2017年09月05日 14:49:55   作者:尊少  
本篇文章主要介紹了SpringBoot初始教程之Servlet、Filter、Listener配置詳解,具有一定的參考價值,有興趣的可以了解一下

1.介紹

通過之前的文章來看,SpringBoot涵蓋了很多配置,但是往往一些配置是采用原生的Servlet進行的,但是在SpringBoot中不需要配置web.xml的

因為有可能打包之后是一個jar包的形式,這種情況下如何解決?SpringBoot 提供了兩種方案進行解決

2.快速開始

2.1 方案一

方案一采用原生Servlet3.0的注解進行配置、@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的方式進行配置的,SpringBoot提供了三種BeanFilterRegistrationBean、ServletRegistrationBean、ServletListenerRegistrationBean 分別對應配置原生的Filter、Servlet、Listener,下面提供的三個配置和方案一采用的方式能夠達到統(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.總結

兩種方案在使用上有差別,但是在內部SpringBoot的實現(xiàn)上是無差別的,即使使用的是Servlet3.0注解,也是通過掃描注解
轉換成這三種bean的FilterRegistrationBean、ServletRegistrationBean、ServletListenerRegistrationBean

4.擴展

大家在使用的時候有沒有發(fā)覺,其實SpringBoot在使用SpringMvc的時候不需要配置DispatcherServlet的,因為已經自動配置了, 但是如果想要加一些初始配置參數(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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論

卢氏县| 南郑县| 金门县| 高州市| 汾阳市| 阳谷县| 甘洛县| 巴林左旗| 普安县| 利津县| 泸州市| 丰台区| 雅安市| 谢通门县| 老河口市| 乌苏市| 湘西| 临漳县| 密山市| 谢通门县| 寻甸| 徐州市| 剑川县| 兴安盟| 新巴尔虎左旗| 大方县| 威远县| 仪陇县| 舞钢市| 滨州市| 盱眙县| 门头沟区| 古交市| 礼泉县| 河曲县| 镇江市| 沙雅县| 广安市| 连平县| 桓台县| 隆回县|