入门
-
@RestContrller :此注解标记的类下的 所有 方法均会返回一个 domain 对象以代替视图@Controller、@ResponseBody 的缩写使用 Jackson2 以及 MappingJackson2HttpMessageConverter 类自动转换对象为 JSON
-
@SpringBootApplication:快捷注释,包含以下内容@Configuration:将该类标记为上下文 bean 对象的源@EnableAutoConfiguration:@ComponentScan
Spring Boot ?
-
Spring Boot 基于 spring framework之上,可以整合所有spring 生态的技术
-
快速构建生产级别的 spring 应用
-
spring boot 是整合 spring 技术栈的一站式框架、脚手架
系统要求
Name |
Version |
Java |
JDK 8+ |
Maven |
3.3+ |
Gradle |
6 (6.3 or later). 5.6.x is also supported but in a deprecated form |
Name |
Servlet Version |
Tomcat 9.0 |
4.0 |
Jetty 9.4 |
3.1 |
Undertow 2.0 |
4.0 |
IDEA 的支持
-
编写 application.properties 是智能提示
-
应用启动后的实时监控:可以实时监控到当前容器中的所有 bean
第一个 Spring boot 应用
创建 pom
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
-
因为我们需要开发 web 应用测试,所有选择 starter-web
-
spring-boot-starter-parent 工程设置了很多参数、插件、资源打包设置等
-
修改 java 版本:<properties> <java.version>1.8</java.version> </properties>因为 父工程已经使用 ${java.version} 设置了 java 的编译版本目前测试无法使用 maven-compiler-3.8 编译 jdk 11 版本的项目,原因未知,切换为 jdk 8 即正常
编写测试代码
-
编写入口@SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
-
编写 Controller 类@RestController public class HelloController { @RequestMapping("/") String home() { return "Hello World!"; } }
测试运行
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.3)
2021-02-24 23:08:21.251 INFO 3240 --- [ main] per.fkrobin.study.App : Starting App using Java 1.8.0_281 on DESKTOP-SKF5ODM with PID 3240 (D:\Projects\javaProjects\SpringBootStudy\HelloSpringBoot\target\classes started by 22136 in D:\Projects\javaProjects\SpringBootStudy)
2021-02-24 23:08:21.254 INFO 3240 --- [ main] per.fkrobin.study.App : No active profile set, falling back to default profiles: default
2021-02-24 23:08:21.824 INFO 3240 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
浏览器打开 http://localhost:8080
可执行的 Jar
- 添加 插件<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
- 执行 mvn clean package
- java -jar xxxxx.jar 文件,若一切正常,结果将是相同的
自动配置
Maven 依赖管理
每一个 Spring boot 应用都会有一个 parent 父工程
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
</parent>
spring-boot-starter-parent的父工程
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.4.3</version>
</parent>
spring-boot-dependencies这个工程中定义几乎所有的 常用工具包版本及其依赖,如 spring-boot-starter-web :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.3</version>
</dependency>
所以我们使用 spring-boot-starter-web 不再需要书写版本号,如果需要更换版本号,可以查看 spring-boot-dependencies 中对应依赖的版本书写定义,然后在自己项目中 进行重新赋值
- IDEA pom 文件中 右键选中 Diagrams->Show Diagrams 可以查看依赖树
常用组件自动配置
- 自动配好SpringMVC
- 引入SpringMVC全套组件
- 自动配好SpringMVC常用组件(功能)
- 自动配好Web常见功能,如:字符编码问题、DispacherServlet、multipart文件上传等
- SpringBoot帮我们配置好了所有web开发的常见场景
- 默认的包结构
- 主程序所在包及其下面的所有子包里面的组件都会被默认扫描进来
- 无需以前的包扫描配置
- 想要改变扫描路径,@SpringBootApplication(scanBasePackages= "com.atguigu" ),或者使用@ComponentScan 指定扫描路径
自动配置原理
SpringApplication
在此类初始化时,就会去 spring-boot、
spring-boot-auto-configure 加载 spring-factories 文件,文件中定义了 所有的 Spring 场景下的工厂,如 各种 xxxAutoConfiguration 类、Listener、Filter 等等,并缓存下来。在接下来调用 run 运行时,会根据条件注入、Filter 等过滤掉其中不需要用到的 工厂 、bean。下面是主要代码
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
configureIgnoreBeanInfo(environment);
Banner printedBanner = printBanner(environment);
context = createApplicationContext();
context.setApplicationStartup(this.applicationStartup);
prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
// 实例化 bean 到容器之中的过程都是在此完成的
refreshContext(c