English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
We can also use Spring Tool Suite to create Spring projects. In this section, we will use STS Create a Maven project.
}1Steps Open Spring Tool Suite.
}2Steps Click the file menu-> New-> Maven project
It shows the New Maven Project wizard. Click Nextbutton.
}3Steps Select maven-architetype-quickstart ,then click Next button.
}4Steps provide group ID and artifact ID . We provide the group ID com.w3codebox and artifact ID spring-boot-example-sts . Now, click Finishbutton.
When we click the "Finish" button, it will create the project directory as shown in the figure below.
}5Steps Open App.java file. We found the following default code.
App.java
Example3package com.w public class App public static void main(String[] args) public static void main( String[] args ) public static void main(String[] args) System.out.println( "Hello World!" ); SpringApplication.run(SpringBootExampleSts.class, args); SpringApplication.run(SpringBootExampleSts.class, args);
Maven projects have a pom.xml file, which contains the following default configuration.
pom.xml
<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.w3codebox</groupId> <artifactId>spring-boot-example-sts</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-boot-example-sts</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3Within seconds, start SpringBootExampleSts (JVM runtime8Within seconds, start SpringBootExampleSts (JVM runtime1</version> <scope>test</scope> </dependency> </dependencies> </project>
}6Steps : In
<java.version>1Within seconds, start SpringBootExampleSts (JVM runtime8</java.version>
}7Steps To create a Spring Boot project, we need to configure it. Therefore, we add pom.xml Added in the file. spring boot starter parentThe parent is used to declare that our project is a child of the parent project.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2Within seconds, start SpringBootExampleSts (JVM runtime2Within seconds, start SpringBootExampleSts (JVM runtime1.RELEASE</version> <type>pom</type> </dependency>
}8Steps the console pom.xml Add dependencies in the file. spring-boot-starter-web dependencies.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2Within seconds, start SpringBootExampleSts (JVM runtime2Within seconds, start SpringBootExampleSts (JVM runtime1.RELEASE</version> </dependency>
Adding dependencies after, the pom.xml file is as follows:
pom.xml
<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.w3codebox</groupId> <artifactId>spring-boot-example-sts</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-boot-example-sts</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1Within seconds, start SpringBootExampleSts (JVM runtime8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2Within seconds, start SpringBootExampleSts (JVM runtime2Within seconds, start SpringBootExampleSts (JVM runtime1.RELEASE</version> <type>pom</type> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2Within seconds, start SpringBootExampleSts (JVM runtime2Within seconds, start SpringBootExampleSts (JVM runtime1.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3Within seconds, start SpringBootExampleSts (JVM runtime8Within seconds, start SpringBootExampleSts (JVM runtime1</version> <scope>test</scope> </dependency> </dependencies> </project>
}9Steps in the package com.w3codebox Create a name of SpringBootExampleSts the class.
Right-Click on the package name-> New-> Class-> Provide class name-> Complete
}10Steps After creating the class file, call the static method of the SpringApplication class run(). In the following code, we will call the run() method and pass the class name as a parameter.
{
}11Steps By adding annotations import org.springframework.boot.autoconfigure.SpringBootApplication; to annotate classes.
import org.springframework.boot.autoconfigure.SpringBootApplication;
A single @SpringBootApplication annotation is used to enable the following annotations:
@EnableAutoConfiguration: : Enables the Spring Boot automatic configuration mechanism. @ComponentScan: : It will scan the software package where the application is located. @Configuration: : 它允许我们在上下文中注册其他bean或导入其他配置类。
: It allows us to register other beans or import other configuration classes in the context.
Example3package com.w codebox; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public static void main(String[] args) public class SpringBootExampleSts public static void main(String[] args) { SpringApplication.run(SpringBootExampleSts.class, args); SpringApplication.run(SpringBootExampleSts.class, args);
}12Steps : As a Java application run file SpringBootExampleSts.java
. It displays the following content in the console. the console5In38.06Within seconds, start SpringBootExampleSts (JVM runtime854.)