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的實現上是無差別的,即使使用的是Servlet3.0注解,也是通過掃描注解
轉換成這三種bean的FilterRegistrationBean、ServletRegistrationBean、ServletListenerRegistrationBean
4.擴展
大家在使用的時候有沒有發(fā)覺,其實SpringBoot在使用SpringMvc的時候不需要配置DispatcherServlet的,因為已經自動配置了, 但是如果想要加一些初始配置參數如何解決,方案如下:
@Bean
public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);
registration.addUrlMappings("*.do");
registration.addUrlMappings("*.json");
return registration;
}
可以通過注入DispatcherServlet 然后用ServletRegistrationBean包裹一層 動態(tài)的加上一些初始參數
本文代碼:springboot-Servlet-Filter-Listener_jb51.rar
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
IDEA創(chuàng)建Servlet并配置web.xml的實現
這篇文章主要介紹了IDEA創(chuàng)建Servlet并配置web.xml的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-10-10
IDEA?2020.3最新永久激活碼(免費激活到?2099?年,親測有效)
分享一下?IntelliJ?IDEA?2020.3.1?最新激活注冊碼,破解教程如下,可免費激活至?2099?年,親測有效,本文給大家分享兩種方法,感興趣的朋友參考下吧2021-01-01
Java JDK動態(tài)代理在攔截器和聲明式接口中的應用小結
Java動態(tài)代理技術通過反射機制在運行時動態(tài)生成代理類,實現對目標對象方法的攔截和增強,本文給大家介紹Java JDK動態(tài)代理在攔截器和聲明式接口中的應用小結,感興趣的朋友跟隨小編一起看看吧2025-01-01
SpringBoot通過redisTemplate調用lua腳本并打印調試信息到redis log(方法步驟詳解)
這篇文章主要介紹了SpringBoot通過redisTemplate調用lua腳本 并打印調試信息到redis log,本文分步驟給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02
SpringBoot監(jiān)控Tomcat活動線程數來判斷是否完成請求處理方式
這篇文章主要介紹了SpringBoot監(jiān)控Tomcat活動線程數來判斷是否完成請求處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02

