一、为什么需要Spring Boot?
1.1 传统Spring开发的痛点
XML配置爆炸:一个中等规模的项目,XML配置文件动辄几百上千行------web.xml、applicationContext.xml、spring-mvc.xml......新人接手项目,光看配置文件就要花半天。
重复劳动:每个新项目都要重复配置DispatcherServlet、DataSource、TransactionManager,而这些配置90%都是一样的。
依赖管理混乱:Spring MVC该引哪些jar?版本号是多少?Spring + Hibernate会不会冲突?一个版本升级可能导致一串依赖跟着改。
1.2 Spring Boot的解决方案
Spring Boot作为"Spring脚手架",彻底改变了这一局面:快速创建独立运行的Spring项目,与主流框架无缝集成使用嵌入式的Servlet容器(如Tomcat),应用无需打成WAR包。
Starters自动依赖和版本控制,告别依赖冲突,大量的自动装配,简化开发,也可按需修改默认值-无需配置XML,无代码生成,开箱即用,与云计算的天然集成,准生产环境的运行时监控。
二、环境准备
2.1 环境约束
|-------------|---------------------------|-------------------------------|
| 工具 | 版本要求 | 说明 |
| JDK | 1.8及以上 | 本文使用 java version "1.8.0_112" |
| Maven | 3.3及以上 | 本文使用 Apache Maven 3.3.9 |
| IDE | IntelliJ IDEA 2017+ / STS | 本文使用 IDEA 2017.3.3 x64 |
| Spring Boot | 2.0.2.RELEASE | 统一环境,Spring 5 |
温馨提示:版本不要求完全一致,但建议保持Spring Boot主版本统一,避免版本冲突。
2.2 Maven配置
在Maven的settings.xml配置文件的profiles标签中添加以下内容,统一JDK编译版本:
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
2.3 IDEA设置
在IDEA中,依次进入 File → Settings → Build, Execution, Deployment → Build Tools → Maven,确认Maven home directory和settings.xml配置正确。
三、HelloWorld实战
3.1 创建Maven工程
创建一个Maven工程,打包方式选择jar。
3.2 导入Spring Boot依赖
在pom.xml中添加以下配置:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
核心解析:spring-boot-starter-parent是Spring Boot的版本仲裁中心,所有依赖的版本都由它统一管理,因此我们导入依赖时默认不需要写版本号。spring-boot-starter-web是Web场景启动器,帮我们导入了Web模块正常运行所需的所有组件。
3.3 编写主程序类
创建HelloWorldMainApplication.java:
/**
* @SpringBootApplication 标注一个主程序类,说明这是一个Spring Boot应用
*/
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
// Spring应用启动起来
SpringApplication.run(HelloWorldMainApplication.class, args);
}
}
3.4 编写Controller
创建HelloController.java:
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello() {
return "Hello World!";
}
}
3.5 运行测试
直接运行主程序类的main方法,在浏览器中访问 http://localhost:8080/hello,即可看到"Hello World!"输出。
3.6 简化部署
在pom.xml中添加Spring Boot Maven插件,可将应用打包成一个可执行的JAR包:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
执行mvn clean package打包后,使用java -jar xxx.jar即可直接运行。
四、核心注解深度解析
4.1 @SpringBootApplication
@SpringBootApplication是一个组合注解,它由三个核心注解组成:
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class)
})
public @interface SpringBootApplication {
}
-
@SpringBootConfiguration:标注在某个类上,表示这是一个Spring Boot的配置类,等价于@Configuration,配置类本身也是容器中的一个组件。
-
@EnableAutoConfiguration:开启自动配置功能的"总开关"。以前需要手动配置的东西,Spring Boot会帮我们自动配置好。
-
@ComponentScan:自动扫描主配置类所在包及其所有子包下的组件,并将其注册到Spring容器中。
4.2 @EnableAutoConfiguration的工作原理
@EnableAutoConfiguration内部通过@Import(AutoConfigurationImportSelector.class)导入组件。AutoConfigurationImportSelector会利用SpringFactoriesLoader.loadFactoryNames()方法,扫描所有JAR包类路径下的META-INF/spring.factories文件。
在spring.factories中,配置了所有需要被加载的自动配置类(xxxAutoConfiguration):
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
这些自动配置类被加载到容器后,会根据@Conditional系列注解的条件判断来决定是否生效。有了这些自动配置类,我们无需手动编写配置,就能获得完整的Web开发环境。