• 核心特性

    • 组件自动装配: 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")

            1. 使用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!");
            }
            }
            1. 使用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

          1. 通过实现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() { }
          }
          1. 通过注解@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)

            1. 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);
            }
            }
            1. 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

SpringBoot2.0初识的更多相关文章

  1. (六)SpringBoot2.0基础篇- Redis整合(JedisCluster集群连接)

    一.环境 Redis:4.0.9 SpringBoot:2.0.1 Redis安装:Linux(Redhat)安装Redis 二.SpringBoot整合Redis 1.项目基本搭建: 我们基于(五) ...

  2. Springboot2.0(Spring5.0)中个性化配置项目上的细节差异

    在一般的项目中,如果Spring Boot提供的Sping MVC不符合要求,则可以通过一个配置类(@Configuration)加上@EnableWebMvc注解来实现完全自己控制的MVC配置.但此 ...

  3. springboot2.0.3源码篇 - 自动配置的实现,发现也不是那么复杂

    前言 开心一刻 女儿: “妈妈,你这么漂亮,当年怎么嫁给了爸爸呢?” 妈妈: “当年你爸不是穷嘛!‘ 女儿: “穷你还嫁给他!” 妈妈: “那时候刚刚毕业参加工作,领导对我说,他是我的扶贫对象,我年轻 ...

  4. spring-boot-2.0.3源码篇 - pageHelper分页,绝对有值得你看的地方

    前言 开心一刻 说实话,作为一个宅男,每次被淘宝上的雄性店主追着喊亲,亲,亲,这感觉真是恶心透顶,好像被强吻一样.........更烦的是我每次为了省钱,还得用个女号,跟那些店主说:“哥哥包邮嘛么叽. ...

  5. SpringBoot2.0之四 简单整合MyBatis

    从最开始的SSH(Struts+Spring+Hibernate),到后来的SMM(SpringMVC+Spring+MyBatis),到目前的S(SpringBoot),随着框架的不断更新换代,也为 ...

  6. springBoot2.0+redis+fastJson+自定义注解实现方法上添加过期时间

    springBoot2.0集成redis实例 一.首先引入项目依赖的maven jar包,主要包括 spring-boot-starter-data-redis包,这个再springBoot2.0之前 ...

  7. SpringBoot2.0+Mybatis-Plus3.0+Druid1.1.10 一站式整合

    SpringBoot2.0+Mybatis-Plus3.0+Druid1.1.10 一站式整合 一.先快速创建一个springboot项目,其中pom.xml加入mybatis-plus 和druid ...

  8. springboot2.0 JPA配置自定义repository,并作为基类BaseRepository使用

    springboot2.0 JPA配置自定义repository,并作为基类BaseRepository使用 原文链接:https://www.cnblogs.com/blog5277/p/10661 ...

  9. springboot2.0配置连接池(hikari、druid)

    springboot2.0配置连接池(hikari.druid) 原文链接:https://www.cnblogs.com/blog5277/p/10660689.html 原文作者:博客园--曲高终 ...

随机推荐

  1. 从零学习Fluter(三):Flutter的路由跳转以及state的生命周期

    今天继续研究Flutter,我是在flutter1.0发布后,才玩flutter的,发现在此之前,许多人已经先发制人,玩起了flutter,不知不觉中,我已经被别人摔在了起跑线上,玩过flutter后 ...

  2. IDEA工具教程

    刚从myeclipse工具转成IntelliJ IDEA工具,在“传智播客*黑马程序员”学习了相关操作和配置,因此整理在该文章中.   文章大纲 教程文档下载地址 链接:https://pan.bai ...

  3. matlab练习程序(水波特效)

    还记得原来写过一个对图像进行波纹扭曲操作的博文. 这次实现的是水波特效,其实就是通过正余弦函数表示波纹中心位置慢慢向外扩散,通过叠加衰减因子使振幅不断减小,进而产生水波的效果. 效果如下: 原图: 波 ...

  4. Oracle获取表字段名,字段类型,字段长度,注释

    SELECT b.comments as 注释, a.column_name as 列名, a.data_type || '(' || a.data_length || ')' as 数据类型, a. ...

  5. deepin 15.8桌面系统

    深度桌面环境是深度科技自主开发的美观易用.极简操作的桌面环境,主要由桌面.启动器.任务栏.控制中心.窗口管理器等组成,系统中预装了 WPS Office.搜狗输入法.有道词典.网易云音乐以及深度特色应 ...

  6. docker容器日志收集方案汇总评价总结

    docker日志收集方案有太多,下面截图罗列docker官方给的日志收集方案(详细请转docker官方文档).很多方案都不适合我们下面的系列文章没有说. 经过以下5篇博客的叙述简单说下docker容器 ...

  7. 我的第一个python web开发框架(35)——权限数据库结构设计

    接下来要做的是权限系统的数据库结构设计,在上一章我们了解了权限系统是通过什么来管理好权限的,我们选用其中比较常用的权限系统来实现当前项目管理要求. 下面是我们选择的权限系统关系模型: 从以上关系可以看 ...

  8. 云数据库PolarDB(一)

    一.出现的背景及PolarDB简介 阿里云,中国第一家拥有完整云计算能力的企业. 2015年,在计算界的奥运会Sort Benchmark中,阿里云计算100TB数据排序只用了不到7分钟,把Apach ...

  9. Storm入门(三)HelloWorld示例

    一.配置开发环境 storm有两种操作模式: 本地模式和远程模式.使用本地模式的时候,你可以在你的本地机器上开发测试你的topology, 一切都在你的本地机器上模拟出来; 用远程模式的时候你提交的t ...

  10. MySQL之表相关操作

    一 存储引擎介绍 存储引擎即表类型,mysql根据不同的表类型会有不同的处理机制 详见:http://www.cnblogs.com/linhaifeng/articles/7213670.html ...