背景
- Spring早已經成為企業級開發的業界標准,尤其是Spring Boot 2.0、Spring 5發布后,Spring的生態系統引領了技術架構發展的潮流,對於Java開發人員,深入掌握Spring全家桶的各種框架應用及必要的底層原理知識,是一件非常重要的事情。
學習路線圖
Spring的基礎知識
什么是Spring
Spring的核心是提供了一個容器(container),通常稱為Spring應用上下文(Spring application context),它們會創建和管理應用組件。這些組件也可以稱為bean,會在Spring應用上下文中裝配在一起,從而形成一個完整的應用程序。
將bean裝配在一起的行為是通過一種基於依賴注入(dependency injection,DI)的模式實現的。此時,組件不會再去創建它所依賴的組件並管理它們的生命周期,使用依賴注入的應用依賴於單獨的實體(容器)來創建和維護所有的組件,並將其注入到需要它們的bean中。通常,這是通過構造器參數和屬性訪問方法來實現的。
Spring框架核心模塊
SpringBoot
在歷史上,一般通過兩種配置方式為Spring應用上下文提供Bean
- 使用一個或多個XML文件描述bean
- 使用@Configuration注解會告知Spring這是一個配置類
隨着Spring Boot 2.x的引入,Spring自動配置的能力已經大大加強,Spring Boot能夠基於類路徑中的條目、環境變量和其他因素合理猜測需要配置的組件並將它們裝配在一起。Java程序員盡可能多地使用Spring Boot,只有在必要的時候才使用顯式配置。
第一個Spring應用DEMO
- 在IntelliJ IDEA中創建新項目
- 通過地址為https://start.spring.io/初始化項目;
- 指定項目通用信息:
- 選擇項目Starter:
- 生成的項目結構:
- maven規范
<?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.3.1.RELEASE</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>
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
可參見本人博客《Maven POM( Project Object Model,項目對象模型 )》及《一圖說清maven常見要素》這兩篇文章。
- 入口類
/**
* SpringBoot應用
*/
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
// 運行應用
SpringApplication.run(DemoApplication.class, args);
}
}
@SpringBootApplication是一個組合注解,它組合了3個其他的注解。
- @SpringBootConfiguration:將該類聲明為配置類。盡管這個類目前還沒有太多的配置,但是后續我們可以按需添加基於Java的Spring框架配置。這個注解實際上是@Configuration注解的特殊形式。
- @EnableAutoConfiguration:啟用Spring Boot的自動配置。我們隨后會介紹自動配置的更多功能。就現在來說,我們只需要知道這個注解會告訴SpringBoot自動配置它認為我們會用到的組件。
- @ComponentScan:啟用組件掃描。這樣我們能夠通過@Component@Controller、@Service這樣的注解聲明其他類,Spring會自動發現它們並將它們注冊為Spring應用上下文中的組件。
8. 測試類
// SpringBoot測試
@SpringBootTest
class DemoApplicationTests {
// 測試方法
@Test
void contextLoads() {
}
}
- 程序啟動
編寫自己的第一個SpringMVC例子
- 第一個Controller
index()是一個簡單的控制器方法。它帶有@GetMapping注解,表明如果針對“/”發送HTTP GET請求,那么這個方法將會處理請求。該方法所做的只是返回String類型的index值,該控制器方法中還通過Spring自動注入IndexService服務組件,及調用服務組件方法。
/**
* 第一個SpringMVC程序--Controller層
*
* @author zhuhuix
* @date 2020-07-02
*/
@Controller
public class IndexController {
// Spring注入服務組件
@Autowired
private IndexService indexService;
@GetMapping("/")
public String index(Model model) {
String index = indexService.getIndex();
model.addAttribute("index", index);
// 返回視圖 即index.html
return "index";
}
}
- 第一個Service
getIndex()是一個簡單的服務方法。該方法所做的只是返回String類型的index值,該服務組件供控制層調用。
/**
* 第一個SpringMVC程序--Service層
* * @author zhuhuix
* @date 2020-07-02
*/
@Service
public class IndexService {
public String getIndex() {
return "hello world!!!";
}
}
- 第一個View
-- 使用Thymeleaf模板引擎
### application.properties
###ThymeLeaf配置
spring:
thymeleaf:
#模板的模式,支持 HTML, XML TEXT JAVASCRIPT
mode: HTML5
#編碼 可不用配置
encoding: UTF-8
#內容類別,可不用配置
content-type: text/html
#開發配置為false,避免修改模板還要重啟服務器
cache: false
#配置模板路徑,默認是templates,可以不用配置
prefix: classpath:/templates
-- 定義index.html視圖層
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" >
<head>
<meta charset="UTF-8"/>
<title>Title</title>
</head>
<body>
<p th:text="${index}" />
</body>
</html>
- 運行測試
嘗試使用Spring Boot DevTools
•代碼變更后應用會自動重啟;
•當面向瀏覽器的資源(如模板、JavaScript、樣式表)等發生變化時,會自動刷新瀏覽器
- pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>runtime</scope>
</dependency>
...
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--fork : devtools生效必須設置成true -->
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
- idea設置
-- 需勾選Build project automaticallty
-- ctrl+alt+shift+/ :Registry 中第一項必須打勾
回顧總結
- Spring核心框架:Spring核心框架是Spring領域中一切的基礎。它提供了核心容器和依賴注入框架。
- Spring Boot:Spring Boot構建在Spring之上,通過簡化依賴管理、自動配置和運行時洞察,使Spring更加易用;
- Spring MVC:我們通過SpringBoot初始化生成的框架上加入Controller,Service,View的分層,編寫了第一個Spring MVC程序,並成功運行。
- 使用Idea開發環境,安裝Spring Boot DevTools並進行配置,提高了開發效率。