文章目錄
歡迎訪問筆者個人技術博客:http://rukihuang.xyz/
一、入門回顧
1.1 maven設置
- 在
settings.xml
配置文件中的profiles
標籤下添加以下代碼,指定jdk版本
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</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>
1.2 使用指定maven與配置文件
1.3 SpringBoot Helloworld
1.3.1 創建一個maven工程(不使用骨架)
- 創建maven工程
1.3.2 導入SpringBoot相關依賴
pom.xml
<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>
1.3.3. 編寫主程序
HelloWorldMainApplication
/**
* @SpringBootApplication 來標註一個主程序類,說明這是一個Spring Boot應用
*/
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
// Spring應用啓動起來
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
1.3.4 編寫Controller
HelloController
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "Hello World!";
}
}
2.2 快速入門探究
2.2.1 POM文件
父項目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
spring-boot-starter-parent
的父項目是spring-boot-dependencies
<!--由他來真正管理SpringBoot用用裏面所有的依賴版本-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
啓動器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
spring-boot-starter
:springboot場景啓動器;幫我們導入了web模塊正常運行所依賴的組件;- Spring Boot將所有的功能場景都抽取出來,做成一個個的starters(啓動器),只需要在項目裏面引入這些starter相關場景的所有依賴都會導入進來。要用什麼功能就導入什麼場景的啓動器
2.2.2 主程序類(主入口)
/**
* @SpringBootApplication 來標註一個主程序類,說明這是一個Spring Boot應用
*/
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
// Spring應用啓動起來
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
@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 {
//@SpringBootConfiguration
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration //!
public @interface SpringBootConfiguration {
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
@SpringBootConfiguration
:Spring Boot的配置類。標註在某個類上,表示這是一個Spring Boot的配置類;@Configuration
:配置類上來標註這個註解。配置類,等價於配置文件;@Component
:配置類也是容器中的一個組件;
//@EnableAutoConfiguration點進去後
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage //!
@Import({AutoConfigurationImportSelector.class})//!
public @interface EnableAutoConfiguration {
//@AutoConfigurationPackage點進去後
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({Registrar.class})//AutoConfigurationPackages.Registrar.class
public @interface AutoConfigurationPackage {
@EnableAutoConfiguration
:開啓自動配置功能。Spring Boot幫我們自動配置;@EnableAutoConfiguration
告訴SpringBoot開啓自動配置功能;這樣自動配置才能生效;@AutoConfigurationPackage
:自動配置包@Import(AutoConfigurationPackages.Registrar.class)
:Spring的底層註解@Import
,給容器中導入一個組件;導入的組件由AutoConfigurationPackages.Registrar.class
將主配置類(@SpringBootApplication
標註的類)的所在包及下面所有子包裏面的所有組件掃描到Spring容器
@Import(EnableAutoConfigurationImportSelector.class)
;給容器中導入組件。EnableAutoConfigurationImportSelector
:導入哪些組件的選擇器;將所有需要導入的組件以全類名的方式返回;這些組件就會被添加到容器中;會給容器中導入非常多的自動配置類(xxxAutoConfiguration
);就是給容器中導入這個場景需要的所有組件,並配置好這些組件;
- Spring Boot在啓動的時候從類路徑下的
META-INF/spring.factories
中獲取EnableAutoConfiguration
指定的值,將這些值作爲自動配置類導入到容器中,自動配置類就生效,幫我們進行自動配置工作;以前我們需要自己配置的東西,自動配置類都幫我們配置了。SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,classLoader);
2.3 Spring Initializer快速創建
-
在聯網情況下快速創建springboot項目
-
resources
文件夾中目錄結構static
:保存所有的靜態資源; js css images;templates
:保存所有的模板頁面;(Spring Boot默認jar包使用嵌入式的Tomcat,默認不支持JSP頁面);可以使用模板引擎(freemarker、thymeleaf);application.properties
:Spring Boot應用的配置文件;可以修改一些默認設置;