SpringBoot

应用场景

  • 传统Web MVC架构
  • 前后端分离架构
  • 微服务架构

特性

  • 可创建独立的Spring应用程序
  • 提供嵌入式Web Server(无需部署war包)
  • 无需任何代码生成技术也无任何XML配置
  • 自动化配置
  • 提供一系列生产级特性
    • 端点(endpoints.enable=true开启端点)
      • autoconfig获取自动配置信息
      • beans获取Spring Bean基本信息
      • configprops获取配置项信息
      • dump获取当前线程基本信息
      • env获取环境变量信息
      • health获取健康检查信息
        • enpoints.health.enable = true 开启健康检查
        • enpoints.health.sensitive = true 获取敏感性,默认false,如果为true,则不返回敏感字段
        • enpoints.health.time-to-live = 500 设置健康检查的缓存时间,单位ms,默认1000
        • 自定义健康检查器
          • 实现org.springframework.boot.actuate.health.HealthIndicator接口
      • info获取应用基本信息
        • enpoints.info.enable = true 开启应用信息
        • info.app.name = @project.name@ 获取pom.xml中的name
        • info.app.description = @project.description@ 获取pom.xml中的description
        • info.app.version = @project.version@ 获取pom.xml中的version
        • 可通过git-commit-id-plugin插件获取Git信息
          • info.git.branch = @git.branch@ 获取Git版本信息
          • info.git.commit.id = @git.commit.id@ 获取Git提交ID
          • info.git.commit.time = @git.commit.time@ 获取Git提交时间
      • metrics获取性能指标信息
        • enpoints.metrics.enable = true 开启性能指标
        • enpoints.metrics.id = performance 自定义ID
        • enpoints.metrics.path = /enpoints/metrices 自定义请求路径
      • mappings获取请求映射信息
      • trace获取请求调用信息
    • 跨域(Spring4.2之后支持)
      • 方法一
        • endpoints.cors.allowed-origins = http://www.baidu.com 配置跨域域名
        • endpoints.cors.allowed-methods = GET,POST,PUT,DELETE 配置跨域支持方法
      • 方法二
        • @CrossOrigin注解
      • 方法三
        • 继承org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter的addCorsMappings方法
    • 外部化配置,按顺序如下
      • Java命令行参数
      • JNDI属性
      • Java系统属性
      • 操作系统环境变量
      • Jar包外的application.properties配置
      • Jar包内的application.properties配置
      • @PropertySource注解
      • SpringApplication.setDefaultProperties默认值
    • 远程监控
      • ssh方式
        • 引入spring-boot-starter-remote-shell
        • 配置
          • shell.ssh.port = 2000
          • shell.auth.simple.user.name = admin
          • shell.auth.simple.user.password = 123456
        • ssh -p 2000 admin@127.0.0.1
      • telnet方式
        • 引入crsh.shell.telnet
  • 提供开箱即用的Spring插件

@EnableWebMvc`

@EnableAsync

@EnableAspectJAutoProxy

  • 处理类 ConfigurationClassParser

  • 扫描类 ClassPathBeanDefinitionScanner

    • ClassPathScanningCandidateComponentProvider
  • 注解驱动上下文 AnnotationConfigApplicationContext

Spring 事件

  • ContextRefreshedEvent
    • ApplicationContextEvent
    • ApplicationEvent
  • ContextClosedEvent

自定义事件

  • PayloadApplicationEvent

Spring 事件的类型 ApplicationEvent

Spring 事件监听器 ApplicationListener

Spring 事件广播器 ApplicationEventMulticaster

  • 实现类:SimpleApplicationEventMulticaster

Spring事件执行顺序

  1. ApplicationStartingEvent(1)
  2. ApplicationEnvironmentPreparedEvent(2)
  3. ApplicationPreparedEvent(3)
  4. ContextRefreshedEvent
  5. ServletWebServerInitializedEvent
  6. ApplicationStartedEvent(4)
  7. ApplicationReadyEvent(5)
  8. ContextClosedEvent
  9. ApplicationFailedEvent (特殊情况)(6)

优化

  • 移除JspServlet(Jsp页面编译)
  • 移除DefaultServlet(静态资源解析)