1.创建一个普通的maven项目,项目名为boot-learnning
2.在pom.xml添加parent依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version></parent>
3.在pom.xml中的dependencies中添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency>
由于在
parent中已经指定了版本,所以在dependencies中关于spring-boot的依赖就不需要版本了,如果要引入其他的,仍然需要指定版本
4.编写main方法程序,用于启动应用
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class App { public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}直接执行
main方法程序,就启动了一个web引用。spring-boot中默认内嵌了tomcat,所以只要执行主程序,就启动了内嵌的tomcat,并默认监听8080端口
5.编写一个controller,用于客户端访问
import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;
@Controllerpublic class IndexController {
@RequestMapping("/index")
@ResponseBody
public String index() { return "Spring boot index Controller";
}
}浏览器中访问http://localhost:8080/index,我们就能在浏览器中看到返回的内容
6.在pom.xml中添加spring-boot编译插件
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins></build>
7.编译打包
mvn clean compile package
编译打包完成后,在项目的target目录下,就会生成一个jar文件
8.部署启动
java -jar boot-learnning-0.0.1-SNAPSHOT.jar
启动后,在浏览器中访问http://localhost:8080/index,在浏览器中,就能看到controller返回的内容
9.依赖探究
我们在搭建环境的时候添加了父级依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version></parent>
而在spring-boot-starter-parent中,也有一个父级依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.0.0.RELEASE</version> <relativePath>../../spring-boot-dependencies</relativePath></parent>
在spring-boot-dependencies中,properties定义了我们常用的的组件版本,帮我们管理spring-boot中需要的所有依赖
那么在我们开发的过程中,我们在导入spring-boot-dependencies中已经声明的依赖库时,就不需要指定版本了,如果在spring-boot-dependencies没有指定,就需要我们来指定版本了
在dependencies,我们依赖了spring-boot-starter-web
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency>
spring-boot-starter是spring-boot的场景启动器,帮我们管理导入了web模块正常运行所需要的依赖库spring-boot将所有的功能场景都抽取出来,做成一个个的starter(启动器),只需要在项目中引入这些starter,相关场景的所有依赖都会自动导入,我们需要使用什么功能,就导入相应的场景的启动器依赖即可
10.主程序探究
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class App { public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}@SpringBootApplication 标注在某个类上,说明这个类是spring-boot应用的主配置类,spring-boot就会通过这个类来启动spring-boot应用
SpringApplication的run方法中,第一个参数,就是一个使用@SpringBootApplication的类,而这个类也可以不是当前main方法所在的类
11.@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标注的类为一个spring-boot应用的配置类@Configuration标注一个类,使其成为一个配置类@EnableAutoConfiguration开启自动配置,表示原来我们使用spring时,需要手动配置的东西,现在由spring-boot帮我们自动配置,这个注解告诉spring-boot开启自动配置功能,是的自动配置功能生效
配置类 与 配置文件是一样的,而配置类也是容器中的一个组件
12.@EnableAutoConfiguration 探究
@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documented@Inherited@AutoConfigurationPackage@Import(AutoConfigurationImportSelector.class)public @interface EnableAutoConfiguration {@AutoConfigurationPackage自动配置包,它使用@Import(AutoConfigurationPackages.Registrar.class)来完成所需的功能@Import是spring的一个底层注解,给容器中导入一个组件;导入的组件由AutoConfigurationPackages.Registrar.class类指定AutoConfigurationPackages.Registrar.class将主配置类(@SpringBootApplication标注的类)的所在包,以及下面的所有子包里面的所有组件扫描到spring容器中@Import(AutoConfigurationImportSelector.class)中的AutoConfigurationImportSelector.class是一个确定导入哪些组件的选择器,将所有需要的组件以全类名的方式返回,然后将这些组件全部添加到容器中,还会给容器中导入非常多的自动配置类,即向容器中导入各个场景需要的所有组件,并且自动配置好这些组件
13.自动配置类
自动配置类,免去了我们手动编写配置注入功能组件等工作
在AutoConfigurationImportSelector的selectImports方法中,调用了getCandidateConfigurations方法获取候选的配置文件,使用了.SpringFactoriesLoader.loadFactoryNames(Class<?> factoryClass, @Nullable ClassLoader classLoader),其中,第一个参数就是EnableAutoConfiguration.class,spring-boot应用在启动的时候,调用loadFactoryNames方法从类路径下的META-INF/spring.factories获取EnableAutoConfiguration指定的值,将这些值作为自动配置类,导入到容器中,自动配置类生效,帮我们进行自动配置工作,而这个文件存在于spring-boot-autoconfigure-2.0.0.RELEASE.jar中。
以前我们需要自己手动配置的东西,自动配置类都帮我们做了,spring-boot底层中,关于spring-framework中的东西,并没有缺少或者省略,只不过是由自动配置类帮我们设置了,J2EE的整体解决方案的自动配置类,都在spring-boot-autoconfigure中。
作者:心扬
链接:https://www.jianshu.com/p/5899a4c5968d