SpringBoot工程的三種搭建方式

SpringBoot的主要目的是簡化配置文件,經過少許配置便可運行Java程序,其強大的自動配置功能幫助開發者輕鬆實現配置裝配,經過引入SpringBoot的starter就能實現想要的功能,不須要額外的配置。php

目前SpringBoot工程有三種搭建方式:java

  • 經過Spring Initializr建立
  • 經過IDEA建立工程
  • 手動建立工程

官方生成工具

Spring團隊提供一個很是方便的網頁用於生成SpringBoot工程,打開瀏覽器進入 Spring Initializr :spring

工程生成參數列表:apache

  • Project: 工程類型(支持Maven和Gradle構建工具)
  • Language:工程主要語言根據須要可選擇Java、Kotlin、Groovy
  • SpringBoot:SpringBoot版本
  • ProjectMatedata:有GroupArtifact等配置
  • Dependencies:工程依賴

參數設置完成後點擊Generate下載工程,完成後使用IDEA導入工程,打開工程同步便可運行。瀏覽器

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工程

除了以上兩種方式外,還能夠經過手動建立的方式建立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建立應用程序了。



《架構文摘》天天一篇架構領域重磅好文,涉及一線互聯網公司應用架構(高可用、高性 能、高穩定)、大數據、機器學習等各個熱門領域。
相關文章
相關標籤/搜索