Migrating from Spring to Spring Boot

In this post, we will take a look at migrating from Spring to Spring Boot application.

 

1. Why Migrate from Spring to Spring Boot?

Spring Boot provides several features to help manage enterprise applications easily.Spring Boot is not a replacement for the Spring, but it’s a tool for working faster and easier on Spring applications.It simplifies much of the architecture by adding a layer that helps automate configuration and deployment while making it easier to add new features.

Most of the changes for migrating Spring Framework application to Spring Boot are related to configurations.This migration will have minimal impact on the application code or other custom components.Spring Boot brings a number of advantages to the development.

  1. It simplifies Spring dependencies by taking the opinionated view ( we will discuss it in more details).
  2. Spring Boot provides a preconfigured set of technologies/framework to reduces error-prone configuration so we as a developer focused on building our business logic and not thinking of project setup.
  3. You really don’t need those big XML configurations for your project.
  4. Embed Tomcat, Jetty or Undertow directly.
  5. Provide opinionated Maven POM to simplify your configurations.
  6. Application metrics and health check using actuator module.
  7. Externalization of the configuration files.

Let’s take a look at the different steps at migrating from Spring Framework to Spring Boot application.

 

2. Spring Boot Starters

Spring Boot brings simplified dependency management using starters.We don’t need to specify the version for each dependency in our pom.xml file, these starters handle and manage dependencies for our application.Add spring-boot-starter-parent as the parent in the pom.xml file

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

[pullquote align=”normal”]The spring-boot-starter-parent is a special starter that provides useful Maven defaults. It also provides a dependency-management section so that you can omit version tags for “blessed” dependencies. [/pullquote]

 

3. Spring Boot Main Class

Spring Boot based application uses a standard method that follows the Java convention for an application entry point.This entry point is a Java class with the main method, annotated with @SpringBootApplication.

@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
public class ApplicationEntry {
    public static void main(String[] args) {
	SpringApplication.run(ApplicationEntry.class, args);
    }
}

@SpringBootApplication annotation is the combination of following annotations

  • @Configuration – Indicates that a class declares one or more @Bean methods processed by the Spring container to generate bean definitions and service requests for those beans at runtime.
  • @EnableAutoConfiguration – Enable auto-configuration of the Spring Application Context, attempting to guess and configure beans that you are likely to need.
  • @ComponentScan – Configures component scanning directives for use with @Configuration classes.

It’s recommended to locate main application class (annotated with @SpringBootApplication) in the root package.It will make sure that @EnableAutoConfiguration scans all the classes in the same package or below the root.

com
 +- javadevjournal
     +- demoapplication
         +- Application.java
         |
         +- package1
         |   +- Class1.java
         |   +- Class11.java
         |
         +- package2
             +- Class2.java
             +- Class22.java

 

4. Configurations and Components

Spring Boot favors Java-based configurations.For legacy and large applications, it is not possible to change all XML based configurations to Java based configurations immediately.To handle all these cases, Spring Boot provides the way to import XML based configurations.We should use @ComponentScan annotation to automatically pick all Spring component and@Importannotations on the main class

@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
@ComponentScan(basePackages="com.javadevjournal.config")
@Import(CoreConfigurations.class)
public class ApplicationEntry {
   public static void main(String[] args) {
	SpringApplication.run(ApplicationEntry.class, args);
   }
}

Use @ImportResource annotation to import XML based configuration if you do not want to migrate to Java-based configurations or want to do this gradually.

@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
@ImportResource({ "context1.xml", "spring-security.xml" }
public class ApplicationEntry {
    public static void main(String[] args) {
	SpringApplication.run(ApplicationEntry.class, args);
   }
}

 

5. Migrating Application Configurations

Spring Boot provides a very powerful mechanism to externalize application-specific configurations.By default Spring Boot load all properties defined under application.properties file (application.yml).Read our article on managing properties using Spring Boot.Spring also provides an option to load profile specific properties from the file application-{profile}.properties.

For more read Spring Profiles for more detail.

 

6. Migrating Static Resources

Spring Boot checks for static resources in the following locations

  • /public
  • /static
  • /resources
  • /META-INF/resources

We can also customize static resources location by using spring.resources.static-locations property

spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/ # Locations of static resources.

 

7. Migrating Non Web Application

Migrating non-web application to Spring Boot based application is not complex, consider below points for the migration

  1. Use SpringApplication or SpringApplicationBuilder class to create ApplicationContext in place of your custom code.

 

8. Migrating Web Application

Migrating web application to the Spring Boot based application need certain changes (not complex).

 

8.1 The Spring Boot Web Starter

Spring Boot provides a starter for the web application which takes care of providing all the required dependencies to start the web application.

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

You should remove all the web-specific dependencies before using the starter.Spring web starter provides following features

  • Support for Spring MVC and RESTful applications.
  • Tomcat as the default embedded container.
  • Support to serve static content from the configured directory (check section 6).
  • Http message conversion using HttpMessageConverter.
  • Out of the box support for Jackson.
  • The default configuration for /error mapping to handles all errors.

 

8.2 View Technologies

Spring Boot includes auto-configuration support for the following templating engines

  • Thymeleaf
  • FreeMarker
  • Mustache

We need to add a specific starter to start working on the chosen templating engine.

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

[pullquote align=”normal”]When you use one of these templating engines with the default configuration, your templates are picked up automatically from src/main/resources/templates. [/pullquote]

Most of the old applications use JSP as the preferred view technology.Extra configurations needed to use JSP with Spring Boot.

spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
 
8.3 Deploying as a war file

To create a deployable way, we should extend our main application class by SpringBootServletInitializer.

@SpringBootApplication
public class Application extends SpringBootServletInitializer {
  
  @Override
   protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
	// Customize the application or call application.sources(...) to add sources
	// Since our example is itself a @Configuration class (via @SpringBootApplication)
	// we actually don't need to override this method.
	return application;
   }
}
 
8.4 Embedded Servlet Container Support

We can also run our application using the embedded server.Spring Boot support tomcat as the default embedded web server.Spring Boot provides support for the following embedded web servers.

  • Tomcat
  • Jetty
  • Undertow

Add appropriate starter for using servlet container of your choice.

 

8.5  Migrating Spring Security Application

Use spring-boot-starter-security to enable Spring Security for your application.

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

Keep in mind following points while using Spring Security starter.

  • By default, it will create a user called “user” with a randomly generated password.
  • This password logged during application startup.
  • Spring Security starters secure all the endpoint and require us to enter username and password (logged during application startup).

 

Summary

In this article, we look at migrating from Spring to Spring Boot application.We covered what are the common use cases which we should keep in mind while migrating Spring application to Spring Boot framework.

2 thoughts on “Migrating from Spring to Spring Boot”

Comments are closed.