1 Spring Boot官网
1.1 Pivotal
Pivotal Software, Inc. is a software and services company
based in San Francisco and Palo Alto, California, with
several other offices. Divisions include Pivotal Labs (consulting services),
Pivotal Cloud Foundry, and a group developing big data products.
The world’s most established companies run on Pivotal.
The results are transformational. Through adoption of our platform,
tools, and methodology, these companies have unleashed innovation
and reduced time-to-market, spending less to maintain their existing
application portfolio. Results span industries, including automotive, financial services, industrial, media, retail, government, technology, and telecommunications.
1.2 BUILD ANYTHING
Spring Boot is designed to get you up and running as quickly as possible, with minimal upfront configuration of Spring. Spring Boot takes an opinionated view of building production-ready applications.
Spring Boot为快速启动和运行以及最小化配置的Spring应用而设计。
Spring Boot采用一套固化的认知来建立生产环境准备的应用。
1.3 Overview
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”. We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.
Spring Boot让创建单独的生产级别的Spring应用变得容易,你仅仅只需要运行即可。 我们采用一套关于固化Spring平台和第三包依赖库的认知,以至于你可以通过最小的烦恼来启动。 大多数Spring Boot的应用程序只需要非常少的Spring配置。
1.4 Features
- Create stand-alone Spring applications
- Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
- Provide opinionated ‘starter’ dependencies to simplify your build configuration
- Automatically configure Spring and 3rd party libraries whenever possible
- Provide production-ready features such as metrics, health checks and externalized configuration
- Absolutely no code generation and no requirement for XML configuration
2 Spring MVC和Spring Boot
试想一下使用Spring或者Spring MVC的经历,有哪些痛苦?
2.1 Spring MVC图解
2.2 图解说明
- 客户端请求提交到DispatcherServlet
- 由DispatcherServlet控制器查询一个或多个HandlerMapping,找到处理请求的Controller
- ispatcherServlet将请求提交到Controller
- Controller调用业务逻辑处理后,返回ModelAndView
- DispatcherServlet查询一个或多个ViewResolver视图解析器,找到ModelAndView指定的视图
- 视图负责将结果显示到客户端
2.3 我们需要做的事
(1) 引入jar包或者maven依赖
spring-aop
spring-context
spring-beans
spring-core
spring-web
spring-webmvc
...
(2) web.xml
配置DispatchServlet映射
(3) springmvc核心配置文件
spring-mvc.xml
<context:component-scan base-package="com.gupao.xxx"/>
<mvc:annotation-driven />
<视图解析器 InternalResourceViewResolver/> ...
(4) controller代码
(5) deploy project->external tomcat
2.4 思考
对于一直盛行的Spring来说,从开始bean的管理方式,到后来web开发,jdbc开发,事务管理等,但是每次创建 Spring项目的时候,配置文件、依赖管理等这些一直浪费了开发人员宝贵的时间,而且稍微有一些地方不正确,就 会要花时间精力排查问题。
这些问题Spring团队是否有意识到?当然有,所以Spring Boot就是为了让开发人员不再去注重配置文件而把主要 精力放到业务代码上,但是Spring中一些优秀的理念和方式还是值得继续延续和使用的
3 初识Spring Boot
3.1 搭建工程方式
4 Spring Boot工程结构
4.1 Pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
<relativePath/>
</parent>
<groupId>com.yang</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>demo</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4.2 DemoApplication
package com.yang.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
4.3 配置文件application.properties
# 文件编码
banner.charset= UTF-8
# 文件位置
banner.location= classpath:banner.txt
4.4 templates和static
5 Spring Boot与微服务
5.1 再次理解Spring Boot
方便搭建和开发,总之很方便,后面再慢慢感受。
5.2 微服务
Microservices 链接:https://martinfowler.com/articles/microservices.htm
In short, the microservice architectural style [1] 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.
|