此文已由作者易国强授权网易云社区发布。
欢迎访问网易云社区,了解更多网易技术产品运营经验。
传统的filter及listener配置
- 在传统的Java web项目中,servlet、filter和listener的配置很简单,直接在web.xml中按顺序配置好即可,程序启动时,就会按照你配置的顺序依次加载(当然,web.xml中各配置信息总的加载顺序是context-param -> listener -> filter -> servlet),项目搭建完成后,估计一般新来的开发同学没啥事都不会去关注里面都做了些啥~ = =
- 废话少说,上代码,下面是一个传统的web.xml配置示例。
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- define charset --><filter> <filter-name>Set UTF-8</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param></filter><servlet> <servlet-name>silver</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:xxx-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup></servlet><servlet-mapping> <servlet-name>silver</servlet-name> <url-pattern>/</url-pattern></servlet-mapping>
Spring Boot中该如何配置?
- 需要说明的是,Spring Boot项目里面是没有web.xml的,那么listener和filter我们该如何配置呢?
- 在Spring Boot项目中有两种方式进行配置,一种是通过Servlet3.0+提供的注解进行配置,分别为@WebServlet、@WebListener、@WebFilter。示例如下:
import javax.servlet.*;import javax.servlet.annotation.WebFilter;import java.io.IOException;/** * @author bjyiguoqiang * @date 2017/11/9 17:28. */@WebFilter(urlPatterns = "/*", filterName = "helloFilter")public class HelloFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("init helloFilter"); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("doFilter helloFilter"); filterChain.doFilter(servletRequest,servletResponse); } @Override public void destroy() { } }
import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;/** * @author bjyiguoqiang * @date 2017/11/9 17:27. */@WebServlet(name = "hello",urlPatterns = "/hello")public class HelloServlet 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); } }
import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import javax.servlet.annotation.WebListener;/** * @author bjyiguoqiang * @date 2017/11/9 17:28. */@WebListenerpublic class HelloListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent servletContextEvent) { System.out.println("HelloListener contextInitialized"); } @Override public void contextDestroyed(ServletContextEvent servletContextEvent) { } }
- 最后在程序入口类加入扫描注解@ServletComponentScan即可生效。
@[email protected] class DemoaApplication { public static void main(String[] args) { SpringApplication.run(DemoaApplication.class, args); } }
- 需要注意的是,使用@WebFilter 是无法实现filter的过滤顺序的,使用org.springframework.core.annotation包中的@Order注解在spring boot环境中也是不支持的。所有如果需要对自定义的filter排序,那么可以采用下面所述的方式。
- 另一种配置则是通过Spring Boot提供的三种类似Bean实现的,分别为ServletRegistrationBean、ServletListenerRegistrationBean以及FilterRegistrationBean。使用示例分别如下:
import org.springframework.boot.web.servlet.FilterRegistrationBean;import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;import org.springframework.boot.web.servlet.ServletRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/** * @author bjyiguoqiang * @date 2017/11/9 17:44. */@Configurationpublic class MyConfigs { /** * 配置hello过滤器 */ @Bean public FilterRegistrationBean sessionFilterRegistration() { FilterRegistrationBean registrationBean = new FilterRegistrationBean(); registrationBean.setFilter(new HelloFilter()); registrationBean.addUrlPatterns("/*"); registrationBean.addInitParameter("session_filter_key", "session_filter_value"); registrationBean.setName("helloFilter"); //设置加载顺序,数字越小,顺序越靠前,建议最小从0开始配置,最大为Integer.MAX_VALUE registrationBean.setOrder(10); return registrationBean; } /** * 配置hello Servlet */ @Bean public ServletRegistrationBean helloServletRegistration() { ServletRegistrationBean registration = new ServletRegistrationBean(new HelloServlet()); registration.addUrlMappings("/hello"); //设置加载顺序,数字越小,顺序越靠前,建议最小从0开始配置,最大为Integer.MAX_VALUE registration.setOrder(3); return registration; } /** * 配置hello Listner */ @Bean public ServletListenerRegistrationBean helloListenerRegistrationBean(){ ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean(); servletListenerRegistrationBean.setListener(new HelloListener()); //设置加载顺序,数字越小,顺序越靠前,建议最小从0开始配置,最大为Integer.MAX_VALUE servletListenerRegistrationBean.setOrder(3); return servletListenerRegistrationBean; } }
- 通过Bean注入的方式进行配置,可以自定义加载顺序,这点很nice。
最后
- 在Spring Boot的环境中配置servlet、filter和listener虽然没有在web.xml中配置那么直观,不过也是挺简单的。
- 不足之处,欢迎指正,谢谢~
更多网易技术、产品、运营经验分享请点击。
相关文章:
【推荐】 网站规划通识:原型图绘制的一些注意事项
原文地址:https://www.cnblogs.com/zyfd/p/9887284.html
时间: 2024-10-07 13:14:49