1 簡介
1.1 Spring5框架和基本概念
1.1.1 什么是注解
Java注解是附加在代碼中的?些元信息戴尸,?于?些?具在編譯粟焊、運?時進(jìn)?解析和使?,起到說明孙蒙、配置的功能,注解本質(zhì)上繼承 Annotation 接?项棠,我們可以通過反射獲取注解的相關(guān)信息,從?做些邏輯操作。
springboot???量使?了注解挎峦,@Controller 香追、@RestController 、@Service坦胶、@Autowired 等
1.1.2 什么是Spring框架
什么是Spring:輕量級的 DI / IoC 和 AOP 容器的開源框架:https://spring.io/projects/spring-framework
1.1.3 有啥好處:
管理創(chuàng)建和組裝對象之間的依賴關(guān)系, 加了spring注解的類會?動創(chuàng)建?個實例透典,加到IOC容器??,然后看哪?需要它顿苇,就?動賦值過去峭咒。
使?前:??創(chuàng)建
Controller -> Service -> Dao
UserControoler
private UserService userService = new UserService();
使?后:Spring創(chuàng)建,?動注?
Controller -> Service -> Dao
UserControoler
@Autowired
private UserService userService
2 快速入門
2.1 SpringCloud和SpringBoot的關(guān)系
單體應(yīng)?:
- 開發(fā)速度慢
- 啟動時間?
- 依賴龐?
微服務(wù):
- 易開發(fā)岖圈、理解和維護(hù)
- 獨?的部署和啟動
問題:
- 分布式系統(tǒng) --> 分布式事務(wù)問題
- 需要管理多個服務(wù) --> 服務(wù)治理
SpringCloud和SpringBoot的關(guān)系
- SpringCloud基于SpringBoot
2.2 SpringBoot2.X介紹和環(huán)境依賴
SpringBoot2.X介紹
- 官?:https://spring.io/projects/spring-boot
- GitHub地址:https://github.com/spring-projects/spring-boot
- 官??檔:https://spring.io/guides/gs/spring-boot/
相關(guān)軟件環(huán)境
- JDK1.8+以上
- Maven3.5+
- IDEA
- PostMan
2.3 SpringBoot2.X項?創(chuàng)建?具Spring Initializr
使?Spring Initializr創(chuàng)建SpringBoot2.X項?
- 常?的創(chuàng)建SpringBoot創(chuàng)建?式
- 本地創(chuàng)建讹语,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 http://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.4.2</version>
<relativePath/>
</parent>
<groupId>com.jackyan</groupId>
<artifactId>springbootdemo1</artifactId>
<version>1.0-SNAPSHOT</version>
<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>
- 在線創(chuàng)建 :https://start.spring.io/
下載之后解壓導(dǎo)入maven即可
最終自動生成的項目結(jié)構(gòu)如下:
2.4 SpringBoot2.X開發(fā)第?個JSON接?
- 什么是SpringBoot,有什么?
?個web開發(fā)框架
Servlet 蜂科、SpringMVC
可以簡化配置 - SpringBoot開發(fā)項?常?的交互使?形式
后端接?直接返回??顽决,現(xiàn)在?較少了
常?的傳統(tǒng)IT公司?較多短条,配合模板引擎,由后端??渲染返回
返回JSON數(shù)據(jù)才菠,主流場景
互聯(lián)?公司?的?較多茸时,?如微服務(wù)接?,前后端分離項?赋访,?機(jī)app等可都,基本都是通
過JSON交互 - 如何使?SpringBoot2.X開發(fā)JSON接?
@RestController注解
return ?個POJO對象 - 開發(fā)一個json接口
創(chuàng)建controller
com.jackyan.springbootdemo2.controller.TestController
@RestController
@RequestMapping("/app/v1/test")
public class TestController {
@RequestMapping("list")
public Object getList() {
Map<String, String> map = new HashMap<>();
map.put("1", "jackyan");
map.put("2", "daisy");
return map;
}
}
啟動項目
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.6.2)
2022-01-11 16:23:51.217 INFO 28676 --- [ main] c.j.s.Springbootdemo2Application : Starting Springbootdemo2Application using Java 1.8.0_291 on LAPTOP-IIK6CDN8 with PID 28676 (C:\Users\yangzhihua01\IdeaProjects\springbootdemo2\target\classes started by yangzhihua01 in C:\Users\yangzhihua01\IdeaProjects\springbootdemo2)
2022-01-11 16:23:51.220 INFO 28676 --- [ main] c.j.s.Springbootdemo2Application : No active profile set, falling back to default profiles: default
2022-01-11 16:23:51.867 INFO 28676 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-01-11 16:23:51.875 INFO 28676 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-01-11 16:23:51.875 INFO 28676 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.56]
瀏覽器訪問
2.5 使用postman工具
2.6 SpringBoot2.X常?注解
- @Controller 作?:?于標(biāo)記這個類是?個控制器,返回??的時候使?蚓耽;如果要返回JSON,則需要在接?上使?@ResponseBody才可以
- @RestController 作?:?于標(biāo)記這個類是?個控制器渠牲,返回JSON數(shù)據(jù)的時候使?,如果使?這個注解步悠,則接?返回數(shù)據(jù)會被序列化為JSON
所以:@RestController = @Controller+@ResponseBody - @RequestMapping 作?:路由映射签杈,?于類上做1級路徑;?于某個?法上做?路徑
- @SpringBootApplication 作?: ?于標(biāo)記是SringBoot應(yīng)?鼎兽,??包含多個?注解,即
@SpringBootApplication =@Configuration+@EnableAutoConfiguration+@ComponentScan - @Configuration: 主要標(biāo)注在某個類上答姥,?于spring掃描注?,?般結(jié)合@Bean使?
- @EnableAutoConfiguration: 啟?Spring的?動加載配置,?動載?應(yīng)?程序所需的所有Bean
- @ComponentScan:告訴spring掃描包的范圍,默認(rèn)是Applocation類所在的全部?包谚咬,可以指定其他包:@ComponentScan({"net.xdclass.package1","net.xdclass.package2"})
3 SpringBoot2.X開發(fā)規(guī)范
3.1 SpringBoot2.x?錄?件結(jié)構(gòu)
- 目錄
src/main/java:存放代碼
src/main/resources: application.properties
static: 存放靜態(tài)?件鹦付,?如 css、js择卦、image, (訪問?式 http://localhost:8080/js/main.js)
templates:存放靜態(tài)??jsp,html,tpl
config:存放配置?件,application.properties
同個?件的加載順序,靜態(tài)資源?件 Spring Boot 默認(rèn)會挨個從
META/resources > resources > static > public ??找是否存在相應(yīng)的資源敲长,如果有則直接返回,不在默認(rèn)加載的?錄互捌,則找不到
默認(rèn)配置:
spring.resources.static-locations = classpath:/META?INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
3.2 SpringBoot2.x啟動類位置
三種形式:
1潘明、當(dāng)啟動類和controller在同?類中時行剂,在該類上添加注解@Controller即可秕噪;
2、當(dāng)啟動類和controller分開時厚宰,啟動類要放在根?錄下腌巾,啟動類上只需要注解
@SpringBootApplication;
3铲觉、當(dāng)啟動類和controller分開時澈蝙,如果啟動類在?根?錄下,需要在啟動類中增加注解
@ComponentScan撵幽,并配置需要掃描的包名灯荧,如(basePackages = )
@ComponentScan(basePackages ={"net.jackyan.controller","net.jackyan.service"})
強(qiáng)烈推薦第?種?式,不然漏配置掃描包盐杂,項?龐?逗载,出現(xiàn)問題則難排查
3.3 SpringBoot2.X啟動?式
1哆窿、IDEA開發(fā)中啟動 - 本地開發(fā)中常?
2、外置Tomcat中啟動 - tomcat版本兼容問題復(fù)雜厉斟,微服務(wù)容器化部署復(fù)雜挚躯,接近淘汰
3、Jar?式打包啟動 - 官?推薦擦秽,?作中最常?
步驟:pom?件新增maven插件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
如果沒有加码荔,則執(zhí)?jar包 ,報錯如下
java -jar spring-boot-demo-0.0.1-SNAPSHOT.jar no main manifest attribute, in spring-boot-demo-0.0.1-SNAPSHOT.jar
打包感挥、啟動命令
構(gòu)建:mvn install
構(gòu)建跳過測試類 mvn install -Dmaven.test.skip=true
target?錄下有對應(yīng)的jar包就是打包后項?
進(jìn)到對應(yīng)的target?錄啟動 java -jar xxxxx.jar 即可
想后臺運?缩搅,就?守護(hù)進(jìn)程 nohup java -jar xxx.jar &
3.4 解打包后的springboot??的?錄結(jié)構(gòu)
example.jar
|
+-META-INF
| +-MANIFEST.MF
+-org
| +-springframework
| +-boot
| +-loader
| +-<spring boot loader classes>
+-BOOT-INF
+-classes
| +-mycompany
| +-project
| +-YourClasses.class
+-lib
+-dependency1.jar
+-dependency2.jar