1. 概述
本文将带你一步步将传统 Spring Framework 应用迁移到 Spring Boot。Spring Boot 不是要取代 Spring,而是让开发更高效、更简单。因此迁移工作主要集中在配置层面,而自定义的 Controller 和其他组件基本可以保留。
使用 Spring Boot 的优势很明显:
✅ 更简单的依赖管理
✅ 默认自动配置
✅ 内嵌 Web 服务器
✅ 应用监控与健康检查
✅ 高级外部化配置
2. Spring Boot 启动器
首先需要引入新的依赖体系。Spring Boot 提供了便捷的启动器(Starter),本质是依赖描述符,能自动引入特定功能所需的所有技术栈。
优势在于:你不再需要为每个依赖指定版本号,而是让启动器统一管理。
最快速的入门方式是在 pom.xml
中添加父项目:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
这会自动处理依赖管理。后续根据迁移的功能模块添加对应启动器,完整列表可参考官方文档。
⚠️ 踩坑提醒:务必移除所有显式声明的、同时被 Spring Boot 管理的依赖版本号,否则可能引发版本冲突。
3. 应用入口点
每个 Spring Boot 应用都需要定义主入口。通常是一个带有 @SpringBootApplication
注解的 Java 类:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@SpringBootApplication
注解组合了:
@Configuration
:标记为 Bean 定义源@EnableAutoConfiguration
:基于类路径依赖自动添加 Bean@ComponentScan
:扫描主类所在包及其子包中的配置和组件
默认扫描范围是主类所在包及其子包,推荐的项目结构如下:
如果原应用是创建 ApplicationContext
的非 Web 应用,可直接替换为上述入口类。
遇到多配置类冲突时,可通过过滤解决:
@SpringBootApplication
@ComponentScan(excludeFilters = {
@ComponentScan.Filter(type = FilterType.REGEX,
pattern = "com.baeldung.config.*")})
public class Application {
//...
}
4. 导入配置与组件
Spring Boot 主要基于注解配置,但仍支持导入现有配置(注解/XML)。
要让现有 @Configuration
或组件类生效,有两种方式:
- 将类移动到主类同包或子包
- 显式导入类
显式导入可通过 @ComponentScan
或 @Import
实现:
@SpringBootApplication
@ComponentScan(basePackages="com.baeldung.config")
@Import(UserRepository.class)
public class Application {
//...
}
官方推荐注解配置,但若已有 XML 文件不想转换,可用 @ImportResource
:
@SpringBootApplication
@ImportResource("applicationContext.xml")
public class Application {
//...
}
5. 迁移应用资源
Spring Boot 默认从以下位置加载资源文件:
/resources
/public
/static
/META-INF/resources
迁移时可:
- 将资源文件移至上述任一目录
- 通过
spring.resources.static-locations
自定义路径:
spring.resources.static-locations=classpath:/images/,classpath:/jsp/
6. 迁移应用属性
框架自动加载以下位置的 application.properties
或 application.yml
:
- 当前目录下的
/config
子目录 - 当前目录
- 类路径下的
/config
目录 - 类路径根目录
为避免手动加载属性,建议将属性文件迁移到上述位置(如 /resources
)。同时支持通过 application-{profile}.properties
加载环境特定配置。
Spring Boot 提供了大量预定义属性名用于配置应用行为。
7. 迁移 Spring Web 应用
7.1 Web 启动器
使用 spring-boot-starter-web
替换所有 Web 相关依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
添加后 Spring Boot 会:
✅ 自动添加 @EnableWebMvc
✅ 配置 DispatcherServlet
Bean
✅ 移除原有的 WebApplicationInitializer
类
若显式使用 @EnableWebMvc
,则 MVC 自动配置失效。该启动器还会自动配置:
- 静态资源服务(支持
/static
、/public
等目录) - JSON/XML 的
HttpMessageConverter
- 全局错误处理映射(
/error
)
7.2 视图技术
官方建议避免使用 JSP,改用模板引擎(如 Thymeleaf)。添加对应启动器即可:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
模板文件放在 /resources/templates
目录。
若坚持用 JSP,需配置视图解析路径:
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
7.3 内嵌 Web 服务器
添加 spring-boot-starter-tomcat
启用内嵌 Tomcat(默认端口 8080):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
也支持 Jetty 和 Undertow,只需替换对应依赖。
8. 迁移 Spring Security 应用
添加 spring-boot-starter-security
:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
默认会创建 "user" 用户(随机密码)并启用基础认证。通常需要自定义配置,保留原有 @EnableWebSecurity
类:
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// ...
}
}
9. 迁移 Spring Data 应用
根据实现添加对应启动器,如 JPA 使用 spring-boot-starter-data-jpa
:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
使用内存数据库(如 H2)时,添加依赖即可自动配置:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
使用 MySQL 等数据库时,需显式配置:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/myDb?createDatabaseIfNotExist=true
spring.datasource.username=user
spring.datasource.password=pass
Spring Boot 会自动配置:
✅ Hibernate 作为默认 JPA 实现
✅ transactionManager
Bean
10. 总结
本文覆盖了从 Spring 迁移到 Spring Boot 的常见场景。迁移体验高度依赖原有应用架构,但核心思路是:
- 用启动器简化依赖
- 用自动配置替代手动配置
- 保留业务逻辑代码
- 按需调整配置细节
实际迁移时,建议分模块逐步验证,避免一次性改动过大导致问题难以定位。