On February 28, 2018, Spring Boot entered the 2.0 era, more than 4 years ago. In November 2022, Spring Boot 3.0 will be officially released, it will be based on Spring Framework 6.0, and requires Java 17 or higher, and it will also be the first Spring Boot version of Jakarta EE 9. There are still six months left for developers to transition to Spring Boot 3.0, today Fat Brother will tell you some ways to quickly migrate to 3.0 in the future.

Java 17

Java 17 will be the most significant LTS release since Java 8, the culmination of eight years of effort by the Java community. Including many important improvements, Java 17 is also the most performant LTS version available. All current Spring Boot 2.x versions are well suited for Java 17. You can start the JDK upgrade and debugging without waiting, and try some new features and APIs.

Upgrade to Spring Boot 2.7 as soon as possible

A few days ago, fat brother has said that Spring Boot 2.7 is basically the last major version of Spring Boot 2.x. Spring Boot 2.5 has stopped OSS support and will no longer be maintained. Spring Boot 2.6 will also be stopped after the release of Spring Boot 3.0. Maintenance and iteration are getting faster and faster. Upgrade to 2.7 as soon as possible to better migrate to 3.0. Here, fat brother recommends not to skip version upgrades. For example, do not directly jump from 2.4 to 2.7. Try to upgrade according to steps such as 2.4, 2.5, 2.6, and 2.7. Too large span is not conducive to smoothness. upgrade.

Remove obsolete code

Each Spring Boot version will have more or less code marked as @Deprecated , Spring Boot 3.0 will completely remove the code that is obsolete in 2.x, of course, the obsolete code of early 2.x may also be It was removed in the latest 2.x. Try not to use outdated code. Generally, outdated code is annotated with outdated reasons or alternative APIs.

Changes to Profile Mechanism

In Spring Boot 2.4, the loading mechanism of the configuration files application.properties and application.yaml has been changed. The purpose is to simplify the loading method of external configuration and make it more reasonable. compatible below. For smooth upgrade, Spring provides a configuration item to be compatible with the old mechanism:

 spring:
  config:
    use-legacy-processing: true

And this mechanism will be removed in 3.0. We must use a configuration method that conforms to the new mechanism. If you have these methods, you need to pay attention.

Multi-document Yaml

If you use the spacer --- in the yaml configuration file to declare multiple documents, you have to know that the declared configuration properties are now registered in the order in which the documents are declared; while in Spring Boot In 2.3 and earlier, the order of activation was based on profiles. for example:

 ---
spring:
  profiles: 
    active: dev
  application:
    name: dev-app
server:
  port: 8081      
---
spring:
  profiles:
    active: prod
  application:
    name: prod-app
server:
  port: 8080

This configuration file in Spring Boot 2.3 and earlier will determine the loaded environment according to spring.profiles.active . But starting from 2.4 later properties will override the previous ones.

External configuration always overrides configuration inside the jar

If your config file is outside the jar and the config file is for a specific environment, eg application-dev.yaml . In versions below 2.4, the application.yaml 87d03e052be46eae3830349800799e47--- file outside the jar will not overwrite the application-<profile名称>.yaml file in the jar. Starting from 2.4, the external file will always override the configuration file inside the jar. You need to check if you have this situation.

activate profile

If you used the spring.profiles property to activate the environment configuration, you should migrate to spring.config.activate.on-profile now.

Old way of playing:

 spring:
  profiles: "prod"
secret: "production-password"

New gameplay:

 spring:
  config:
    activate:
      on-profile: "prod"
secret: "production-password"

This is really frustrating. spring.profiles.active can still be used to activate specific environments, such as the command line

 $ java -jar myapp.jar --spring.profiles.active=prod

You can also use application.properties or application.yaml application-<profile>.yaml spring.profiles.active , starting from 2.4 spring.profiles.active application-<profile>.yaml cannot be used in --bbad4aa46dc0e822c527541ba084af05---, nor can it be used in multiple documents with --- interval. In a word, you can no longer use spring.profiles.active to merge a configuration file that contains the spring.config.activate.on-profile attribute.

In addition, the spring.profiles.include attribute can only be used in non-specific configuration files. The following configuration is invalid:

 # 无效配置
spring:
  config:
    activate:
      on-profile: "prod"
  profiles:
    include: "metrics"

For more points, please refer to the official configuration file Spring boot configuration migration guide .

A more performant path resolution method

Starting from Spring Boot 2.6, path resolution uses PathPatternParser by default, replacing the previous Ant style matching AntPathMatcher , many people have problems with Swagger when upgrading, through spring.mvc.pathmatch.matching-strategy Fixed this issue. Although in Spring Boot 3.0 AntPathMatcher will continue to take effect, but PathPatternParser become the official recommendation, because it has higher performance, I will also publish a special topic to analyze it later PathPatternParser .

Compatibility problems

The first is the compatibility issue of Jakarta EE 9, make sure your third-party dependencies and your code are compatible with Jakarta EE 9. Also check that the third-party dependency jars that are being used by the Spring framework have plans to be compatible with Spring 6.

Try to learn Spring 6

Spring 6 and Spring Boot 3 have released several milestones, and you can take some time to try them out in your spare time, experience new features and changes, and evaluate the difficulty of upgrading your application.

关注公众号:Felordcn 获取更多资讯

Personal blog: https://felord.cn


码农小胖哥
3.8k 声望8k 粉丝