Spring Tool Suite,STS



Spring Tool Suite,STS 是一个 IDE(集成开发环境),非常适合开发 Spring Boot 应用程序。

下载并安装 STS

  1. 可以从 https://springframework.org.cn/tools 下载 STS。

  2. 选择操作系统(本例中为 Windows)。

  3. 下载 zip 文件并解压。

  4. 点击 SpringToolSuite4.exe 文件。

  5. Spring Tool Suite 4 启动器对话框会询问工作区位置。输入位置,然后按“启动”按钮。

在 STS 中创建一个简单的 Spring Boot 应用程序

  1. 转到文件 -> 新建 Maven 项目

  2. 点击下一步。将出现以下对话框

    Create New Project
  3. 选择maven-archetype-quickstart

    Create Project Wizard
  4. 对于GroupId,输入com.tutorialspoint,对于ArtifactId,输入first-spring-boot-example

    Finish Project Wizard
  5. 点击完成

  6. 完成后,将创建文件夹结构 -

    Project Explorer
  7. 打开pom.xml文件。在dependencies下添加以下内容。

    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-autoconfigure</artifactId>
       <version>3.3.3</version>
    </dependency>
    

    此外,通过右键单击项目 -> 构建路径 -> 添加外部存档将 Spring 框架核心 jar 添加到构建路径

    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>com.tutorialspoint</groupId>
       <artifactId>first-spring-boot-example</artifactId>
       <version>0.0.1-SNAPSHOT</version>
       <name>first-spring-boot-example</name>
       <!-- FIXME change it to the project's website -->
       <url>first-spring-boot-example</url>
       <properties>
          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
          <maven.compiler.release>17</maven.compiler.release>
       </properties>
       <dependencyManagement>
          <dependencies>
             <dependency>
                <groupId>org.junit</groupId>
                <artifactId>junit-bom</artifactId>
                <version>5.11.0</version>
                <type>pom</type>
                <scope>import</scope>
             </dependency>
          </dependencies>
       </dependencyManagement>
       <dependencies>
          <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-autoconfigure</artifactId>
             <version>3.3.3</version>
          </dependency>
          <dependency>  
             <groupId>org.springframework.boot</groupId>  
             <artifactId>spring-boot-starter-web</artifactId>  
             <version>2.2.1.RELEASE</version>  
          </dependency>  
          <dependency>  
             <groupId>org.springframework.boot</groupId>  
             <artifactId>spring-boot-starter-parent</artifactId>  
             <version>2.2.1.RELEASE</version>  
             <type>pom</type>  
          </dependency>  
          <dependency>
             <groupId>org.junit.jupiter</groupId>
             <artifactId>junit-jupiter-api</artifactId>
             <scope>test</scope>
          </dependency>
          <!-- Optionally: parameterized tests support -->
          <dependency>
             <groupId>org.junit.jupiter</groupId>
             <artifactId>junit-jupiter-params</artifactId>
             <scope>test</scope>
          </dependency>
       </dependencies>
       <build>
          <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
             <plugins>
                <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
                <plugin>
                   <artifactId>maven-clean-plugin</artifactId>
                   <version>3.4.0</version>
                </plugin>
                <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
                <plugin>
                   <artifactId>maven-resources-plugin</artifactId>
                   <version>3.3.1</version>
                </plugin>
                <plugin>
                   <artifactId>maven-compiler-plugin</artifactId>
                   <version>3.13.0</version>
                </plugin>
                <plugin>
                   <artifactId>maven-surefire-plugin</artifactId>
                   <version>3.3.0</version>
                </plugin>
                <plugin>
                   <artifactId>maven-jar-plugin</artifactId>
                   <version>3.4.2</version>
                </plugin>
                <plugin>
                   <artifactId>maven-install-plugin</artifactId>
                   <version>3.1.2</version>
                </plugin>
                <plugin>
                   <artifactId>maven-deploy-plugin</artifactId>
                   <version>3.1.2</version>
                </plugin>
                <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
                <plugin>
                   <artifactId>maven-site-plugin</artifactId>
                   <version>3.12.1</version>
                </plugin>
                <plugin>
                   <artifactId>maven-project-info-reports-plugin</artifactId>
                   <version>3.6.1</version>
                </plugin>
             </plugins>
          </pluginManagement>
       </build>
    </project>
    
  8. 在 src/main/java 下创建一个新的 Java 类,包名为com.tutorialspoint.first_spring_boot_example。将类命名为FirstSpringBootTestClass

    FirstSpringBootTest.java

    package com.tutorialspoint.first_spring_boot_example;
    
    import org.springframework.boot.SpringApplication;    
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class FirstSpringBootTest {
    
       public static void main(String[] args) {   
          System.out.println("Hello World, this is First Spring Boot Test");
          SpringApplication.run(FirstSpringBootTest.class, args);
       }
    }
    

输出

  1. 从项目资源管理器中,右键单击文件FirstSpringBootTest,然后以...方式运行 -> Java 应用程序

  2. 在控制台中,您将看到 -

    Hello World, this is First Spring Boot Test
    
广告