C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Creating a Spring Boot Project Using STSWe can also use Spring Tool Suite to create a Spring project. In this section, we will create a Maven Project using STS. Step 1: Open the Spring Tool Suite. Step 2: Click on the File menu -> New -> Maven Project It shows the New Maven Project wizard. Click on the Next button. Step 3: Select the maven-archetype-quickstart and click on the Next button. Step 4: Provide the Group Id and Artifact Id. We have provided Group Id com.TheDeveloperBlog and Artifact Id spring-boot-example-sts. Now click on the Finish button. When we click on the Finish button, it creates the project directory, as shown in the following image. Step 5: Open the App.java file. We found the following code that is by default. App.java package com.TheDeveloperBlog; public class App { public static void main( String[] args ) { System.out.println( "Hello World!" ); } } The Maven project has 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.TheDeveloperBlog</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>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project> Step 6: Add Java version inside the <properties> tag. <java.version>1.8</java.version> Step 7: In order to make a Spring Boot Project, we need to configure it. So, we are adding spring boot starter parent dependency in pom.xml file. Parent is used to declare that our project is a child to this parent project. <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.1.RELEASE</version> <type>pom</type> </dependency> Step 8: Add the spring-boot-starter-web dependency in pom.xml file. <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.2.1.RELEASE</version> </dependency> Note: When we add the dependencies in the pom file, it downloads the related jar file. We can see the downloaded jar files in the Maven Dependencies folder of the project directory.After adding all the dependencies, the pom.xml file looks like the following: 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.TheDeveloperBlog</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>1.8</java.version> </properties> <dependencies> <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.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.2.1.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project> Step 9: Create a class with the name SpringBootExampleSts in the package com.TheDeveloperBlog. Right-click on the package name -> New -> Class -> provide the class name -> Finish Step 10: After creating the class file, call the static method run() of the SpringApplication class. In the following code, we are calling the run() method and passing the class name as an argument. SpringApplication.run(SpringBootExampleSts.class, args); Step 11: Annotate the class by adding an annotation @SpringBootApplication. @SpringBootApplication A single @SpringBootApplication annotation is used to enable the following annotations:
SpringBootApplicationSts.java package com.TheDeveloperBlog; 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); } } Step: Run the file SpringBootExampleSts.java, as Java Application. It displays the following in the console. The line Started SpringBootExampleSts in 5.038 seconds (JVM running for 6.854) in the console shows that the application is up and running.
Next TopicSpring
|