第一章:spring5.0概述

本章主要介绍spring的组成部分,支持的能力,整体架构,版本升级的内容。 spring5框架划分为以下的部分:

Core 核心

IoC container, Events, Resources, i18n, Validation, Data Binding, Type Conversion, SpEL, AOP.

Testing

Mock objects, TestContext framework, Spring MVC Test, WebTestClient.

Data Access

Transactions, DAO support, JDBC, ORM, Marshalling XML.

Web Servlet

Spring MVC, WebSocket, SockJS, STOMP messaging.

Web Reactive
新增加的能力

Spring WebFlux, WebClient, WebSocket.

Integration 集成

Remoting, JMS, JCA, JMX, Email, Tasks, Scheduling, Cache.

Languages 多语言

Kotlin, Groovy, Dynamic languages.

注解:
1、SpEL: Spring表达式语言(Spring Expression Language,SpEL),是一种强大的、简洁的装配Bean的方式,它通过运行期执行的表达式将值装配到Bean的属性或构造器参数中。
2、SockJS:是一套支持客户端、服务端进行全双工通讯的js框架,包括client server两部分。其中,client可以单独与服务端的websocket进行协作实现全双工访问。
3、STOMP即Simple (or Streaming) Text Orientated Messaging Protocol,简单(流)文本定向消息协议,它提供了一个可互操作的连接格式,允许STOMP客户端与任意STOMP消息代理(Broker)进行交互。STOMP协议由于设计简单,易于开发客户端,因此在多种语言和多种平台上得到广泛地应用。
4、Reactive响应式(反应式)编程 是一种新的编程风格,其特点是异步或并发、事件驱动、推送PUSH机制以及观察者模式的衍生。
5、JMS:java message service。
6、JCA:JCA (J2EE 连接器架构,Java Connector Architecture)是对J2EE标准集的重要补充。因为它注重的是将Java程序连接到非Java程序和软件包中间件的开发。

相对于spring4,spring5进行了以下的更新:

1、JDK 8+和Java EE7+以上版本

(1)整个框架的代码基于java8
    • 通过使用泛型、lambdas等特性提高可读性。
    • 对java8的部分特性提供直接的代码支撑。
(2)开发部署完全兼容JDK9
    • On classpath as well as module path (with stable automatic module names).
    • 整个框架在jdk9上构建、测试通过(默认为jdk8)
(3)当前spring的相应模块需要Java EE 7 API的支持
    • Servlet 3.1, Bean Validation 1.1, JPA 2.1, JMS 2.0
    • Recent servers: e.g. Tomcat 8.5+, Jetty 9.4+, WildFly 10+
(4) 运行时兼容Java EE8 API
    • Servlet 4.0, Bean Validation 2.0, JPA 2.2, JSON Binding API 1.0
    • Tested against Tomcat 9.0, Hibernate Validator 6.0, Apache Johnzon 1.1

2、取消的包,类和方法

(1)包  beans.factory.access (BeanFactoryLocator 机制).
(2)包 jdbc.support.nativejdbc (NativeJdbcExtractor 机制).
(3)从 spring-aspects module 移除了 mock.staticmock 包.
    • 不再支持 AnnotationDrivenStaticEntityMockingControl .
(5)包 web.view.tiles2 and orm.hibernate3/hibernate4 dropped.
    • 当前版本最低要求: Tiles 3 and Hibernate 5 .
(6)放弃支持: Portlet, Velocity, JasperReports, XMLBeans, JDO, Guava.
    • 建议: 如果需要这些的话请保持在 Spring Framework 4.3.x .
(7)从代码中删除了许多弃用( deprecated)的类和方法.
    • 仅仅为系统中少数常用的方法做了妥协.

3、核心修改概述

(1)JDK 8+ 支持增强:
    • 基于java8 反射机制提供更高效的方法参数访问.
    • 核心spring接口中提供对java8默认方法的可选择性声明.
    • 与JDK 7 Charset and StandardCharsets 使用方式保持一致.
(2)JDK 9 兼容:
    • 摒弃在jdk9中可能废弃的jdk apis.
    • 使用构造器进行实例化保持一致 (修正了异常处理机制).
    • 针对jdk核心类的反射进行防御性使用.
(3)在包级别支持非空api声明:
    • 通过@Nullable注解显示声明 非空的参数、类变量和返回值.
    • 主要用于 IntelliJ IDEA and Kotlin, 但是也支持 Eclipse and FindBugs.
    • 一些Spring APIs 不再容忍空值 (例如 StringUtils).
(4)资源抽象为getFile防御式访问提供了 isFile 判断.
    • 在资源访问上提供基于NIO的管道读取方式.
    • 文件系统访问使用NIO.2 流式方式 (不再使用FileInput/OutputStream).
(5)Spring Framework 5.0 自带通用日志框架:
    • spring-jcl 替换了标准的日志框架,仍然可排他的/覆盖.
    • 无需额外设置,自动检测Log4j 2.x, SLF4J, JUL (java.util.logging) .
(6)spring-core 引入 ASM 6.0 (接近 CGLIB 3.2.5 and Objenesis 2.6).

4、核心容器

(1)支持 @Nullable 注解作为可选注入点的指示器.
(2)在 GenericApplicationContext/AnnotationConfigApplicationContext 上采用函数式风格:
    • Supplier-based bean registration API with bean definition customizer callbacks.
(3)接口方法上提供对 transaction, caching, async annotations 统一检测.
    • CGLIB代理的情况下.
(4)XML 配置命名空间简化为无版本的模式( unversioned  schemas).
    • 总是处理最新的xsd文件,不支持废弃的功能.
    • 版本特定的声明仍然支持,但是根据最新的模式进行验证.
(5)支持候选组件索引 (也支持类路径的扫描).

5、Spring Web MVC

(1)Spring提供的过滤器机制完全支持 Servlet 3.1特性.
(2) Spring MVC controller的方法中支持  Servlet 4.0 PushBuilder argument .
(3)在通用servers上为  Servlet 3.0 多附件转换提供了MaxUploadSizeExceededException.
(4)通过 MediaTypeFactory为通用media类型提供了统一支持.
    • 替换掉 Java Activation Framework.
(5)绑定不可变对象 (Kotlin / Lombok / @ConstructorProperties)
(6)支持JSON绑定API (使用Eclipse Yasson or Apache Johnzon 作为 Jackson and GSON的替代品).
(7)支持 Jackson 2.9.
(8)支持 Protobuf 3.
(9)支持Reactor 3.1 Flux和Mono以及RxJava 1.3和2.1作为Spring MVC控制器方法的返回值,目的是在以Spring MVC控制器中使用新的反应式WebClient(见下文)或Spring Data Reactive存储库.
(10)新的路径转换器作为 AntPathMatcher的替代品,提供更高效率的转换及扩展语法.
(11)@ExceptionHandler 注解方法允许 RedirectAttributes  参数 (and therefore flash attributes).
(12)支持 可编程式的ResponseStatusException 作为 @ResponseStatus的替换品.
(13)脚本引擎不需要继承Invocable,而是通过使用ScriptEngine#eval(String, Bindings)对脚本进行直接渲染,支持 i18n and nested .
(14) ScriptTemplateView 中的模版使用新的渲染上下文参数.
(15)Spring's FreeMarker宏命令 (spring.ftl) 当前使用 HTML 输出格式化 (需要FreeMarker 2.3.24+).

6、Spring WebFlux:响应式web编程

(1)新的spring-webflux模块基于响应式编程,可作为传统的spring-webmvc的替换版本。它是完全异步非阻塞的, 使用 enent-loop执行模型。而传统的方式则是大线程池模式:每个线程处理一个请求.
(2)在spring-core中对响应式编程提供了基础的支持,例如 通过 Encoder and Decoder 为对象流进行编解码操作; DataBuffer抽象用来使用 Java ByteBuffer or Netty ByteBuf; ReactiveAdapterRegistry 提供在 controller方法签名中透明的使用响应式编程的库.
(3)spring-web中提供 HttpMessageReader and HttpMessageWriter,构建于 Encoder and Decoder之上; server HttpHandler with adapters to (non-blocking) runtimes such as Servlet 3.1+ containers, Netty, and Undertow; WebFilter, WebHandler 以及其它的非阻塞组件作为Servlet API的替代品.
(4)@Controller编程风格与spring MVC一致,基于注解方式,但是在WebFlux中,运行在响应式栈上,例如在 controller方法参数上支持reactive,在I/O上从不阻塞,在HTTP socket上全面支持backpressure, 在其它非 Servlet容器上运行,例如Netty and Undertow.
(5)函数式编程作为基于注解式编程的替代品,通过一个endpoint routing API就能运行在响应式栈和WebFlux设施上.
(6)采用函数式、响应式API的HTTP调用类 WebClient , 类似于 RestTemplate ,但采用的是流式API,尤其擅长基于 WebFlux的非阻塞模式下的流式场景; 在 5.0 中为了支持 WebClient, AsyncRestTemplate已经弃用.

7、Kotlin support:Kotlin 是一个基于 JVM 的新的编程语言

(1)在 Kotlin 1.1.50+之上支持 Null-safe API 。
(2)支持 Kotlin不可变的类,使用可选参数和默认值.
(3)支持函数式bean定义  Kotlin DSL .
(4)为 WebFlux提供Functional routing Kotlin DSL.
(5)通过 Kotlin reified 类型参数,可以避免在不同的apis中显示指定序列化/反序列化使用的类,例如RestTemplate or WebFlux APIs.
(6)Kotlin null-safety 支持  @Autowired/@Inject and @RequestParam/@RequestHeader/等注解,目的是确定注入点或处理方法的参数是否是非空的.
(7)ScriptTemplateView支持Spring MVC和Spring WebFlux的Kotlin脚本.
(8)在Model,ModelMap和Environment中添加了类似数组的设置器。
(9)支持具有可选参数的Kotlin autowired构造函数.
(10)Kotlin reflection 用来确定接口方法参数.

8、Testing Improvements

(1)在 Spring TestContext Framework中完成了对 JUnit 5's Jupiter 编程和扩展模块 的支持.
    • SpringExtension: 是一个来自于JUnit Jupiter的多个扩展api的实现,Spring TestContext Framework特性的全面支持。该支持通过 @ExtendWith(SpringExtension.class)打开.
    • @SpringJUnitConfig: 是一个组合注解,由JUnit Jupiter 的@ExtendWith(SpringExtension.class) 与 Spring TestContext Framework 的 @ContextConfiguration注解合并而成.
    • @SpringJUnitWebConfig: 是一个组合注解,由JUnit Jupiter 的@ExtendWith(SpringExtension.class) 与 Spring TestContext Framework 的 @ContextConfiguration和 @WebAppConfiguration 注解合并而成.
    • @EnabledIf: 标识当SpEL表达式或属性值替换后为true时,标识的测试类或测试方法执行.
    • @DisabledIf: 标识当SpEL表达式或属性值替换后为true时,标识的测试类或测试方法不执行.
(2)在 Spring TestContext Framework中支持并行执行测试.
(3)现在 Spring TestContext Framework中测试执行前后的回调函数,使用  SpringRunner (but not via JUnit 4 rules) 来支持 TestNG, JUnit 5, and JUnit 4.
现在beforeTestExecution() and afterTestExecution() 回调函数在  TestExecutionListener API and TestContextManager.
(4)MockHttpServletRequest  通过 getContentAsByteArray() and getContentAsString() 方法访问请求内容 (i.e., request body).
(5) Spring MVC测试中,假如mock请求的字符集已经设置的话,print() and log() 方法会打印出请求的内容.
(6) Spring MVC 中的redirectedUrl() and forwardedUrl() 方法的测试,支持带有可变参数的URI模版.
(7)XMLUnit 更新到 2.3.





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值