SpringBoot2.0
-
核心特性
-
组件自动装配: Web MVC , Web Flux , JDBC 等
激活: @EnableAutoConfiguration
配置: /META_INF/spring.factories
实现: XXXAutoConfiguration
-
嵌入式的Web容器: Tomcat , Jetty以及Undertow
Web Servlet: Tomcat , Jetty 和 Undertow
Web Reactive: Netty Web Server(基于web flux)
-
生产准备特性:指标 , 健康检查 , 外部化配置等
指标: /actuator/metrics
健康配置: /actuator/health
外部化配置: /actuator/configprops
-
-
Web应用
-
传统的Servlet应用
-
引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
-
使用Servlet组件
package cn.lisongyu.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication @ServletComponentScan(basePackages = "cn.lisongyu.demo") //使用java原生操作需添加@ServletComponentScan注解来实现对应的@WebServlet,@WebFilter,@WebListener public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
-
Servlet
-
实现
在类上添加@WebServlet注解
继承HttpServlet,重写doGet,doPost方法
注册到mapping中
-
URL映射
给@WebServlet(urlPatterns = "/my/servlet")添加属性urlPatterns,指明映射的路径
-
注册到应用中
在应用的启动主类上添加@ServletComponentScan(basePackages = "cn.lisongyu.demo")
- 使用java原生添加Servlet
package cn.lisongyu.demo; 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 lisongyu * @ClassName cn.lisongyu.demo.MyServlet * @description * @create 2018年11月26日 14:57 */ @WebServlet(urlPatterns = "/my/servlet") public class MyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("这里是servlet"); resp.getWriter().append("hello,world!"); } }
- 使用spring注解添加Servlet
package cn.lisongyu.demo; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * @author lisongyu * @ClassName cn.lisongyu.demo.MyServlet2 * @description * @create 2018年11月26日 17:05 */ @Configuration public class MyServlet2 { @Bean public ServletRegistrationBean getMyServlet(){ ServletRegistrationBean servlet = new ServletRegistrationBean(); servlet.setServlet(new HttpServlet() { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("这里是servlet"); resp.getWriter().append("hello,world!"); } }); return servlet; } }
-
-
Filter
- 通过实现Filter(javax.servlet.*),重写其init(),doFilter(),destroy()方法,主要重写其doFilter()方法,在类上添加@Component注解,将组件添加到spring容器中
package cn.lisongyu.demo; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * @author lisongyu * @ClassName cn.lisongyu.demo.MyFilter * @description * @create 2018年11月26日 15:14 */ @WebFilter(urlPatterns = "/my/*",filterName = "myfilter") //是java原生提供,所以在项目容器启动主类上要添加@ServletComponentScan() public class MyFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("这是我的拦截器------"); HttpServletRequest request = (HttpServletRequest)servletRequest; HttpServletResponse response = (HttpServletResponse)servletResponse; String servlet = request.getServletPath(); if (!servlet.endsWith("servlet")){ response.sendRedirect("/error"); return; } // 有值,就继续执行下一个过滤链 filterChain.doFilter(request, response); } @Override public void destroy() { } }
- 通过注解@Configuration和@Bean实现启动服务时候加载Bean
package cn.lisongyu.demo; /** * @author lisongyu * @ClassName cn.lisongyu.demo.MyFilter2 * @description * @create 2018年11月26日 15:56 */ import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @Configuration //装配配置到spring容器中 public class MyFilter2 { @Bean public FilterRegistrationBean getMyFilter(){ FilterRegistrationBean<Filter> filter = new FilterRegistrationBean<>(); filter.setFilter(new Filter() { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("这是我的拦截器------"); HttpServletRequest request = (HttpServletRequest)servletRequest; HttpServletResponse response = (HttpServletResponse)servletResponse; String servlet = request.getServletPath(); if (!servlet.endsWith("servlet")){ response.sendRedirect("/error"); return; } // 有值,就继续执行下一个过滤链 filterChain.doFilter(request, response); } }); //过滤规则 filter.addUrlPatterns("/my/*"); //过滤器名称 filter.setName("myfilter2"); //过滤器顺序 filter.setOrder(1); return filter; } }
-
Listener
-
监听器类型很多,本篇只做一个举例(HttpSessionListener)
- java原生@WebListener
package cn.lisongyu.demo; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; /** * @author lisongyu * @ClassName cn.lisongyu.demo.MyListener * @description * @create 2018年11月26日 16:17 */ @WebListener public class MyListener implements HttpSessionListener { public int count = 0; @Override public void sessionCreated(HttpSessionEvent se) { System.out.println("这里是listener.创建"); count ++; se.getSession().getServletContext().setAttribute("count",count); } @Override public void sessionDestroyed(HttpSessionEvent se) { System.out.println("这里是listener.关闭"); count --; se.getSession().getServletContext().setAttribute("count",count); } }
- springboot2.0集成,通过@Configuration和@Bean注解实现
package cn.lisongyu.demo; import org.springframework.boot.web.servlet.ServletListenerRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; /** * @author lisongyu * @ClassName cn.lisongyu.demo.MyListener2 * @description * @create 2018年11月26日 16:50 */ @Configuration public class MyListener2 { @Bean public ServletListenerRegistrationBean getMyListener(){ ServletListenerRegistrationBean listener = new ServletListenerRegistrationBean(); listener.setListener(new HttpSessionListener() { int count; @Override public void sessionCreated(HttpSessionEvent se) { System.out.println("这里是listener.创建"); count ++; se.getSession().getServletContext().setAttribute("count",count); } @Override public void sessionDestroyed(HttpSessionEvent se) { System.out.println("这里是listener.关闭"); count --; se.getSession().getServletContext().setAttribute("count",count); } }); return listener; } }
-
在Servlet组件当中,当前端发送请求时,后端的执行顺序是Filter->Servlet->Listener
-
-
-