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;
@SpringBootApplication
public 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;
@Controller
public 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;
@SpringBootApplication
public 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
中。