• 核心特性

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

      • 引入依赖

        1. <dependency>    
        2. <groupId>org.springframework.boot</groupId>    
        3. <artifactId>spring-boot-starter-web</artifactId>
        4. </dependency>
      • 使用Servlet组件

        1. package cn.lisongyu.demo;
        2. import org.springframework.boot.SpringApplication;
        3. import org.springframework.boot.autoconfigure.SpringBootApplication;
        4. import org.springframework.boot.web.servlet.ServletComponentScan;
        5. @SpringBootApplication
        6. @ServletComponentScan(basePackages = "cn.lisongyu.demo") //使用java原生操作需添加@ServletComponentScan注解来实现对应的@WebServlet,@WebFilter,@WebListener
        7. public class DemoApplication {
        8. public static void main(String[] args) {
        9. SpringApplication.run(DemoApplication.class, args);
        10. }
        11. }
        • Servlet

          • 实现

            在类上添加@WebServlet注解

            继承HttpServlet,重写doGet,doPost方法

            注册到mapping中

          • URL映射

            给@WebServlet(urlPatterns = "/my/servlet")添加属性urlPatterns,指明映射的路径

          • 注册到应用中

            在应用的启动主类上添加@ServletComponentScan(basePackages = "cn.lisongyu.demo")

            1. 使用java原生添加Servlet
            1. package cn.lisongyu.demo;
            2. import javax.servlet.ServletException;
            3. import javax.servlet.annotation.WebServlet;
            4. import javax.servlet.http.HttpServlet;
            5. import javax.servlet.http.HttpServletRequest;
            6. import javax.servlet.http.HttpServletResponse;
            7. import java.io.IOException;
            8. /**
            9. * @author lisongyu
            10. * @ClassName cn.lisongyu.demo.MyServlet
            11. * @description
            12. * @create 2018年11月26日 14:57
            13. */
            14. @WebServlet(urlPatterns = "/my/servlet")
            15. public class MyServlet extends HttpServlet {
            16. @Override
            17. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            18. System.out.println("这里是servlet");
            19. resp.getWriter().append("hello,world!");
            20. }
            21. }
            1. 使用spring注解添加Servlet
            1. package cn.lisongyu.demo;
            2. import org.springframework.boot.web.servlet.ServletRegistrationBean;
            3. import org.springframework.context.annotation.Bean;
            4. import org.springframework.context.annotation.Configuration;
            5. import javax.servlet.ServletException;
            6. import javax.servlet.http.HttpServlet;
            7. import javax.servlet.http.HttpServletRequest;
            8. import javax.servlet.http.HttpServletResponse;
            9. import java.io.IOException;
            10. /**
            11. * @author lisongyu
            12. * @ClassName cn.lisongyu.demo.MyServlet2
            13. * @description
            14. * @create 2018年11月26日 17:05
            15. */
            16. @Configuration
            17. public class MyServlet2 {
            18. @Bean
            19. public ServletRegistrationBean getMyServlet(){
            20. ServletRegistrationBean servlet = new ServletRegistrationBean();
            21. servlet.setServlet(new HttpServlet() {
            22. @Override
            23. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            24. System.out.println("这里是servlet");
            25. resp.getWriter().append("hello,world!");
            26. }
            27. });
            28. return servlet;
            29. }
            30. }
        • Filter

          1. 通过实现Filter(javax.servlet.*),重写其init(),doFilter(),destroy()方法,主要重写其doFilter()方法,在类上添加@Component注解,将组件添加到spring容器中
          1. package cn.lisongyu.demo;
          2. import javax.servlet.*;
          3. import javax.servlet.annotation.WebFilter;
          4. import javax.servlet.http.HttpServletRequest;
          5. import javax.servlet.http.HttpServletResponse;
          6. import java.io.IOException;
          7. /**
          8. * @author lisongyu
          9. * @ClassName cn.lisongyu.demo.MyFilter
          10. * @description
          11. * @create 2018年11月26日 15:14
          12. */
          13. @WebFilter(urlPatterns = "/my/*",filterName = "myfilter") //是java原生提供,所以在项目容器启动主类上要添加@ServletComponentScan()
          14. public class MyFilter implements Filter {
          15. @Override
          16. public void init(FilterConfig filterConfig) throws ServletException {
          17. }
          18. @Override
          19. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
          20. System.out.println("这是我的拦截器------");
          21. HttpServletRequest request = (HttpServletRequest)servletRequest;
          22. HttpServletResponse response = (HttpServletResponse)servletResponse;
          23. String servlet = request.getServletPath();
          24. if (!servlet.endsWith("servlet")){
          25. response.sendRedirect("/error");
          26. return;
          27. }
          28. // 有值,就继续执行下一个过滤链
          29. filterChain.doFilter(request, response);
          30. }
          31. @Override
          32. public void destroy() {
          33. }
          34. }
          1. 通过注解@Configuration和@Bean实现启动服务时候加载Bean
          1. package cn.lisongyu.demo;
          2. /**
          3. * @author lisongyu
          4. * @ClassName cn.lisongyu.demo.MyFilter2
          5. * @description
          6. * @create 2018年11月26日 15:56
          7. */
          8. import org.springframework.boot.web.servlet.FilterRegistrationBean;
          9. import org.springframework.context.annotation.Bean;
          10. import org.springframework.context.annotation.Configuration;
          11. import javax.servlet.*;
          12. import javax.servlet.http.HttpServletRequest;
          13. import javax.servlet.http.HttpServletResponse;
          14. import java.io.IOException;
          15. @Configuration //装配配置到spring容器中
          16. public class MyFilter2 {
          17. @Bean
          18. public FilterRegistrationBean getMyFilter(){
          19. FilterRegistrationBean<Filter> filter = new FilterRegistrationBean<>();
          20. filter.setFilter(new Filter() {
          21. @Override
          22. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
          23. System.out.println("这是我的拦截器------");
          24. HttpServletRequest request = (HttpServletRequest)servletRequest;
          25. HttpServletResponse response = (HttpServletResponse)servletResponse;
          26. String servlet = request.getServletPath();
          27. if (!servlet.endsWith("servlet")){
          28. response.sendRedirect("/error");
          29. return;
          30. }
          31. // 有值,就继续执行下一个过滤链
          32. filterChain.doFilter(request, response);
          33. }
          34. });
          35. //过滤规则
          36. filter.addUrlPatterns("/my/*");
          37. //过滤器名称
          38. filter.setName("myfilter2");
          39. //过滤器顺序
          40. filter.setOrder(1);
          41. return filter;
          42. }
          43. }
        • Listener

          • 监听器类型很多,本篇只做一个举例(HttpSessionListener)

            1. java原生@WebListener
            1. package cn.lisongyu.demo;
            2. import javax.servlet.annotation.WebListener;
            3. import javax.servlet.http.HttpSessionEvent;
            4. import javax.servlet.http.HttpSessionListener;
            5. /**
            6. * @author lisongyu
            7. * @ClassName cn.lisongyu.demo.MyListener
            8. * @description
            9. * @create 2018年11月26日 16:17
            10. */
            11. @WebListener
            12. public class MyListener implements HttpSessionListener {
            13. public int count = 0;
            14. @Override
            15. public void sessionCreated(HttpSessionEvent se) {
            16. System.out.println("这里是listener.创建");
            17. count ++;
            18. se.getSession().getServletContext().setAttribute("count",count);
            19. }
            20. @Override
            21. public void sessionDestroyed(HttpSessionEvent se) {
            22. System.out.println("这里是listener.关闭");
            23. count --;
            24. se.getSession().getServletContext().setAttribute("count",count);
            25. }
            26. }
            1. springboot2.0集成,通过@Configuration和@Bean注解实现
            1. package cn.lisongyu.demo;
            2. import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
            3. import org.springframework.context.annotation.Bean;
            4. import org.springframework.context.annotation.Configuration;
            5. import javax.servlet.http.HttpSessionEvent;
            6. import javax.servlet.http.HttpSessionListener;
            7. /**
            8. * @author lisongyu
            9. * @ClassName cn.lisongyu.demo.MyListener2
            10. * @description
            11. * @create 2018年11月26日 16:50
            12. */
            13. @Configuration
            14. public class MyListener2 {
            15. @Bean
            16. public ServletListenerRegistrationBean getMyListener(){
            17. ServletListenerRegistrationBean listener = new ServletListenerRegistrationBean();
            18. listener.setListener(new HttpSessionListener() {
            19. int count;
            20. @Override
            21. public void sessionCreated(HttpSessionEvent se) {
            22. System.out.println("这里是listener.创建");
            23. count ++;
            24. se.getSession().getServletContext().setAttribute("count",count);
            25. }
            26. @Override
            27. public void sessionDestroyed(HttpSessionEvent se) {
            28. System.out.println("这里是listener.关闭");
            29. count --;
            30. se.getSession().getServletContext().setAttribute("count",count);
            31. }
            32. });
            33. return listener;
            34. }
            35. }

        在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. .net mvc session失效问题

    最近解决基于.net mvc项目的session失效问题,这个跟大家聊聊. 1.问题分析 .net mvc中,Session失效需要考虑几种情况: 基于权限认证的Action,使用非Ajax请求: 基 ...

  2. Python常用模块:datetime

    使用前提: >>> from datetime import datetime 常见用法: 1.获取当前日期和时间 >>> now = datetime.now() ...

  3. nodejs+express+mongodb写api接口的简单尝试

    1:启动mongodb服务 我的mongoDB的安装目录:E:\mongoDB\bin,版本:3.4.9 打开cmd  -> e:(进入e盘) -> cd mongoDB/bin(进入mo ...

  4. vue父子组件之间传值

    vue父子组件进行传值 vue中的父子组件,什么是父组件什么是子组件呢?就跟html标签一样,谁包裹着谁谁就是父组件,被包裹的元素就是子组件. 父组件向子组件传值 下面用的script引入的方式,那种 ...

  5. 智能POS常见问题整理

    智能POS预警值为小于所设的数量,H5就会变为锁定状态 智能POS查看数据库方法: 商米D1:设置-存储设备和USB-内部存储设备-浏览-winboxcash tablet.db为智能POS数据库 W ...

  6. 【原】Java学习笔记015 - 面向对象

    package cn.temptation; public class Sample01 { public static void main(String[] args) { // 传递 值类型参数 ...

  7. 【原】Java学习笔记013 - 阶段测试

    package cn.temptation; import java.util.Scanner; public class Sample01 { public static void main(Str ...

  8. Django REST framework框架介绍和基本使用

    Django REST framework介绍 Django REST framework是基于Django实现的一个RESTful风格API框架,能够帮助我们快速开发RESTful风格的API. 官 ...

  9. 【Python使用】使用pip安装卸载Python包(含离线安装Python包)未完成???

    pip 是 Python 包管理工具,该工具提供了对Python包的查找.下载.安装.卸载的功能.Python 2.7.9 + 或 Python 3.4+ 以上版本都自带 pip 工具. pip使用( ...

  10. vue 应用生产环境的 webpack 打包配置优化

    转:https://blog.csdn.net/robin_star_/article/details/83856363 前言:很好的打包优化的帖子,还没来的急去实测验证 1. 去掉 console ...