SpringBoot (一) 入门、配置、自动配置源码剖析理解

0 Spring Boot

前言

  • 主要是作为个人笔记, 特别熟练知识的可能会无意识跳过或者淡化. 大多数是有用的文字.
  • 一些可以复用的章节, 例如各种配置的章节, 其实是可以在标题上加★来标识的.
  • 借鉴B站狂神老师和尚硅谷雷老师的课程, 源于官方文档

Document Directory

  • 是官网文档标题目录

水平暂时远做不到翻译文档, 就把官方文档的目录拿来学习一下英语

version 2.2.0

  1. Spring Boot Documentation
    1. About the Documentation
    2. Getting Help
    3. First Steps
    4. Wording with Spring Boot
    5. Learning about Spring Boot Features
    6. Moving to Production
    7. Advanced Topics
  2. Getting Started
    1. Introducing Spring Boot
    2. System Requirements
    3. Installing Spring Boot
    4. Developing Your First Spring Boot Application
    5. What to Read Next
  3. Using Spring Boot
    1. Build Systems
    2. Structuring Your Code
    3. Configuration Classes
    4. Auto-configuration
    5. Spring Beans and Dependendy Injection
    6. Using the @SpringBootApplicatior Annotation
    7. Running Your Application
    8. Developer Tools
    9. Packaging Your Application for Production
    10. What to Read Next
  4. Spring Boot Features
    1. SpringApplication
    2. Externalized Configuration
    3. Profiles
    4. Logging
    5. Internationalization
    6. JSON
    7. Developing Web Applications
    8. RSocket
    9. Security
    10. Working with SQL Databases
    11. Wording with NoSQL Technologies
    12. Caching
    13. Messaging
    14. Calling REST Services with RestTemplate
    15. Calling REST Services with WebClient
    16. Validation
    17. Sending Email
    18. Distributed
    19. hazelcast
    20. Quartz Scheduler
    21. Task Execution and Scheduling
    22. Spring Integration
    23. Spring Session
    24. Monitoring and Management over JMX
    25. Testing
    26. WebSockets
    27. Web Services
    28. Creating Your Own Auto-configuration
    29. Kotlin support
    30. What to Read Next
  5. Spring Boot Actuator: Production-ready Features
    1. Enabling Production-ready Features
    2. Endpoints
    3. Mnitoring and Management over HTTP
    4. Monitoring and Management over JMX
    5. Loggers
    6. Metrics
    7. Auditing
    8. HTTP Tracing
    9. Process Monitoring Cloud Foundry Support
    10. What to Read Next
  6. Deploying Spring Boot Applications
    1. Deploying to the Cloud
    2. Installing Spring Boot Applications
    3. What to Read Next
  7. Spring Boot CLI
    1. Installing the CLI
    2. Using the CLI
    3. Developing Applications with the Groovy Beans DSL
    4. Configuring the CLI with settings.xml
    5. What to Read Next
  8. Build Tool Plugins
    1. Spring Boot Maven Plugin
    2. Spring Boot Gradle Plugin
    3. Spirng Boot AntLib Module
    4. Supporting Other Build Systems
    5. What to Read Next
  9. “How-to” Guides
    1. Spring Boot Application
    2. Properties and Configuration
    3. Embedded Web Servers
    4. Spring MVC
    5. Testing With Spring Security
    6. Jersey
    7. HTTP Clients
    8. Logging
    9. Data Access
    10. Database Initialization
    11. Messaging
    12. Batch Applications
    13. Actuator
    14. Security
    15. Hot Swapping
    16. Build
    17. Traditional Deployment
  10. Appendices

知识结构

根据课程整理知识结构

  1. Overview
  2. Configuration
  3. Log
  4. Web
  5. Docker
  6. Data Access
  7. 启动配置原理
  8. 自定义starter
  9. Cache (以下高级)
  10. 消息中间件
  11. 检索
  12. 任务
  13. 安全
  14. 分布式
  15. 开发热部署
  16. 监控 (运维)

1 Overview

1.1 Introduce

Spring

为了解决企业级应用开发的复杂性而创建的框架, 可大幅简化开发

通过以下关键策略实现的简化:

  • 基于POJO, 轻量级最小侵入性编程 (对扩展开放)
  • IoC,DI和面向接口, 实现松耦合
  • 基于AOP实现声明式编程
  • 通过AOP和template(模板), 减少样式代码 (这点其实体会不够深)

SpringBoot

  • 用于简化Spring开发的框架

  • 整个Spring技术栈的大整合

  • J2EE开发一站式解决方案

优点

  • 快速创建+独立运行+易与主流工具集成 (其它主流框架) 的Spring项目
  • 使用"嵌入式Servlet容器", 无需打成.war
  • starter自动依赖和版本管理 (控制)
  • 大量"自动配置", 也可手动修改
  • 无需配置XML, 无代码生成, “开箱即用”
  • “运行时应用监控”, 准生产环境的
  • 与云计算天然集成

缺点

  • 入门容易, 精通难. 所以必须懂老Spring

个人认识

  • springcloud之后可能慢慢需要关注"服务网格"
  • “约定大于配置”, 不光要欢呼避免了配置, 同时也意味着约定也会变得更严格.
  • 一些鸡汤讲到, 不能光会"使用工具", 而忽视代码能力

微服务

架构演变:

from Dubbo Document

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ySEpDbyc-1587907368465)(SpringBoot.assets/image-20200423161837334.png)]

概念

In short, the microservice architectural style is an approach to developing a single application as a suite of small services, each running in its own process and communicating with lightweight mechanisms, often an HTTP resource API.

  • 2014, Martin Fowler提出概念

  • 是一种架构的风格, 一个应用可以是一组小型服务, 彼此通过轻量级的例如HTTP方式沟通

  • 每一个功能元素

1.2 快速上手

Hello World

学习环境

jdk 1.8 | maven 3.x | SpringBoot 1.5.9

maven的settings.xml中添加, 按照jdk1.8编译

传统步骤

  1. 新建普通maven

  2. 添加parent标签和web起步依赖

    <parent>
    	官网文档起步可以找到
    </parent>
    <dependencies>
        <depen></depen>
    </dependencies>
    
  3. 主程序类, org.zhangcl

    @SpringBootApplication	// 标注主程序类, 说明这是一个SpringBoot应用
    public class HelloWorldMainApplication{
         
        public static void main(Strring[] args){
         
            SpringApplication.run(HelloWorldMainApplication.class, args);
        }
    }
    
  4. 业务org.zhangcl.controller.HelloController.java

    @Controller
    public class HelloController{
         
        
        @RequestMapping
        @ResponseBody
        public String hello(){
         
            return "Hello, World!";
        } 
    }
    

部署

以前war包, 要在tomcat中, 但boot可以直接jar包

  1. 引入打包插件

      <build>
      <plugins>
       	<plugin>
           	...
    
     </plugin>
       </plugins>
      </build>
    
  2. 使用Maven工具中有package功能

包中有内嵌的tomcat

pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

parent

是SpringBoot的版本仲裁中心

它的父项目是<artifactId>spring-boot-dependencies<artifactId/>

  • 管理各个可整合组件 (依赖) 的版本!
    • 导入这些依赖不需要写版本

    • spring-boot-starter-parent-1.5.9.RELEASE.pom, Ctrl+鼠标, 看parent, 能进来此文件. 其中管理很多重要内容, 如:

      • 很多依赖jar的版本号的仓库, 所以我们引入很多依赖不需要手动指定版本号
      • 过滤器对不同命名类型配置文件的过滤设置
    • 没有管理的依赖仍然需要手动规定版本号

starters 场景启动器

spring-boot-starter-web, "SpringBoot场景启动器"

web, 帮我们导入web模块正常运行所依赖的组件

SpringBoot将所有的功能场景抽取出来, 做成很多starters, 我们只需要在项目里引入这些starter项目场景的, 相关依赖回自动导入进来.

要用什么功能呢, 就导入对应场景启动器

1.3 自动配置-原理1

主启动类HelloApplication.java

@SpringBootApplication

  • 注解标注在某个类上, 说明是主启动类

@SpringBootApplication内部结构

纯结构

  • @SpringBootConfiguration
    • @Configuration()
  • @EnableAutoConfiguration
    • @AutoConfigurationPackage
      • @Import(AutoConfigurationPackages.Registrar.class)
        • AutoConfigurationImportSelector
          • List<String> getCandidateConfigurations(...)
    • @Import(AutoConfigu
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值