天天看點

如何建立 Spring Boot 項目

Spring Boot 簡介

SpringBoot是一個快速開發架構,通過用MAVEN依賴的繼承方式,幫助我們快速整合第三方常用架構,完全采用注解化(使用注解方式啟動SpringMVC),簡化XML配置,内置HTTP伺服器(Tomcat,Jetty),最終以Java應用程式進行執行。

如何建立 Spring Boot 項目

Sping Boot 項目的本質其實還是一個 Maven 項目,主要有如下幾種建立 Spring Boot 項目的方式;

線上建立

1、打開 ​​https://start.spring.io/​​ 來生成 Spring Boot 項目;

​​

如何建立 Spring Boot 項目

​​/2、

2、然後選擇和填寫相關配置;

  • Project:表示使用什麼建構工具,Maven or Gradle;
  • Language:表示使用什麼程式設計語言, Java 、Kotlin or Groovy;
  • Spring Boot:Spring Boot 的版本;
  • Project Metadata:項目中繼資料,即 Maven項目基本元素,根據自己的實際情況填寫;
  • Dependencies:要加入的 Spring Boot 元件;

​​

如何建立 Spring Boot 項目

​​

3、然後點選生成或 Ctrl + Enter 即可;

​​

如何建立 Spring Boot 項目

​​

4、将壓縮包下載下傳後,解壓縮後用自己喜歡的 IDE 開發即可;

IntelliJ IDEA 建立

1、建立項目時選擇 Spring Initializr ;

​​

如何建立 Spring Boot 項目

​​

2、點選下一步,填寫相關配置;

  • Group:組織 ID,一般分為多個段,一般第一段為域,而第二段則是 公司名稱;
  • Artifact:唯一辨別符,一般是項目名;

​​

如何建立 Spring Boot 項目

​​

3、選擇包,添加相關依賴;

​​

如何建立 Spring Boot 項目

​​

4、配置項目名,點選完成即可;

​​

如何建立 Spring Boot 項目

​​

Maven 建立

1、建立 Maven 項目;

​​

如何建立 Spring Boot 項目

​​

2、填寫項目名和相關配置;

​​

如何建立 Spring Boot 項目

​​

3、點選完成即可;

​​

如何建立 Spring Boot 項目

​​

4、配置 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.cunyu1943</groupId>
    <artifactId>springDemo</artifactId>
    <version>1.0-SNAPSHOT</version>
    
    <properties>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>
    </dependencies>
    
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.5</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <encoding>UTF-8</encoding>  
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

</project>      

5、在 main/java 目錄下建立一個包,然後建立一個類,比如我的如下;

package controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@SpringBootApplication
@RestController
public class App {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(App.class, args);
    }

    @GetMapping("/index")
    public String index() {
        return "Hello World!";
    }
}      

6、運作上一步中的 main 方法即可;

常見項目結構

代碼層

根目錄:com.springboot:

  • build :工程啟動類;
  • entity :實體類;
  • dao :資料通路層;
  • service :資料服務層,業務類代碼;
  • controller :前端通路控制器;
  • config :配置資訊類;
  • util :工具類;
  • constant :常用接口類;
  • vo :資料傳輸類;
  • Application.java:項目的啟動類;

資源檔案結構

根目錄 src/main/resources:

  • config :.properties、.json 等配置檔案;
  • i18n :國際化相關;
  • META-INF/spring :spring.xml ;
  • static :頁面以及 js、css、image 等分别放在各自檔案夾下;

@SpringBootApplication 注解分析

package org.springframework.boot.autoconfigure;
@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 {
   ......
}      

說明:

@SpringBootApplication 标注該類是一個啟動類,可以看做是 @Configuration、@EnableAutoConfiguration、@ComponentScan 的集合;

  • @Configuration :允許在上下文中注冊額外的 Bean 或導入其他配置;
  • @EnableAutoConfiguration:啟動 Spring Boot 的自動配置機制;
  • @ComponentScan:掃描被 @ComponentScan(@Service、@Controller、@Repository) 注解的 Bean,預設掃描該類所在包下所有類,将這些 Bean 定義加載到 IOC 容器中;

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.3.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.cunyu</groupId>
    <artifactId>springboot-03</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-03</name>
    <description>spring boot - 03</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>      

建立好項目後,如果沒有選其他元件,會生成如上的 Spring Boot 項目依賴,主要有四個部分:

  • 項目中繼資料

建立時輸入的 Project Metadata 部分,即 Maven 項目的基本元素,包括 groupId、artifactId、version、name、description 等;

  • parent

繼承 spring-boot-starter-parent 的依賴管理,控制版本與打包等等内容;

  • dependencies

項目具體依賴,預設包含 spring-boot-starter-web,用于實作HTTP接口(該依賴中包含了Spring MVC);spring-boot-starter-test用于編寫單元測試的依賴包。後續開發中,主要就是在這裡添加各種依賴。

  • build

建構配置部分,預設使用 spring-boot-maven-plugin,配合 spring-boot-starter-parent 可以把 Spring Boot 應用打包成 jar 來直接運作。

Spring Boot中parent标簽的作用

在Spring Boot的官方示例中,都是讓我們繼承一個spring的 spring-boot-starter-parent作為parent标簽。

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.6.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
</parent>      

parent 源碼

<?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>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.1.6.RELEASE</version>
    <relativePath>../../spring-boot-dependencies</relativePath>
  </parent>
  <artifactId>spring-boot-starter-parent</artifactId>
  <packaging>pom</packaging>
  <name>Spring Boot Starter Parent</name>
  <description>Parent pom providing dependency and plugin management for applications
    built with Maven</description>
  <url>https://projects.spring.io/spring-boot/#/spring-boot-starter-parent</url>
  <properties>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <resource.delimiter>@</resource.delimiter>
    <maven.compiler.source>${java.version}</maven.compiler.source>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.target>${java.version}</maven.compiler.target>
  </properties>
  <build>
    <resources>
      <resource>
        <filtering>true</filtering>
        <directory>${basedir}/src/main/resources</directory>
        <includes>
          <include>**/application*.yml</include>
          <include>**/application*.yaml</include>
          <include>**/application*.properties</include>
        </includes>
      </resource>
      <resource>
        <directory>${basedir}/src/main/resources</directory>
        <excludes>
          <exclude>**/application*.yml</exclude>
          <exclude>**/application*.yaml</exclude>
          <exclude>**/application*.properties</exclude>
        </excludes>
      </resource>
    </resources>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.jetbrains.kotlin</groupId>
          <artifactId>kotlin-maven-plugin</artifactId>
          <version>${kotlin.version}</version>
          <executions>
            <execution>
              <id>compile</id>
              <phase>compile</phase>
              <goals>
                <goal>compile</goal>
              </goals>
            </execution>
            <execution>
              <id>test-compile</id>
              <phase>test-compile</phase>
              <goals>
                <goal>test-compile</goal>
              </goals>
            </execution>
          </executions>
          <configuration>
            <jvmTarget>${java.version}</jvmTarget>
            <javaParameters>true</javaParameters>
          </configuration>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <configuration>
            <parameters>true</parameters>
          </configuration>
        </plugin>
        <plugin>
          <artifactId>maven-failsafe-plugin</artifactId>
          <executions>
            <execution>
              <goals>
                <