SpringBoot
一、第一個專案的步驟:
1.創建Maven專案
選擇自己的maven D:/apache-maven-3.6.3
2.修改pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.13.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
3.撰寫一個主程式:啟動spring boot應用
/**
* @SpringBootApplication 來標注一個主程式類,說明在是一個spring boot應用
*/
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
//Spring應用啟動起來
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
4.撰寫相關的Controller、servlet
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "Hello World!";
}
}
5.運行主程式測驗
6.簡化部署
<!--這個插件,可以將應用打包成一個可執行的jar包-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
將這個應用打成jar包,直接使用java -jar的命令進行執行;
二、Hello World探究
1.pom檔案
1.父專案
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.13.RELEASE</version>
</parent>
他的父專案是:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.13.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
他來真正管理Spring Boot應用里的所有依賴版本;
Spring Boot的版本仲裁中心
以后我們匯入依賴默認是不需要寫版本: spring-boot-dependencies
(沒有在dependencies的依賴需要寫版本)
2.匯入的依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
spring-boot-starter-web
spring-boot-starter:springboot場景啟動器:幫我們匯入了web模塊正常運行所依賴的組件.
Spring Boot 將所有的功能場景都抽取出來,做成一個個的Starters(啟動器),只需要在專案里面參考這些starter相關場景的所有依賴都會匯入進來,要用什么功能就匯入什么場景的啟動器
2.主程式類,主入口類
/**
* @SpringBootApplication 來標注一個主程式類,說明在是一個spring boot應用
*/
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
//Spring應用啟動起來
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
@SpringBootApplication: spring boot應用 標注在某個類上說明這個類是Spring Boot的主配置類,SpringBoot就應該運行這個類的main方法來啟動SpringBoot應用
@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 {
@SpringBootConfiguration :springBoot的配置類
? 標注在某個類上,表示這是一個springBoot的配置類:
? @Configuration:配置類上來標注這個注解
? 配置類 ----- 組態檔;配置類也是容器中的一個組件:@Component
@EnableAutoConfiguration :開啟自動配置功能.
下一篇學習
SpringBoot詳解(二) 從入門到入土 (組態檔)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/11669.html
標籤:其他
