在上一章,我講解了React+node+express相應的框架搭建,一個項目只有一個前端框架夠么,當然不夠啦!!!
所以這節我們就來講后台springboot框架的搭建和相關原理吧~~~版本(2.1.1)
1.搭建Springboot所需要的前提
①JDK8-JDK11
②Maven3.3+ 或 Gradle4.4+版本(我的隨筆內用的都是Maven)
③Tomcat9.0 或 Jetty 9.4 或 Undertow 2.0
2. 到底什么是Springboot么,讓我們來看下圖
意思就是: Springboot是Spring框架的集成,相比Spring框架, 除了擁有Spring的功能外, Springboot配置無疑會更加輕松簡單。另外默認情況生成的包為jar包,我們以java -jar相應的指令啟動服務。
另外透露一點:以Springboot為基礎的SpringCloud目前是微服務的首選框架~~
3. 讓我們開始搭建吧
①首先我們在IDE里面建立一個Maven project, 本地命名為對應的maven.example
②更換Pom的內容: 以下為內容:
<?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> <groupId>maven</groupId> <artifactId>example</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> </parent> <!-- Add typical dependencies for a web application --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <!-- Package as an executable jar --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
關於部分模塊的作用,我們這里需要講述一下。
<!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> </parent>
如圖,即該project繼承了spring-boot-starter-parent, 那么spring-boot-starter-parent提供了什么功能呢?
Ⅰ: 它以JAVA 1.8做默認編譯器
Ⅱ: 它以UTF-8作為編碼格式
Ⅲ: 從spring-boot-dependencies pom繼承的依賴管理部分,它允許我們省略這些依賴的<version>標記。(可看“spring-boot-starter-web”對應的dependency就省略了<version>標記)。當要升級springboot本身時,有對應依賴關系的包會一同升級。
Ⅳ: 智能的resource過濾; 智能的插件配置(如git commind id、exec plugin等)
Ⅴ: 對application.properties、application.yml等文件的智能過濾與利用
<!-- Package as an executable jar --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
如圖,即如果編譯完程序,最后是要以jar形式去運行的話,則一定要加入此塊內容,否則會報錯。
<!-- Add typical dependencies for a web application --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies>
如圖,spring-boot-starter-web包即是使用Spring MVC來構建RESTful Web應用,並使用tomcat作為內嵌容器;junit包即指引入junit單元測試模塊。
③建立不同層的package,如圖(注:controller、entity、mapper等package要建立在maven.example該package層內):
這幾層package的作用是什么呢?
controller: 負責路由
entity: model層
mapper: mybatis相應的SQL操作層
service: controller調用的service層
以上四層的用法我會在之后的章節講解,其余各位可再根據業務自己建立對應的package層
④在maven.example的package下的App.java里寫下這段代碼:
package maven.example;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; @SpringBootApplication public class App extends SpringBootServletInitializer{ public static void main(String[] args) throws Exception { SpringApplication.run(App.class, args); } }
⑤關於@SpringBootApplication標簽
@ SpringBootApplication注釋等同於使用@Configuration,@EnableAutoConfiguration和@CommponentScan及其默認屬性
@ EnableAutoConfiguration:啟用Spring Boot的自動配置機制
@ ComponentScan:在應用程序所在的包上啟用@Component 掃描
@Configuration:允許在上下文中注冊額外的bean或導入額外的配置類
關於注釋詳細的解釋會在以后的章節講明
⑥關於main函數
main(String[] args) throws Exception {
SpringApplication.run(App.class, args);
}
我們的主要方法通過調用run來委托Spring Boot的Spring Application類SpringApplication從我們的應用程序中引導Spring,從而啟動它 ,然后啟動自動配置的Tomcat web服務器,我們需要將App.class作為參數傳遞給run方法,以告訴SpringApplication它是主要的Spring組件,args數組也被傳遞以公開所有命令行參數
⑦編譯啟動服務:
打開控制台,進到項目的根目錄處,執行以下語句:mvn spring-boot:run
執行成功后如圖:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.1.RELEASE) ....... . . . ....... . . . (log output here) ....... . . .
今天咱就暫時寫到這啦, 下章會寫上前端通過ajax與springboot框架的通信相關的內容