SpringBoot的主要目的是簡化配置文件,經過少許配置便可運行Java程序,其強大的自動配置功能幫助開發者輕鬆實現配置裝配,經過引入SpringBoot的starter
就能實現想要的功能,不須要額外的配置。php
目前SpringBoot工程有三種搭建方式:java
Spring團隊提供一個很是方便的網頁用於生成SpringBoot工程,打開瀏覽器進入 Spring Initializr :spring
工程生成參數列表:apache
Group
和Artifact
等配置參數設置完成後點擊Generate
下載工程,完成後使用IDEA
導入工程,打開工程同步便可運行。瀏覽器
較新的IDEA
版本都內置建立SpringBoot工程插件,其建立原理也是使用的 Spring Initializr 來建立工程,建立流程下如:架構
IDEA
開發工具file
-> new
-> project
菜單Spring Initializr
Next
便可建立SpringBoot工程最後添加main
方法啓動應用程序:機器學習
@SpringBootApplication
@Slf4j
public class SpringEnvApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(SpringEnvApplication.class, args);
}
}
複製代碼
除了以上兩種方式外,還能夠經過手動建立的方式建立SpringBoot工程,經過IDEA
建立一個空的Maven
工程,而後指定SpringBoot的依賴就,基本流程以下:maven
IDEA
開發工具file
-> new
-> project
菜單Mavenn
Next
根據提示完成項目建立工程建立完成後,打開pom.xml
文件,設置pom.xml
的parent配置:spring-boot
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
</parent>
複製代碼
添加SpringBootMaven
打包插件:工具
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
複製代碼
添加main
方法啓動應用程序:
@SpringBootApplication
@Slf4j
public class SpringEnvApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(SpringEnvApplication.class, args);
}
}
複製代碼
完整pom.xml
文件:
<?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 https://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.2.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.csbaic.arch</groupId>
<artifactId>spring-env</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-env</name>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
複製代碼
設置parent和插件後,就能夠使用SpringBoot建立應用程序了。
《架構文摘》天天一篇架構領域重磅好文,涉及一線互聯網公司應用架構(高可用、高性 能、高穩定)、大數據、機器學習等各個熱門領域。