一、SpringBoot簡介
1. SpringBoot簡化Spring應用開發,
隨著時間的推移,Spring開發越來越笨重,需要眾多繁瑣的配置,開發和部署流程都變得復雜,集成第三方的框架也變得很難,SpringBoot的推出就是為了解決上述問題而誕生,
2. Spring、SpringBoot、SpringCloud
- 最開始Spring全家桶的時代,有眾多Spring相關組件,
- SpringBoot:J2EE一站式的解決方案,是產品級別的應用,
- SpringCloud:分布式的整體解決方案,
二、SpringBoot特點
- 快速創建單體Spring應用
- 嵌入Tomcat,Jetty或Undertow(無需部署WAR檔案)
- 提供
starter,以簡化依賴和版本控制,方便構建配置 - 盡可能自動配置Spring和第三方框架,
- 提供可用于生產的功能,比如監控和外部化配置
- 開箱機用,沒有代碼生成,也不需要XML配置
三、環境準備
- IntelliJIDEA 2019.2
- Maven3.3.3
- jdk1.8: 1.8.0_144
- SpringBoot 2.2.2.RELEASE
Maven配置
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<servers>
</servers>
<mirrors>
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
<mirror>
<id>repo1</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://repo1.maven.org/maven2/</url>
</mirror>
<mirror>
<id>repo2</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://repo2.maven.org/maven2/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>jdk18</id>
<activation>
<jdk>1.8</jdk>
<activeByDefault>true</activeByDefault>
</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>
</profiles>
</settings>
四、專案構建與測驗
1、新建專案
- 新建一個專案springboot-helloworld
2、POM配置
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
3、撰寫啟動類
@SpringBootApplication
public class HelloWorldApp {
public static void main(String[] args) {
SpringApplication.run(HelloWorldApp.class, args);
}
}
4、撰寫Controller
在controller包下新建
@RestController
public class HelloController {
@GetMapping(value = "https://www.cnblogs.com/hello")
public String hello() {
return "Hello World";
}
}
5、運行并測驗專案
運行專案,可以直接運行,也可以生成Jar包用java -jar命令運行
在瀏覽器輸入 http://localhost:8080/hello ,即可得到專案結果,應用得到預期的結果,

五、原理決議
1、POM
父POM
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
他的父專案是
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
spring-boot-starter-web
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
spring-boot-starter是場景啟動器,幫助我們自動匯入依賴的組件,其中spring-boot-starter-web就自動匯入了web的相應依賴
spring-boot-maven-plugin
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
簡化部署,這個插件可以把應用打包成一個可以直接執行的Jar包,相應的依賴也會打到Jar包中,只需要執行java -jar命令就可以執行,
2、主啟動類
2.1 @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 {
從@SpringBootApplication來看,已經包含了@EnableAutoConfiguration和@SpringBootConfiguration,開啟了自動配置
2.2 @EnableAutoConfiguration:開啟了自動配置,
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
通過定義可以看到,@EnableAutoConfiguration包含了@AutoConfigurationPackage注解和@Import注解
@AutoConfigurationPackage:自動配置包
@Import:匯入資源注解,@Import(AutoConfigurationImportSelector.class)匯入AutoConfigurationImportSelector類
2.3 @SpringBootConfiguration注解標明這是個SpringBoot的配置類,包含了@Configuration,配置類還包含了@Component,
3、Controller類
@RestController:注解相當于@Controller和@ResponseBody組合注解
@Controller 用于標記在一個類上,使用它標記的類就是一個SpringMVC Controller 物件,
@responseBody 注解的作用是將controller的方法回傳的物件通過適當的轉換器轉換為指定的格式之后,寫入到response物件的body區,通常用來回傳JSON或者是XML資料,
參考檔案:
SpringBoot官方:https://spring.io/projects/spring-boot
尚硅谷檔案
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/95092.html
標籤:Java
上一篇:MyBatis動態SQL
