一. SpringBoot 环境准备
- jdk1.8:Spring Boot 推荐jdk1.7及以上;
- maven3.x:maven 3.3以上版本;
- IntelliJIDEA2019:IntelliJ IDEA 2019.1.2 x64
- SpringBoot 1.5.9.RELEASE:1.5.9;
1. maven设置
在D:\java\apache-maven-3.6.0\conf文件夹下的settings.xml文件中,添加如下代码
<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. IDEA设置
二. SpringBoot HelloWorld
实现一个功能:浏览器发送hello请求,服务器接受请求并处理,响应Hello World字符串
1. 创建一个maven工程
2. 导入Spring Boot相关的依赖
<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>
3. 编写一个主程序,启动Spring Boot应用
package com.santiago;
/**
* @SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用
*/
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldMainApplication.class, args);
}
}
4. 编写相关的Controller、Service
package com.santiago.controller;
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "hello world.";
}
}
5. 运行主程序测试
直接运行主程序的main函数
6. 简化部署
导入插件
<!-- 这个插件,可以将应用打包成一个可执行的jar包;-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
导入插件后,将这个应用打成jar包
将程序打包成jar包后,使用dos命令进入到jar包所在文件夹,直接使用java -jar的命令进行执行;
三. HelloWorld探究
1. pom文件
1.1 父项目
首先引用了父项目
<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
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
点击进去发现
原来他来真正管理Spring Boot应用里面的所有依赖版本
spring-boot-starter-parent是Spring Boot的版本仲裁中心,以后我们导入依赖默认是不需要写版本;
(没有在dependencies里面管理的依赖自然需要声明版本号)
2. 启动器
pom文件中导入的第一个依赖为
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
spring-boot-starter:spring-boot场景启动器
spring-boot-starter-web:帮我们导入了web模块正常运行所依赖的组件
查看Spring Boot的官方文档,发现Spring Boot将所有的功能场景都抽取出来,做成一个个的starters(启动器),只需要在项目里面引入这些starter相关场景的所有依赖都会导入进来。要用什么功能就导入什么场景的启动器
2. 主程序类,主入口类
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldMainApplication.class, args);
}
}
@SpringBootApplication: Spring Boot应用标注在某个类上说明这个类是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 {
我们一一来分析
2.1 @SpringBootConfiguration
@SpringBootConfiguration:Spring Boot的配置类;标注在某个类上,表示这是一个Spring Boot的配置类;
点开@SpringBootConfiguration
,其代码如下所示
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
}
其中包含**@Configuration**注解,这是spring最底层的注解,用来标注在配置类上。
点开@Configuration
,其代码如下所示
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
String value() default "";
}
其中包含**@Configuration**,标志着配置类其实也是容器中的一个组件。
2.2 @EnableAutoConfiguration
@EnableAutoConfiguration:开启自动配置功能。
以前我们需要配置的东西,Spring Boot帮我们自动配置;@EnableAutoConfiguration告诉SpringBoot开启自动配置功能;这样自动配置才能生效;
点开@EnableAutoConfiguration
,其代码如下所示
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({EnableAutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
Class<?>[] exclude() default {};
String[] excludeName() default {};
}
2.2.1 @AutoConfigurationPackage
@AutoConfigurationPackage:自动配置包
点开@AutoConfigurationPackage
,其代码如下所示
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({Registrar.class})
public @interface AutoConfigurationPackage {
}
发现其中包含@Import({Registrar.class})
,这是Spring的底层注解@Import,给容器中导入一个组件;由Registrar.class来指定需要导入哪些组件
点开Registrar
,其代码如下所示,并打断点进入调试
其中metadata
包含注解的元信息
在PackageImport(metadata)
上右击,选择计算表达式
于是就得到了包名的信息,即为com.santiago
@AutoConfigurationPackage将主配置类(@SpringBootApplication标注的类)的所在包(本例中为
com.santiago
)及下面所有子包里面的所有组件扫描到Spring容器
2.2.2 @Import({EnableAutoConfigurationImportSelector.class})
@Import({EnableAutoConfigurationImportSelector.class}):给容器中导入组件
EnableAutoConfigurationImportSelector:导入哪些组件的选择器;
将所有需要导入的组件以全类名的方式返回;这些组件就会被添加到容器中;
点开EnableAutoConfigurationImportSelector
public class EnableAutoConfigurationImportSelector extends AutoConfigurationImportSelector {
public EnableAutoConfigurationImportSelector() {
}
protected boolean isEnabled(AnnotationMetadata metadata) {
return this.getClass().equals(EnableAutoConfigurationImportSelector.class) ? (Boolean)this.getEnvironment().getProperty("spring.boot.enableautoconfiguration", Boolean.class, true) : true;
}
}
发现其继承自AutoConfigurationImportSelector
,点开AutoConfigurationImportSelector
,并加入断点,查看到底其导入了哪些组件
step over 到如下位置,得到一个configurations的列表,包含96个组件
**@Import({EnableAutoConfigurationImportSelector.class})**的最终作用:会给容器中导入非常多的自动配置类(xxxAutoConfiguration);就是给容器中导入这个场景需要的所有组件,并配置好这些组件;
有了自动配置类,免去了我们手动编写配置注入功能组件等的工作;
我们看到这个configurations列表是由getCandidateConfigurations(annotationMetadata, attributes)
方法得到的
点开getCandidateConfigurations()
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.");
return configurations;
}
其实是调用了loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader())
方法
点开loadFactoryNames()
方法,发现如下
Spring Boot在启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值,将这些值作为自动配置类导入到容器中,自动配置类就生效,帮我们进行自动配置工作;以前我们需要自己配置的东西,自动配置类都帮我们
四. 使用Spring Initializer快速创建Spring Boot项目
第一步,新建项目
第二步,填写各种名称
第三步,选择需要导入的模块。在这一步我只导入了web模块
修改项目名,联网即可完成
默认生成的Spring Boot项目;
- 主程序已经生成好了,我们只需要我们自己的逻辑
- resources文件夹中目录结构
- static:保存所有的静态资源; js css images;
- templates:保存所有的模板页面;(Spring Boot默认jar包使用嵌入式的Tomcat,默认不支持JSP页面);可以使用模板引擎(freemarker、thymeleaf);
- application.properties:Spring Boot应用的配置文件;可以修改一些默认设置;