pom. xml 探究
- 下面来简单分析一下《Spring Boot 入门》中的 "helloWorld" 应用中的 pom.xml 配置文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>www.wmx.com</groupId> <artifactId>helloWorld</artifactId> <version>1.0-SNAPSHOT</version> <!-- 为了减少引用,于是Spring将必须的引用都封装在了spring-boot-starter-parent中 只需要继承下来即可,这些配置都可以去Spring官网找到--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <dependencies> <!-- 因为应用会是一个web应用,所以引用web包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <!-- spring-boot-maven-plugin插件:可以将应用打包成一个可执行的jar包;--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
spring-boot-starter-parent
- 自己的 Spring Boot 项目继承 spring-boot-starter-parent 项目
<!-- 为了减少引用,于是Spring将必须的引用都封装在了spring-boot-starter-parent中 只需要继承下来即可,这些配置都可以去Spring官网找到--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent>
- 而 spring-boot-starter-parent 项目继承了 spring‐boot‐dependencies
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.5.9.RELEASE</version> <relativePath>../../spring-boot-dependencies</relativePath> </parent>
- spring‐boot‐dependencies 真正管理 Spring Boot 应用里面的所有版本依赖关系,它的pom.xml文件也是最大的,有2999行,是 Spring Boot 的版本仲裁中心;
spring-boot-starter-web
<dependencies> <!-- 因为应用会是一个web应用,所以引用web包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
- spring-boot-starter-web 是 spring-boot场景启动器(spring-boot-starter)之一,导入了web模块正常运行所依赖的组件;
- Spring Boot 将所有的功能场景都抽取出来,做成一个个starters(启动器),只需要在项目里面引入这些starter,相关场景的所有依赖都会导入进来,要用什么功能就导入什么场景的启动器。
- Spring Boot 1.5.9官方文档:https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/reference/htmlsingle/#using-boot-starter,可以看到所有的启动器
- Spring Boot 所有版本的官方文档:https://docs.spring.io/spring-boot/docs/
- 这些starters几乎涵盖了javaee所有常用场景,Spring Boot对这些场景依赖的jar也做了严格的测试与版本控制
主程序类
package main; /** * Created by Administrator on 2018/7/9 0009. * 应用入口 */ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @SpringBootApplication :表示当前类为主程序类,这是一个Spring Boot应用 */ @SpringBootApplication public class MainApp { public static void main(String[] args) { /** 启动Spring应用 * 第一个参数"MainApp.class"类上面必须是"@SpringBootApplication"注解修饰的类 * 写法是约定的,就像启动Java FX应用一样*/ SpringApplication.run(MainApp.class, args); } }
@SpringBootApplication
- @SpringBootApplication:标注在某个类上说明这个类是SpringBoot的主配置类,SpringBoot 就应该运行这个类的main方法来启动SpringBoot应用;
- @SpringBootApplication 注解内容如下所示
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan( excludeFilters = { @Filter( type = FilterType.CUSTOM, classes = {TypeExcludeFilter.class} ), @Filter( type = FilterType.CUSTOM, classes = {AutoConfigurationExcludeFilter.class} )} ) public @interface SpringBootApplication {
@SpringBootConfiguration
- @SpringBootConfiguration:是 Spring Boot的配置类,标注在某个类上,表示这是一个Spring Boot的配置类;
- @SpringBootConfiguration 内容如下所示
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Configuration public @interface SpringBootConfiguration { }
- @Configuration :标识配置类、配置文件;
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface Configuration { String value() default ""; }
@EnableAutoConfiguration
- @EnableAutoConfiguration:开启自动配置功能,以前需要手动配置的东西,现在Spring Boot帮我们自动进行配置
- @EnableAutoConfiguration 是核心注解;
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @AutoConfigurationPackage @Import({EnableAutoConfigurationImportSelector.class}) public @interface EnableAutoConfiguration {
- @Import 导入所有的自动配置场景
- @AutoConfigurationPackage 定义默认的包扫描规则,程序启动扫描加载主程序类所在的包以及下面所有子包的组件;
- EnableAutoConfigurationImportSelector:导入哪些组件的选择器,将所有需要导入的组件以全类名的方式返回,这些组件就会被添加到容器中;会给容器中导入非常多的自动配置类(xxxAutoConfiguration);就是给容器中导入这个场景需要的所有组件,并配置好这些组件;
- Spring Boot 在启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration 指定的值,将这些值作为自动配置类导入到容器中,自动配置类就生效,帮我们进行自动配置工作;以前我们需要手动配置的东西,自动配置类都帮我们配置好
- J2EE的整体整合解决方案和自动配置都在spring-boot-autoconfigure-1.5.9.RELEASE.jar;
扫描二维码关注公众号,回复:
2109918 查看本文章