Interpretation of new features of Spring Boot 2.0

Click on " Programmer Gray " above and select "Public Account"

Interesting and meaningful articles are delivered as soon as possible!



This article is reproduced from the public account InfoQ


640?wx_fmt=jpeg Author|Editor Zhai Yongchao|Guo Lei Spring Boot 2.0 is coming, what new features are there? Upgrade? write in front

On March 1, Beijing time, after a long wait, Spring Boot 2.0 was officially released. As an important open source project in the Spring ecosystem, Spring Boot aims to simplify the creation of production-level Spring applications and services. Users only need to "run" to create a stand-alone, production-level Spring application.

Once released, Spring Boot was quickly favored by developers. So far, it has more than 20,000 stars and 16,000 forks (GitHub top ten in 2017). And Spring Boot 2.0 has been brewing for a period of time, from the M1 version released on May 16 last year to the later RC version, it has been nearly a year.

Many exciting new features have been introduced in Spring 2.0, including support for Java 9, HTTP/2, building on Spring 5, strong GSON integration, and more. In order to understand the overall development history of Spring Boot and the important updates in 2.0, InfoQ invited Spring Boot expert and Yonghui Yunchuang architect Zhai Yongchao to write an interpretation.


640?wx_fmt=png

Spring Empire

Spring is a development framework that almost every Java developer is familiar with. Whether you are a fledgling programmer or an experienced driver, you will have some understanding or experience in using it. In the modern enterprise-level application architecture, the Spring technology stack has almost become synonymous with the Java language, so why can Spring stand out among many open source frameworks and become the unanimously recognized technical solution in the industry? Let's start with the original Spring Framework and see why it can swept thousands of troops and ruled the rivers and lakes!

 Challenge authority and become famous

In March 2004, the first version of Spring and its founder Rod Johnson's classic masterpiece "Expert one-on-one J2EE Development without EJB" were released, breaking the traditional thinking mode in the field of Java development at that time, and enterprise-level applications began to move towards The pace of "lightweight" development.

The original Spring Framework 1.0 is not as complicated as today's Spring, but this version has included the two core elements of Spring: Dependency Injection (IOC) and Aspect Oriented Programming (AOP), these two functions are Spring The key to distinguishing it from other excellent frameworks and establishing a core position in enterprise applications.

Many developers may feel that these two functions are of little significance when they are first involved in Java applications, because we can still implement business functions well without them. This is indeed the case, but with the iteration and development of business In-depth, complex and changeable requirements began to slowly erode the original "perfect" architecture, and the difficulty of development and testing gradually increased. Often at this time, we realized the value of Spring.

Therefore, even in the initial version of Spring, many business-oriented functional packages are encapsulated, such as: mail sending, transaction management, etc., but we need to know that the reason why enterprise-level applications are really inseparable from Spring is not that these are directly related to business. related functions, but the two cores mentioned above that have nothing to do with business implementation.

Since Spring's encapsulation of many functionalities was not as powerful as today's Spring in the initial version, for a long time, we have used Spring for project management to integrate other better functional frameworks to complete the architectural pattern of system development. For example, the once-popular Spring + Struts + Hibernate architecture is believed to evoke memories of a generation.

 Elegant and flexible, attracting countless powder

After Spring was released and generally recognized by the industry, the Spring open source community has become very active. In addition to the community's continuous enhancement of Spring, other functional frameworks have also adapted and supported Spring. In the subsequent releases of Spring 2.x and 3.x, the elegant configuration of Annotation and the configuration of more flexible Java classes have been supported successively, which makes Spring more diversified in the configuration of management beans.

However, with the in-depth application of Spring, tedious configuration problems have begun to appear. We will find that every time we build a project, we are always copying and pasting some templated configurations and codes. Sometimes we just want to achieve a few This is a very simple function, and the configuration content is much larger than the writing of business logic code; at the same time, in the process of framework integration, there is a potential conflict risk for some common dependent Jar packages, which makes some complex integration tasks difficult. Therefore, Spring's "lightweight" is not so light in front of other dynamic languages.

 Master of the Wheel, Uncertain Future

In the subsequent Spring 4.x, in addition to providing support for Java 8 and enhancements to dependency injection, for a long time, the Spring community's innovation in its core framework was not so brilliant, and the community began to focus more energy Turning the spear to those who used to be close friends. As a result, we found various functional sibling projects in the Spring community, such as: Spring Data to simplify data access, Spring Batch to provide batch processing capabilities, and Spring Security to protect application security.

Although these frameworks have certain advantages and advanced concepts from an individual perspective, for many existing systems, it is difficult to make changes in the functional framework, and it is difficult for these new wheel projects to be applied, except Some systems starting from scratch will make some attempts. In view of the cost of learning and the risk of stepping on pits, small and medium-sized teams are rarely willing to try these new projects. Therefore, some old functional frameworks are difficult to be replaced by these wheels unless there are serious performance or safety issues.

During this period of time, although the Spring community has launched so many wheel projects, not many have been widely used in China. Many development teams still only use the most core IOC and AOP, and according to their own team's technology stack situation Integrate a more suitable scaffolding for system development.

 The God Soldier was born, creating greater glories

On April 1, 2014, Spring Boot released its first official version. The project aims to help developers more easily create Spring-based applications and services, enabling existing and new Spring developers to get the Spring functionality they need most quickly. It has been nearly 4 years of development until the 2.x version was released today. Spring Boot is already a super popular open source project with more than 21,000 Stars, more than 15,000 Commits, and more than 400 contributors.

Why is Spring Boot suddenly so much attention and respect? The main points are as follows:

  • Simplified dependency management: A series of Starter POMs are provided in Spring Boot, which divides and encapsulates various functional modules, making it easier for us to introduce and use, and effectively avoid users' maintenance when building traditional Spring applications Problems such as JAR conflicts caused by a large number of dependencies.

  • Automatic configuration: Spring Boot provides an automatic Java configuration class for each Starter, which is used to replace the cumbersome and less-changing Bean configuration in XML for our traditional Spring applications; at the same time, with the help of a series of conditional annotation modifications, we can also These auto-configured beans can be easily replaced for extensions.

  • Embedded container: In addition to the optimization of code organization, the embedded container supported in Spring Boot is also a great highlight (here I seem to hear the sentence of Josh Long: "Deploy as a Jar, not a War" ), which makes the packaging and running of Spring Boot applications very lightweight.

  • 生产级的监控端点:spring-boot-starter-actuator的推出可以说是 Spring Boot 在 Spring 基础上的另一个重要创新,为 Spring 应用的工程化变得更加完美。该模块并不能帮助我们实现任何业务功能,但是却在架构运维层面给予我们更多的支持,通过该模块暴露的 HTTP 接口,我们可以轻松的了解和控制 Spring Boot 应用的运行情况。

Spring Boot 虽然是基于 Spring 构建的,但是通过上面这些特性的支持,改变了我们使用 Spring 的姿势,极大得简化了构建企业级应用的各种配置工作,尤其对于很多初学者来说,变得更加容易入门使用。

如约而至,升级与否?

万众期待的 Spring Boot 2.0 终于发布了第一个正式版本,为什么 Spring Boot 2.0 如此受期待呢?我认为主要有以下几个原因:

  1. 支持最新的 Java 9

  2. 基于 Spring 5 构建,Spring 的新特性均可以在 Spring Boot 2.0 中使用

  3. 为各种组件的响应式编程提供了自动化配置,如:Reactive Spring Data、Reactive Spring Security 等

  4. 支持 Spring MVC 的非阻塞式替代方案 WebFlux 以及嵌入式 Netty Server

  5. Spring Boot 2.0 的发布,Spring Cloud Finchley 还会远吗?

上述列举的内容是笔者主要关心的重要内容,并非 Spring Boot 2.0 所有的新特性,对于不同的使用者来说相信会有不同的关注点。

除此之外,在 Spring Boot 2.0 中还有非常多其他令人振奋的新特性,比如:对 HTTP/2 的支持、新增了更灵活的属性绑定 API(可以不通过@ConfigurationProperties注解就能实现配置内容读取和使用)、对 Spring Security 整合的简化配置、Gradle 插件的增强、Actuator 模块的优化等等。

This article does not introduce these new features in detail. The following mainly talks about whether it is necessary for us to upgrade our Spring Boot 1.x to Spring Boot 2.x, and what problems we need to consider and pay attention to in this process.

 Changes in Java Version Requirements

When we choose whether to upgrade Spring Boot, the first thing we need to consider is the choice of Java version. In Spring Boot 2.0, the Java version requirements have been increased, we need to use at least Java 8 to use it, if your Spring Boot application is still running on Java 7, then you have to consider the Java upgrade cost.

In addition, whether you want to use Java 9 in the future will be an important decision-making basis for whether to upgrade or not, because Spring Boot 1.x version clearly states that there is no support plan for Java 9; in other words, if you To run Spring Boot on Java 9, you must upgrade to Spring Boot 2.0.

Tips: Although the current version of Spring Boot 2.0 supports Java 9, there are still some problems. For example: JDK proxy support requires AspectJ 1.9, but this version is still in the RC version; Apache Cassandra is not supported yet; JSP TLDs cannot be supported in embedded Tomcat, etc. For specific solutions to these problems, see: Running Spring Boot on Java 9: ​​https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-with-Java-9

 Upgrading of dependent components

Spring Boot's Starter integrates many excellent third-party components. The upgrade of these components also requires us to do some consideration. During the version upgrade process of these components, whether there are changes in use and other issues. Among them, the most critical components need our attention:

  • Tomcat upgrade to 8.5

  • Flyway upgrade to 5

  • Hibernate upgrade to 5.2

  • Thymeleaf upgrade to 3

Tips: The Tomcat vulnerability problem that was exposed a few days ago. After investigation, the version selected for Spring Boot 2.0 is 8.5.28, which is a safe version, so you can use it with confidence.

 Dependency reorganization and configuration relocation

In the upgrade process of Spring Boot 2.0, this part of the content may be the place where you need to make more changes, so it is recommended that you keep an eye on it here. Since Spring Boot is not a flat layer structure when building Starter POMs, there are reference relationships between some functional module Starters, for example: spring-boot-starter-thymeleaf contains spring-boot-starter-web, Because the thymeleaf template engine must have been used under Spring MVC before.

However, in Spring Boot 2.0, the emergence of WebFlux will no longer be the only solution for web applications, so the dependencies in spring-boot-starter-thymeleaf no longer include spring-boot-starter-web, and developers need to add spring by themselves -boot-starter-web or spring-boot-starter-webflux to decide which module to use to implement the web application.

In addition to the dependency reorganization similar to the above, there are also many relocations of configuration properties in Spring Boot 2.0, which will cause some original configurations to no longer take effect, and we need to manually modify the keys of these configurations to complete the upgrade. adaptation. For example, some servlet-related server.*properties are relocated server.servletunder the prefix:

Old property New property
server.context-parameters.* server.servlet.context-parameters.*
server.context-path server.servlet.context-path
server.jsp.class-name server.servlet.jsp.class-name
server.jsp.init-parameters.* server.servlet.jsp.init-parameters.*
server.jsp.registered server.servlet.jsp.registered
server.servlet-path server.servlet.path

For more dependency changes, configuration relocation and default configuration changes, readers can refer to the official upgrade manual:

https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Migration-Guide

 unnecessary worries

之前有朋友在 spring4all 社区上问:如果 Spring Boot 升级 2.0,2.0 出了那么多新功能,我们的业务代码是否也需要随之修改,风险会不会很大?其实,这个问题大家完全不用太多的顾虑,Spring Boot 2.0 虽然新增了很多强大的新特性,但是对于原有功能的支持并没有抛弃。所以,就算我们不用任何类似 WebFlux 这样的新功能,将工程升级到了 Spring Boot 2.0 之后,继续使用 Spring MVC 开发我们的项目也是完全没有影响的。只是,就如上面所述的,我们可能需要做一些依赖和配置上的调整才能继续将应用正常的运行起来。

总结与展望

感谢大家能够读完上面我对 Spring Boot 2.0 的薄见,希望这些内容能够对你在 Spring Boot 2.0 的选择上有一定的参考价值。这个版本虽然不像 Spring Boot 1.0 那样颠覆我们对繁琐的 Spring 应用的认识,但是依然透露着很多时代前沿的气息。同时,Spring Boot 2.0 的发布,也意味着 Spring Cloud Finchley 离正式发布又近了一步,因为这个版本中同样的将会带来很多令人兴奋的内容,相信这一天的到来也不远了!

对于当前 Spring Boot 2.0 的迁移升级,作为一名 Spring Boot 与 Spring Cloud 的忠实拥护者,在时间允许的情况下,这是一件必然会去尝试的事情,在未来的时间里,我也尽可能的希望抽出时间继续分享一些其中的问题与收获,与大家共勉!

参考资料
  • [Spring Boot 2.0 Release Notes]

    https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Release-Notes

  • [Spring Boot 2.0 Migration Guide]

    https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Migration-Guide

  • [Running Spring Boot on Java 9]

    https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-with-Java-9

about the author

Zhai Yongchao , working in Yonghui Yunchuang as an architect, is responsible for the implementation of the Spring Cloud microservice architecture. Author of "Spring Cloud Microservices in Practice", the initiator of spring4all community.




—————END—————




Friends who like this article, welcome to long press the picture to follow the subscription number programmer Xiaohui , and watch more exciting content

640?wx_fmt=jpeg


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324635573&siteId=291194637