Spring Boot开发入门
Spring Boot概述
Spring Boot框架本身并不提供Spring框架的核心特性以及拓展功能,只是用于快速
敏捷的开发新一代基于Spring框架的应用。
SpringBoot的优点
(1)可快速构建独立的Spring应用
(2)直接嵌入Tomcat、Jetty和Undertow服务器
(3)通过依赖启动器简化构建配置
(4)自动化配置Spring和第三方库
(5)提供生产就绪功能
(6)极少的代码生成和XML配置
SpringBoot入门程序
使用Maven方式构建Spring Boot项目
初始化IDEA配置
(1)配置Maven
Maven下载地址:https://archive.apache.org/dist/maven/maven-3/
下载完后进行解压

配置环境变量

变量值为maven的位置
在Path中加入

新建一个文件夹maven-repository(Maven仓库,位置任意)。

打开maven下的conf文件夹下的setting.xml文件
增加一句代码
<localRepository>maven-repository文件位置</localRepository>

打开idea,点击右上角的点击Setting

配置Maven

勾选这三个框,可以进行自动导入

配置好之后点击Apply,点击OK
(2)JDK初始化配置


之后点击Apply和OK
创建Maven项目
点击Create New Project创建项目

左侧选择maven选项

点击next

点击finish
添加Spring Boot相关依赖
打开pom.xml文件,在该pom.xml文件中添加构建Spring Boot项目和Web场景开发对应的依赖
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.6.6</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
编写主程序启动类
在项目的java目录下创建一个名称为com.itheima的包,创建一个主程序启动类
ManualChapter01Application
package com.itheima;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ManualChapter01Application {
public static void main(String [] args){
SpringApplication.run(ManualChapter01Application.class,args);
}
}
@SpringBootApplication注解式Spring Boot框架的核心注解用于表明ManualChapter01Application类是Spring Boot项目的主程序启动类。
创建一个用于Web访问的Controller
在com.itheima包下创建一个controller的包,在该包下创建一个名称为HelloController的请求控制类
package com.itheima.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(){
return "hello Spring boot";
}
}
@RestController注解是一个组合注解等同于@Controller和@ResponseBody两个注解结合使用的效果
@GetMapping(“/hello”)注解等同于@RequestMapping(method=RequestMethod.GET)
运行项目
运行ManualChapter01Application项目,项目成功启动后在访问http://localhost:8080/hello

使用Spring Intializer方式创建SpringBoot项目
(1)创建Spring Boot项目
选择Create New Project新建项目,在弹出的New Project项目中选择Spring Inltializr

点击next

点击Finish
Spring Intializer方式构建的Spring Boot项目会默认生成项目启动类,存放前端的静态资源和页面文件夹,编写项目配置的配置文件以及进行项目测试的测试类
打开DemoApplication和pom.xml可以看出已经自动写好了
DemoApplication

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>..................00
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!--Maven打包工具-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
(2)创建一个用于Web 访问的Controller
在com.example.demo下创建一个controller的包,编写helloController
package com.example.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(){
return "first String Boot";
}
}
(3)启动运行DemoApplication,访问地址http://localhost:8080/hello

单元测试与热部署
单元测试
在实际开发中,每当完成一个功能接口或业务方法后,通常都会借助单元测试验证该功能是否正确。单元测试的步骤如下:
(1)添加spring-boot-starter-test测试依赖启动器
在项目pom.xml文件中添加spring-boot-starter-test测试启动器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
如果是使用Spring Initializr方式创建Spring Boot项目,会自动导入spring-boot-starter-test测试依赖启动器
(2)编写单元测试类和测试方法
在项目中添加测试依赖启动器后,可以编写Spring Boot项目中相关方法对应的单元测试
在src .test.java目录下自动创建与项目主程序启动类对应的单元测试类。
DemoApplicationTests:
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
class DemoApplicationTests {
@Test
void contextLoads() {
}
@Autowired
private HelloController helloController;
@Test
public void HelloControllerTest(){
String hello=helloController.hello();
System.out.println(hello);
}
}
其中的@SpringBootTest注解用于标记测试类,并加载项目的上下文环境ApplicationContext,@RunWith注解是一个类运行器,用于加载SpringBoot测试注解@SpringBootTest。
(3)运行DemoApplicationTests

热部署
在研发中,通常会对一段业务代码不断修改测试,修改之后玩玩需要重启服务需要加载很久才能启动成功,进行项目热部署,可以无须手动重启项目。热部署的步骤如下:
(1)在pom.xml添加spring-boot-devtools热部署依赖启动器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
(2)IDEA热部署设置,选择IDEA工具界面的File下的setting选项,打开compiler面板设置页面

选择Build.Excution.Deployment下的Compiler选项,勾选Build projectautomatically选项设置为自动编译。
在任意页面使用组合键“Ctrl+Shift+Alt+/”打开Maintenance选项框

勾选compiler.automake.allow.when.app.running勾选其中的值将程序运行方式设置为自动编译
(3)运行DemoApplication,访问http://localhost:8080/hello。

修改HelloController中的输出语句,然后进行保存

Spring Boot原理分析
Spring Boot依赖管理
在Spring Boot入门程序中,项目pom.xml有两个核心依赖,分别是spring-boot-starter-web和spring-boot-starter-parent
spring-boot-starter-parent依赖
spring-boot-starter-parent依赖代码如下
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
使用“Ctrl+鼠标左键”进入并查看spring-boot-starter-parent底层源代码,
底层源代码有一个父依赖spring-boot-dependcies代码如下
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.6.6</version>
</parent>
该文件是通过< property>标签对一些常用技术框架的依赖文件进行统一版号管理。
spring-boot-starter-web依赖
spring-boot-starter-web依赖主要作用是进行版本统一管理。
核心代码如下
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.6.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
<version>2.6.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.6.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.18</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.18</version>
<scope>compile</scope>
</dependency>
</dependencies>
spring-boot-starter-web依赖主要启动器主要作用是提供场景所需的底层所有依赖文件,对Web开发场景所需的依赖文件进行了统一管理。
Spring Boot提供了许多的启动器,我们可以打开Spring Boot的官方文档搜索“starter”关键字查询场景依赖启动器。
Spring Boot自动配置
Spring Boot的启动入口是@SpringBootApplication注解标注类中的main()方法,@SpringBootApplication能够扫描Spring组件并自动配置Spring Boot。
@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 {
.......
}
@SpringBootApplication是一个组合注解包含了@SpringBootConfiguration、@ComponentScan、@EnableAutoConfiguration三个核心注解
@SpringBootConfiguration
@SpringBootConfiguration注解表示SpringBoot配置类,查看@SpringBootConfiguration源码为
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
@Indexed
public @interface SpringBootConfiguration {
@AliasFor(
annotation = Configuration.class
)
boolean proxyBeanMethods() default true;
}
@SpringBootConfiguration注解nebula有一个核心注解@Configuration,该注解是Spring提供的,表示当前类是一个配置类,可以被组件扫描器相同,所以@SpringBootConfiguration与@Configuration作用相同,都是标识一个组件扫描的配置类,只不过是@SpringBootConfiguration是被Spring Boot进行了重新封装命名而已。
@EnableAutoConfiguration
@EnableAutoConfiguration注解表示开启自动配置功能,该注解是Spring Boot框架最重要的注解,也是实现自动化的注解,核心代码为:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
Class<?>[] exclude() default {};
String[] excludeName() default {};
}
@EnableAutoConfiguration是一个组合注解,主要包含了@AutoConfigurationPackage和@import两个核心注解。
@AutoConfigurationPackage
@AutoConfigurationPackage核心代码如下

@AutoConfigurationPackage注解功能是由@import注解实现的,作用是想容器导入注册的所有组件,导入组件由Registar决定。核心代码为

@AutoConfigurationPackage主要作用是获取项目主程序的启动类所在的根目录,从而执行后续组件扫描器要扫描的包的位置。
@Import({AutoConfigurationImportSelector.class})
核心代码如下:

上述的getAutoConfigurationEntry主要作用是筛选出当前项目环境需要启动的自动配置类,从而实现当前项目运行所需要的自动配置环境。
@ComponentScan
@ComponentScan是一个组件包扫描器,用于指定将包中的注解类自动装配到Spring Bean容器中,
@ComponentScan注解具体扫描的包的根路径由Spring Boot项目主程序启动类所在包位置决定在扫描过程中由前面介绍的@AutoConfigurationPackage注解进行解析。
本文介绍了Spring Boot的基础知识和入门程序,包括使用Maven和Spring Initializer创建项目,讲解了如何编写主程序启动类、Controller,以及如何运行项目。此外,还探讨了单元测试、热部署的配置方法,以及Spring Boot的依赖管理和自动配置原理。

4313

被折叠的 条评论
为什么被折叠?



