springboot学习笔记(简介与入门)

一、SpringBoot简介

Spring作为一个轻量级的容器,在JavaEE开发中得到了广泛的应用,但是Spring的配置繁琐臃肿,在和各种第三方框架进行整合时代码量都非常大,为了使开发者能够快速上手Spring,利用Spring框架快速搭建JavaEE项目,SpringBoot应运而生。SpringBoot带来了全新的自动化配置解决方案,使用SpringBoot可以快速创建基于Spring生产级的独立应用。SpringBoot中对一些常用的第三方库提供了默认的自动化配置方案,使得开发者只需要很少的Spring配置就能运行一个完整的Web应用。SpringBoot项目可以采用传统的方案打成war包,然后部署到Tomcat中运行。也可以直接打成可执行的jar包,通过java -jar 命令就可以启动一个SpringBoot项目。SpringBoot是随着spring4.0诞生的,它于2014年4月,发布了SpringBoot1.0.0。

一、为什么要使用SpringBoot
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".

能快速创建出生产级别的Spring应用
二、传统的Spring存在的问题
  • spring的xml文件,配置相当繁琐
  • 第三方框架的配置相当复杂
  • spring的开发效率和部署效率等问题
  • 外部的web服务器
  • 管理需要依赖
  • 依赖在maven中pom.xml中
三、SpringBoot的优点
  • Create stand-alone Spring applications
    创建独立Spring应用
  • Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
    内嵌web服务器(默认使用tomcat)
  • Provide opinionated ‘starter’ dependencies to simplify your build configuration
    自动starter依赖,简化构建配置、防止各jar包冲突
  • Automatically configure Spring and 3rd party libraries whenever possible
    自动配置Spring以及第三方功能
  • Provide production-ready features such as metrics, health checks, and externalized configuration
    提供生产级别的监控、健康检查及外部化配置
  • Absolutely no code generation and no requirement for XML configuration
    无代码生成、无需编写XML
四、SpringBoot的缺点
  • SpringBoot是整合Spring技术栈的一站式框架
  • SpringBoot是简化Spring技术栈的快速开发脚手架

时代背景

一、微服务

James Lewis and Martin Fowler (2014) 提出微服务完整概念。

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. These
services are built around business capabilities and independently
deployable by fully automated deployment machinery. There is a
bare minimum of centralized management of these services, which
may be written in different programming languages and use
different data storage technologies.-- James Lewis and Martin
Fowler (2014)
  • 微服务是一种架构风格
  • 一个应用拆分为一组小型服务
  • 每个服务运行在自己的进程内,也就是可独立部署和升级
  • 服务之间使用轻量级HTTP交互
  • 服务围绕业务功能拆分
  • 可以由全自动部署机制独立部署
  • 去中心化,服务自治。服务可以使用不同的语言、不同的存储技术
二、分布式

在这里插入图片描述
分布式的困难:

  • 远程调用
  • 服务发现
  • 负载均衡
  • 服务容错
  • 配置管理
  • 服务监控
  • 链路追踪
  • 日志管理
  • 任务调度

分布式的解决:
springboot+springcloud
在这里插入图片描述

二、入门

1、系统要求

  • Java 8 & 兼容java14 .
  • Maven 3.3+
  • idea 2019.1.2

2、导入依赖

	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
    </parent>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>

3、程序:

@RestController
@SpringBootApplication
public class Satrt01Application {

    public static void main(String[] args) {
        SpringApplication.run(Satrt01Application.class, args);
    }

    @GetMapping("/hello")
    public String hello(@RequestParam(value = "name",defaultValue = "World")String name){
        return String.format("Hello %s!",name);
    }

}

说明:如果不带name属性,输出结果是Hello Word!。

三、了解自动配置原理

1、依赖管理
	父项目做依赖管理
 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.9.RELEASE</version>
    </parent>
他的父项目
 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.3.9.RELEASE</version>
  </parent>

springboot已经管理了常用的依赖,只需要导入spring-boot-start-xxxx就可以开启。
spring-boot-starter: 第三方为我们提供的简化开发的场景启动器。

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

也可以修改默认版本号

1、查看spring-boot-dependencies里面规定当前依赖的版本 用的 key。
2、在当前项目里面重写配置
    <properties>
        <mysql.version>5.1.43</mysql.version>
    </properties>
2、自动配置
@SpringBootApplication
public class MainApplication {
    public static void main(String[] args) {
      SpringApplication.run(MainApplication.class, args); 
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值