Web 开发使用 Controller 基本上可以完成大部分需求,但是我们还可能会用到 Servlet、 Filter、Listener 等等
更多干货
- spring-boot系列一 之restfull api与多环境配置
- springboot系列二之 日志
- SpringBoot系列三之 MVC 模版引擎
- SpringBoot 2.0.0.M7 系列四 异常处理
- springboot 2.0.0.M7之 注解 与 配置
- springboot 2.0.0.M7 配置mvc
- springboot 2.0.0.M7 之 Servlet Listener Filter
- springboot 2.0.0.M7 之 跨域
- springboot 2.0.0.M7 之使用mysql
- spring boot 2.0.0.M7 之 数据库-事务处理
- springboot 2.0.0.M7 之 h2 嵌入式数据库的使用
- springboot 2.0.0.M7 之 数据库-redis
- Spring Boot 2.0.0.M7 中使用Swagger2构建RESTful API文档
- Spring Boot 2.0.0.M7 springBoot-mongodb使用
- Spring Boot 2.0.0.M7 使用 Caching-EhCache
- Spring Boot spring boot 使用 Caching-Redis
- Spring Boot 2.0.0.M7 使用异步消息服务-AMQP(RabbitMQ)
- Spring Boot 2.0.0.M7 调用REST服务-如何使用代理
- Spring Boot 2.0.0.M7 发送邮件-使用模板邮件并实现多账号轮询发送
- Spring Boot 2.0.0.M7 使用Spring Session实现集群-redis
- Spring Boot 2.0.0.M7 如何进行远程调试
- Spring Boot 生产准备-基于HTTP的监控
- Spring Boot 集成 Druid
- springboot思维导图
第一种方法
- 方法一:通过注册 ServletRegistrationBean、 FilterRegistrationBean 和 ServletListenerRegistrationBean 获得控制
@SpringBootApplication
public class SpringBootEduApplication {
@Bean
public ServletRegistrationBean servletRegistrationBean() {
return new ServletRegistrationBean(new CustomServlet(), "/api/blog");
}
@Bean
public FilterRegistrationBean filterRegistrationBean() {
return new FilterRegistrationBean(new CustomFilter(), servletRegistrationBean());
}
@Bean
public ServletListenerRegistrationBean<CustomListener> servletListenerRegistrationBean() {
return new ServletListenerRegistrationBean<CustomListener>(new CustomListener());
}
public static void main(String[] args) {
SpringApplication.run(SpringBootEduApplication.class, args);
}
}
servlet
public class CustomServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("servlet get method");
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("servlet post method");
response.getWriter().write("hello world");
}
}
filter
public class CustomFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("init filter");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("do filter");
chain.doFilter(request, response);
}
@Override
public void destroy() {
System.out.println("destroy filter");
}
}
listener
public class CustomListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("contextInitialized");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("contextDestroyed");
}
}
第二种方法
通过实现 ServletContextInitializer 接口直接注册
@SpringBootApplication
public class SpringBootEduApplication implements ServletContextInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
servletContext.addServlet("customServlet", new CustomServlet()).addMapping("/api/blog");
servletContext.addFilter("customFilter", new CustomFilter())
.addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST), true, "customServlet");
servletContext.addListener(new CustomListener());
}
public static void main(String[] args) {
SpringApplication.run(SpringBootEduApplication.class, args);
}
}
方法三
在 SpringBootApplication 上使用@ServletComponentScan 注解后,直接通过@WebServlet、@WebFilter、@WebListener 注解自动注册
@ServletComponentScan
@SpringBootApplication
public class SpringBootDemo103Application {
public static void main(String[] args) {
SpringApplication.run(SpringBootDemo103Application.class, args);
}
}
@WebFilter(filterName = "customFilter", urlPatterns = "/*")
public class CustomFilter implements Filter {
}
@WebListener
public class CustomListener implements ServletContextListener {
}
@WebServlet(name = "customServlet", urlPatterns = "/roncoo")
public class CustomServlet extends HttpServlet {
}