How does Spring Boot transform old projects quickly?

Posted by alsal on Thu, 28 May 2020 04:59:04 +0200

Source: KL Blog
http://www.kailing.pub/article/index/arcid/188.html

1. Preamble Fragments

Blogger's company upgraded the entire module to spring boot because of limitations in the low version of spring when using certain features in development for a project. I want to make a note here to help friends with the same scene.

The whole process is very simple and takes about 2 hours to resolve various version conflicts in the project, but I will introduce a magic artifact below.

2. Status of old projects

1. The project uses spring-context as a container and uses RabbitMQ Provide RPC services

2.Spring.springframeworkVersion low, version 3.1.x, upgraded to 4.3.x

3. Project Use maven structure

The above is the basic situation of the project. For the above situation, the following describes in detail the focus needed in the transformation process.

Step 1: Add a spring boot dependency

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.7.RELEASE</version>
      <scope>import</scope>
      <type>pom</type>
    </dependency>
  </dependencies>
</dependencyManagement>
<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
  </dependency>
</dependencies>

Step 2: **** Add a new spring boot boot class to load the original xml configuration

/**
* Created by kl on 2018/1/29.
* Content :Lbt-service-ext Service Starter
*/@SpringBootApplication(exclude = {RabbitAutoConfiguration.class})@ImportResource("service-context.xml")publicclassLbtServiceExtApplication{
publicstaticvoid main(String[] args) {
  SpringApplication application= newSpringApplication(LbtServiceExtApplication.class);
  application.setWebEnvironment(false);
  application.run(args);
}}

Notes:

1. Excluded RabbitMQ Autoloaded because it is already configured in xml RabbitMQ Related Connections and Service Information

2. setWebEnvironment(false) is set, marking the project as a non-web project because it is only provided RPC Service, so no servlet container is required.

Step 3: **** Try to start and eliminate jar conflicts

At this point, you can start the main method to see if it can be started. Normally, it is not so easy to start, and there are various jar conflicts.Our projects range from 3.x to 4.x and are full of conflicts.

Here's a plug-in to get rid of jar conflict resolution, provided that it's developed under IDEA and eclipse should have something similar.

Plugin name: ******Maven Helper

Can replace MVN DEpendency:treeCommand, this plug-in can list the jars that the project depends on more intuitively. What's fantastic is that you can directly list the jars that conflict in the project, which is useful for finding jar conflicts and can be excluded by right-clicking.

jar-related anomaly recognition techniques:

NoSuchMethodError: usually jar conflicts

ClassNotFoundException: missing related jar

After three steps, the project ran properly.

3. How Spring Boot identifies web items

1.spring boot identifies if the item is a web item, and throws an exception if it identifies the web item in question and does not add a container jar such as tomcat.

2. Identify if the project relies on servlet-api and spring-web.Our project requires spring-web-related functions such as el and no tomcat container, so it can be designated as a non-Web project.

3. Excluding tomcat, the jar volume of the project and the memory footprint at runtime have improved significantly.

Recommend going to my blog to read more:

1.Java JVM, Collections, Multithreaded, New Features Series Tutorial

2.Spring MVC, Spring Boot, Spring Cloud series tutorials

3.Maven, Git, Eclipse, Intellij IDEA Series Tools Tutorial

4.Latest Interview Questions for Java, Backend, Architecture, Alibaba, etc.

Feel good, don't forget to say yes + forward!

Topics: Java Spring RabbitMQ Maven xml