Migrating from Spring to Spring Boot – 从Spring迁移到Spring Boot

最后修改: 2017年 7月 31日

1. Overview


In this article, we’re going to take a look at how we can migrate an existing Spring Framework application to a Spring Boot application.

在这篇文章中,我们将看看如何将现有的Spring Framework应用程序迁移到Spring Boot应用程序。

Spring Boot is not intended to replace Spring, but to make working with it faster and easier. As a result, most of the changes needed for migrating an application are related to configuration. For the most part, our custom controllers and other components will remain the same.

Spring Boot并不是为了取代Spring,而是为了使其工作更快、更容易。因此,迁移应用程序所需的大部分变化都与配置有关。在大多数情况下,我们的自定义控制器和其他组件将保持不变。

Developing with Spring Boot brings several advantages:

使用Spring Boot进行开发会带来几个优势。

  • simpler dependency management
  • default auto-configuration
  • embedded web server
  • application metrics and health checks
  • advanced externalized configuration

2. Spring Boot Starters


First, we’ll need a new set of dependencies. Spring Boot provides convenient starter dependencies, which are dependency descriptors that can bring in all the necessary technology for certain functionality.

首先,我们需要一组新的依赖项。Spring Boot提供了方便的启动器依赖,这是依赖描述符,可以为某些功能带来所有必要的技术。

These have the advantage that you no longer need to specify a version for each dependency, but instead, let the starter manage dependencies for you.


The quickest way to get started is by adding the spring-boot-starter-parent pom.xml:

最快的方法是通过添加spring-boot-starter-parent pom.xml:开始。


This will take care of dependency management.


We’ll go through some more starters in the next sections, depending on which functionality we will migrate. For reference, you can find the full list of starters here.


As a more general note, we will want to remove any explicitly defined dependency version which is also managed by Spring Boot. If not, we may encounter incompatibilities between our defined versions and those used by Boot.

一般来说,我们希望删除任何明确定义的、也由Spring Boot管理的依赖版本。否则,我们可能会遇到我们定义的版本与 Boot 使用的版本之间不兼容的情况。

3. Application Entry Point


Each application built using Spring Boot needs to define the main entry point. This is usually a Java class with the main method, annotated with @SpringBootApplication:

使用Spring Boot构建的每个应用程序都需要定义主入口点。这通常是一个带有main方法的Java类,用@SpringBootApplication注释。

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

The @SpringBootApplication annotation adds the following annotations:


  • @Configuration – which marks the class as a source of bean definitions
  • @EnableAutoConfiguration – which tells the framework to add beans based on the dependencies on the classpath automatically
  • @ComponentScan – which scans for other configurations and beans in the same package as the Application class or below

By default, the @SpringBootApplication annotation scans all classes in the same package or below. Therefore, a convenient package structure could look like this:



If your application is a non-web application which creates an ApplicationContext, this code can be removed and replaced with the @SpringBootApplication class above.


An issue we may encounter has multiple configuration classes which conflict. To avoid this, we have the possibility of filtering the classes which are scanned:


@ComponentScan(excludeFilters = { 
  @ComponentScan.Filter(type = FilterType.REGEX, 
  pattern = "com.baeldung.config.*")})
public class Application {

4. Import Configuration and Components


Spring Boot relies heavily on annotations for configuration, but you can still import your existing configuration in both annotation and XML format.

Spring Boot在很大程度上依赖注释进行配置,但你仍然可以以注释和XML格式导入你现有的配置。

For your existing @Configuration or component classes to be picked up, you have two options:


  • move the existing classes to a package that is the same or below the main Application class package
  • import the classes explicitly

To import the classes explicitly, you can use the @ComponentScan or @Import annotations on the main class:


public class Application {

The official documentation recommends using annotations over XML configuration. However, if you already have XML files you do not wish to convert to Java configuration, you can still import these using @ImportResource:


public class Application {

5. Migrate Application Resources


By default, Spring Boot looks for resource files in one of the following locations:

默认情况下,Spring Boot会在以下位置寻找资源文件。

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

To migrate, we can move all our resource files to one of these locations, or we can customize the resource locations by setting the spring.resources.static-locations property:



6. Migrate Application Properties


The framework will automatically load any properties defined in files called application.properties or application.yml placed in one of these locations:


  • a /config subdirectory of the current directory
  • the current directory
  • a /config directory on the classpath
  • the classpath root

To avoid loading properties explicitly, we can move them to a file with this name in one of these locations. For example, into the /resources folder which should be present on the classpath.


We can also automatically load profile-specific properties from files called application-{profile}.properties.


Also, a large number of predefined property names are available for configuring different application behaviors.


Each Spring framework module that you use in your application will require slight modifications, mainly relating to the configuration. Let’s take a look at some of the most commonly used functionalities.


7. Migrate a Spring Web Application

7.迁移一个Spring Web应用程序

7.1. Web Starter


Spring Boot provides a starter for web applications that will bring in all the necessary dependencies. This means we can remove all the web-specific dependencies from the Spring framework and replace them with spring-boot-starter-web:

Spring Boot为Web应用程序提供了一个启动器,它将带来所有必要的依赖。这意味着我们可以从Spring框架中移除所有针对Web的依赖,并用spring-boot-starter-web取代它们。


Since Spring Boot attempts to auto-configure an application whenever possible based on the classpath, adding this dependency will result in having the @EnableWebMvc annotation added to the main Application class, as well as setting up a DispatcherServlet bean.

由于Spring Boot试图尽可能根据classpath自动配置应用程序,因此添加此依赖关系将导致在主Application类中添加@EnableWebMvc注解,以及设置DispatcherServletbean。

If you had a WebApplicationInitializer class that sets up a DispatcherServlet, this is no longer necessary, nor is the @EnableWebMvc annotation.


We can, of course, define our beans if we want a custom behavior, and in that case, our beans will be used.


If we explicitly use the @EnableWebMvc annotation on a @Configuration class, then the MVC auto-configuration will no longer be enabled.


Adding the web starter also determines the auto-configuration of the following beans:


  • support for serving static content from a directory called /static, /public, /resources or /META-INF/resources on the classpath
  • HttpMessageConverter beans for common use cases such as JSON and XML
  • a /error mapping that handles all errors

7.2. View Technologies


As far as building web pages go, the official documentation recommends not using JSP files and using a template engine instead. Auto-configuration is included for the following template engines: Thymeleaf, Groovy, FreeMarker, Mustache. All we need to do to use one of them is add the specific starter:

就构建网页而言,官方文档建议不要使用JSP文件,而是使用模板引擎。自动配置包括以下模板引擎。Thymeleaf, Groovy, FreeMarker, Mustache。我们需要做的就是添加特定的启动器来使用它们中的一个。


The template files should be placed in /resources/templates folder.


If we want to continue using JSP files, we need to configure the application so that it can resolve JSPs. For example, if our files are in /webapp/WEB-INF/views, then we need to set the following properties:



7.3. Embedded Web Server


Also, we can also run our application using an embedded Tomcat server, which will be auto-configured on port 8080 by adding the spring-boot-starter-tomcat dependency:



Other web servers for which Spring Boot provides auto-configuration are Jetty and Undertow.

Spring Boot提供自动配置的其他Web服务器是JettyUndertow

8. Migrate a Spring Security Application


The starter for enabling Spring Security is spring-boot-starter-security:

启用Spring Security的启动器是spring-boot-starter-security


By default, this will create a user called “user” with a randomly generated password logged during startup and secure all endpoints with basic authentication. However, we usually want to add our security configuration, which is different than the default.

默认情况下,这将创建一个名为 “user “的用户,并在启动时记录一个随机生成的密码,用基本认证保护所有端点。然而,我们通常想添加我们的安全配置,这与默认值不同。

For this reason, we will keep our existing class annotated with @EnableWebSecurity which extends WebSecurityConfigurerAdapter and defines a custom configuration:


public class SecurityConfig extends WebSecurityConfigurerAdapter {
    // ...

9. Migrate a Spring Data Application

9.迁移一个Spring Data应用程序

Depending on which Spring Data implementation we are using, we will need to add the corresponding starter. For example, for JPA, we can add the spring-boot-starter-data-jpa dependency:

根据我们所使用的Spring Data实现,我们将需要添加相应的启动器。例如,对于JPA,我们可以添加spring-boot-starter-data-jpa依赖性。


If we want to use an in-memory database, adding the corresponding dependency enabled auto-configuration for databases of type H2, Derby, and HSQLDB.


For example, to work with an H2 in-memory database, all we need is the h2 dependency:



If we want to work with a different database type and configuration, such as a MySQL database, then we need the dependency as well as to define a configuration.


For this, we can either keep our DataSource bean definition or make use of pre-defined properties:



Spring Boot will auto-configure Hibernate as the default JPA provider, as well as a transactionManager bean.

Spring Boot将自动配置Hibernate作为默认JPA提供者,以及transactionManagerbean。

10. Conclusion


In this article, we have shown some common scenarios encountered when migrating an existing Spring application to the newer Spring Boot framework.

在本文中,我们展示了将现有Spring应用程序迁移到较新的Spring Boot框架时遇到的一些常见情况。

Overall, your experience when migrating will, of course, be highly dependent on the application you have built.