最新国产好看的视频,伊人天堂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.總結

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

4.擴展

大家在使用的時候有沒有發(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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論

嘉禾县| 喀喇沁旗| 肇州县| 桐柏县| 石景山区| 永新县| 股票| 左云县| 隆化县| 凉山| 西藏| 万全县| 克山县| 阿拉善右旗| 玉田县| 葵青区| 萝北县| 沙雅县| 土默特左旗| 渑池县| 神农架林区| 嘉义县| 通海县| 永吉县| 张掖市| 雷波县| 水富县| 淄博市| 额济纳旗| 合作市| 辰溪县| 曲阜市| 余干县| 富阳市| 且末县| 蒙阴县| 台东县| 镇雄县| 康保县| 鹤壁市| 宜君县|