当前位置: 代码迷 >> 综合 >> SpringBoot入门7
  详细解决方案

SpringBoot入门7

热度:87   发布时间:2023-09-30 02:51:06.0

参考:Spring Boot 2.0深度实践之系列总览

 

核心特性

组件自动装配:模式注解、@Enable模块、条件装配、加载机制

外部化配置:Environment抽象、生命周期、破坏性变更。

嵌入式容器:Servlet Web容器、Reactive Web容器

Spring Boot Starter:依赖管理、装配条件、装配顺序

Production-Ready:健康检查、数据指标、@Endpoint管控

SpringBoot与Java EE规范

Web Servlet(JSR-315,JSR-340)
SQL JDBC(JSR-221)
数据校验 Bean Validation(JSR303,JSR-349)
缓存 Java Caching API (JSR-107)
WebSockets Java API for WebSocket(JSR-356)
Web Services JAX-WS(JSR-224)
Java 管理

JMX(JSR 3)

消息 JMS(JSR-914)
   

组件自动装配

激活:@EnableAutoConfiguration

配置:/META-INF/spring.factories

实现:XXXAutoConfiguration

创建一个项目

生产准备特性

指标:/actuator/metrics

健康检查: /actuator/health

外部化配置:/actuactor/configprops

传统Servlet应用

Servlet组件:Servlet,Filter, Listener

Servlet注册:Servlet注解、Spring Bean, RgistrationBean

异步非阻塞:异步Servlet,非阻塞Servlet

1、pom.xml中依赖

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

2、代码..web.servlet.MyServlet.java

package com.imooc.diveinspringboot.web.servlet;import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;@WebServlet(urlPatterns = "/my/servlet")
public class MyServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		super.doGet(req, resp);resp.getWriter().print("Hello World!!!");}
}

3、在DiveInSpringBootApplication.java中注册

@SpringBootApplication
@ServletComponentScan(basePackages = "com.imooc.diveinspringboot.web.servlet")
public class DiveInSpringBootApplication {public static void main(String[] args) {SpringApplication.run(DiveInSpringBootApplication.class, args);}
}

异步Servlet

代码..web.servlet.MyServletAsync.java

@WebServlet(urlPatterns = "/my/servletasync",asyncSupported = true)
public class MyServletAsync extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {AsyncContext asyncContext = req.startAsync();asyncContext.start(()->{try {resp.getWriter().print("Hello World!!!");asyncContext.complete(); //触发完成;} catch (IOException e) {e.printStackTrace();}});}
}

Spring Web MVC应用

Web MVC 视图: 模板引擎、内容协商、异常处理等

Web MVC REST:资源服务、资源跨域、服务发现等

Web MVC核心:核心架构、处理流程、核心组件

 

1、ViewResolver、view

 

Spring Web Flux应用

Reactor基础:Java Lambda,Mono,Flux

Web Flux核心:Web MVC注解,函数声明,异步非阻塞

使用场景:Web Flux优势和限制

Web Server应用

切换Web Server

自定义Servlet Web Server

自定义 Reactive Web Server

1、Tomcat切换为Jetty,pom.xml依赖

        <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><exclusions><!-- Exclude the Tomcat dependency --><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></exclusion></exclusions></dependency><!-- Use Jetty instead --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jetty</artifactId></dependency>

2、替换为WebFlux容器。

需要注释Web容器,因为Web容器的优先级高。

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

Nettystart

自定义 Servlet Web Server
WebServerFactoryCustomizer
自定义 Reactive Web Server
ReactiveWebServerFactoryCustomizer

关系型数据

JDBC:数据源, JdbcTemplate, 自动装配

JPA:实体映射关系, 实体操作, 自动装配

事务: Spring事务抽象,JDBC事务处理,自动装配

1、JDBC POM.xml依赖

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

2、JPA POM.xml依赖

        <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency>    

事务

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>

Spring Boot应用

SpringApplication:失败分析,应用特性,事件监听等

Spring Boot配置:外部化配置,Profile, 配置属性

Spring Boot Starter: Starter开发,最佳实践

Spring Boot Actuator 运维管理

端点:各类Web和JMX Endpoints

健康检查:Health, HealthIndicator

指标:内建Metrics, 自定义Metrics

1、Pom.xml依赖

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

 

2、Java Mission Control

3、开放所有的端点,配置application.yml

# 开放索引的 Web Endpoints
management:endpoints:web:exposure:include: '*'