📜  使用STS创建Spring Boot项目

📅  最后修改于: 2021-01-11 04:59:12             🧑  作者: Mango

使用STS创建Spring Boot项目

我们还可以使用Spring Tool Suite创建一个Spring项目。在本节中,我们将使用STS创建一个Maven项目

步骤1:打开Spring Tool Suite。

第2步:点击文件菜单->新建-> Maven项目

它显示了New Maven Project向导。单击下一步按钮。

步骤3:选择maven-archetype-quickstart ,然后单击Next(下一步)按钮。

第4步:提供组ID工件ID 。我们提供了组ID com.javatpoint和工件ID spring-boot-example-sts 。现在单击完成按钮。

当我们单击Finish按钮时,它将创建项目目录,如下图所示。

步骤5:打开App.java文件。我们发现以下默认代码。

App.java

package com.javatpoint;
public class App 
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}

Maven项目有一个pom.xml文件,其中包含以下默认配置。

pom.xml


4.0.0
com.javatpoint
spring-boot-example-sts
0.0.1-SNAPSHOT
jar
spring-boot-example-sts
http://maven.apache.org

UTF-8



junit
junit
3.8.1
test



步骤6:标记内添加Java版本。

1.8

步骤7:为了制作一个Spring Boot项目,我们需要对其进行配置。因此,我们在pom.xml文件中添加了spring boot starter父依赖项。父级用于声明我们的项目是该父级项目的子级。


 org.springframework.boot
spring-boot-starter-parent
2.2.1.RELEASE
pom

步骤8:在pom.xml文件中添加spring-boot-starter-web依赖项。


org.springframework.boot
spring-boot-starter-web
2.2.1.RELEASE

注意:当我们在pom文件中添加依赖项时,它将下载相关的jar文件。我们可以在项目目录的Maven Dependencies文件夹中看到下载的jar文件。

添加所有依赖项后,pom.xml文件如下所示:

pom.xml


4.0.0
com.javatpoint
spring-boot-example-sts
0.0.1-SNAPSHOT
jar
spring-boot-example-sts
http://maven.apache.org

UTF-8
1.8  



org.springframework.boot
spring-boot-starter-parent
2.2.1.RELEASE
pom


org.springframework.boot
spring-boot-starter-web
2.2.1.RELEASE


junit
junit
3.8.1
test



步骤9:在包com.javatpoint中创建一个名称为SpringBootExampleSts的类。

右键单击包名称->新建->类->提供类名称->完成

步骤10:在创建了类文件之后,调用SpringApplication类的静态方法run()。在下面的代码中,我们将调用run()方法并将类名作为参数传递。

SpringApplication.run(SpringBootExampleSts.class, args);  

步骤11:通过添加注释@SpringBootApplication来对类进行注释。

@SpringBootApplication

单个@SpringBootApplication批注用于启用以下批注:

  • @EnableAutoConfiguration:启用Spring Boot自动配置机制。
  • @ComponentScan:它扫描应用程序所在的软件包。
  • @Configuration:它允许我们在上下文中注册其他bean或导入其他配置类。

SpringBootApplicationSts.java

package com.javatpoint;
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication; 
@SpringBootApplication  
public class SpringBootExampleSts 
{
public static void main(String[] args)
{  
SpringApplication.run(SpringBootExampleSts.class, args);  
} 
}

步骤:作为Java Application运行文件SpringBootExampleSts.java。它在控制台中显示以下内容。

控制台中的在5.038秒内启动SpringBootExampleSts(JVM运行6.854)行显示该应用程序已启动并正在运行。