Migrating from Spring To Spring Boot

Priyam Gupta
Engineering @ Housing/Proptiger/Makaan
4 min readDec 7, 2022

You heard it right, we have recently upgraded our backend Spring application to Springboot. Earlier on we were using Spring 4 until we have decided to take up the task for upgrading it to SpringBoot 2. Moving from such lower version of Spring to Springboot can be considered as changing the whole application at Framework level.

SpringBoot Versions

Challenges

  • Dependency Upgradation, finding compatible dependency with Springboot to support the existing behaviour of the application.
  • Functionality Migration, some of the functionality is now being provided using Annotations in Springboot.
  • Upgrading Deprecated Methods, since we are now going to upgrade the dependencies which are compatible with Springboot, we also need to upgrade or use alternative for Deprecated methods.
  • Understanding Request Cycle Flow change, the Request Attributes present with HttpServletRequest are lost in the child threads as soon as the request is completed.

Advantages

  • Default Auto-Configuration is present.
  • Embedded Tomcat are time saver and makes an application ready to Go-Live in few minutes.
  • Springboot are preferably packaged in JAR file which are easily deployable both at development and production environment.
  • New features supported by Springboot as well as new version of 3rd party dependencies are available now such as Dev-Tools, Actuator, 3rd party dependency advancement such as Swagger, Redis-Clients, Apache-Poi.
  • Advanced externalized configurations helps to manage environment dependent properties file efficiently.
  • Application is light-weight and takes less time for Startup.

Code Exploration

Springboot provide a number of starter dependencies each of which is a combination of various essential dependencies which are needed for a certain functionality. It helps us in the way that we don’t need to mention the dependencies version for each of the other dependencies added.

Moving from parent spring dependency (org.springframework) to sprint-boot dependency (org.springframework.boot)

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
</parent>

The above dependency brings a lot more of Annotations used to define our applications, @SpringBootApplication, @ComponentScan, @EnableAutoConfiguration, etc.

The starting point of our application is from class annotated with @SpringBootApplication having the main method

@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

Next we need to add the starter-web dependency which configures up all the web-related functionality such as Dispatcher Servlet, tomcat, validation, jackson.

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

Moved from spring-data-jpa to spring-boot-starter-data-jpa and it brought a whole set of new methods with it.

We also need to upgraded our mysql-connector-java connector which is compatible with the spring-boot-starter-data-jpa, we have upgraded from version 5 to version 8.

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
  • saveAll changed to save, deleteAll to delete
  • findByOne changed to findById
  • Paging and Sorting methods such as PageRequest was changed to Page.of and Sort was changed to Sort.of

Since the JPA has been upgraded so the respective ORM has also to be upgraded, we were using Hibernate for our application so we have upgraded it to Hibernate 5.2

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.17.Final</version>
</dependency>
  • You will also find some annotations are not scanned by default in Springboot which were a part of Spring 4 such as @WebFilter, we need to explicitly enable servlet related annotations for Springboot. One need to find out the respective annotations and provide suitable fix for the same.

Other dependencies related to our use cases such as redis-client, apache-poi, swagger, hystrix, mysql-connector-java

Environment Properties files can now be automatically loaded using profile-specific names suffix, such as application-{profile}.properties.

This helped us to maintain and load easily our environment dependent profiles. To load a specific profile we need to add the vm argument for the same.

-Dspring.profiles.active=staging

If you using module-based structure and have parent repositories you need to update the Spring versions of the parent repos without moving them to Springboot, they should be upgraded to Spring 5. For our use-case, we upgraded our parent repositories from Spring 4 to Spring 5.

In the last few years, Spring Framework has also been advancing wrt time.

Spring Framework Versions

Once we have upgraded our backend application to Spring Boot, it has now been easier for team members to easily setup and run the application, add any latest 3rd party dependency to our codebase , better monitoring due to present of actuator endpoints. The application is quite light weight and takes less time to startup.

--

--