Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build a Spring Boot project

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to build a Spring Boot project". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to build a Spring Boot project.

1. Overview of Spring Boot

Spring framework, as a very excellent lightweight container, is very popular in enterprise project development, but it needs to integrate many third-party resources during its use, which will lead to overstaffed configuration, which is also a headache for everyone.

The emergence of Spring Boot is to make it easier for everyone to use the Spring framework for development. Based on the design concept of "contract over configuration (COC)", it implements automated configuration solutions, including automatic configuration of third-party resources, thus simplifying the creation, operation, debugging, deployment and other convenient operations of Spring applications, and allowing developers to focus more on the implementation of the application business.

In fact, Spring Boot can be thought of as an intermediary, it is a simplified communication platform between developers and the Spring framework, some "procedures (application configuration)" it directly helps us to complete, these "procedures" are actually some default processes in the business process (that is, the default configuration), which happens to be the above-mentioned "agreement is better than configuration" concept. The ultimate goal of Sprint Boot is to increase developers' focus on business implementation.

2. Advantages of Spring Boot

Inherit the excellent features of the Spring framework itself.

It can make the configuration easier and quickly build Spring application projects through automatic configuration.

Ability to run the project independently as a jar file.

More powerful annotations to simplify business implementation.

Embedded with common Web servers, let you use Tomcat, Jetty and so on at any time.

It provides a service monitoring scheme for enterprise production projects, which makes the monitoring easier.

There are also some non-functional general configurations to meet more development needs.

Can carry on the distributed development, unifies Spring Cloud to carry on the micro service development.

3. Set up the Spring Boot project

3.1 create a Maven project

Step 1: select File → New → Project... in the menu bar Pop up the following image, then select Maven, and then click Next.

Step 2: fill in the organization name, module name, project version and other relevant information, as shown below:

Step 3: select the location to save the project, as shown below:

The specific directory of the project after creation, as shown below:

The specific directory of the local folder of the project after you have created it, as shown below:

3.2 relevant explanations about catalogs in the project

Src/main/java: used to store written Java source files, that is, xxx.java files.

Src/main/resources: used to store written configuration files.

Src/test/java: mainly used to store Java source files for testing.

3.3 add configuration and code

Step 1: edit the pom.xml file

The pom.xml file, the project object model (Project Object Model) description file, as the basic configuration file of Maven, is often used to specify the dependency configuration in the project.

Next, we need to add related dependencies to the pom.xml file, as follows:

First, use the tag to specify the spring-boot-starter-parent dependency module, knowing from the tag name that it wants to specify (inherit) something at the parent level.

Setting it up when building a Spring Boot application means that it automatically includes a large number of configurations such as automatic configuration, logging, and YAML that simplify our work. As the core initiator of Spring Boot, it provides some Maven default configuration and dependency-management, which allows you to quickly use Spring Boot for development.

For example, the version number of parent is specified here, so when we introduce other dependencies, we no longer have to care about their version number, which needs to be considered in the past, and it is easy to cause version conflicts.

Edit the pom.xml file

Org.springframework.boot

Spring-boot-starter-parent

2.2.1.RELEASE

If we need to develop Web, we also need to specify the spring-boot-starter-web dependency module, which needs to be specified by adding elements in the pom.xml file, as follows:

Specify web dependency module

Org.springframework.boot

Spring-boot-starter-web

Finally, you can add plug-ins through elements, such as setting Spring Boot's Maven plug-in Spring Boot Maven plugin, which provides Maven operations for Spring Boot applications. This plug-in enables Spring Boot applications to be packaged into executable files in the form of jar or war, and then run in the usual way. The specific configuration is as follows:

Add a maven plug-in

Org.springframework.boot

Spring-boot-maven-plugin

Complete pom.xml file, as follows:

Complete pom.xml file source code

4.0.0

Com.nx

SpringDemo

1.0-SNAPSHOT

Org.springframework.boot

Spring-boot-starter-parent

2.2.1.RELEASE

Org.springframework.boot

Spring-boot-starter-web

Org.springframework.boot

Spring-boot-maven-plugin

Step 2: add the Controller class

Create a new package for com.nx, and then add a HelloControler class, as shown in the figure:

The code for the HelloController controller class, as follows:

HelloController controller

Package com.nx.controller

Import org.springframework.web.bind.annotation.RequestMapping

Import org.springframework.web.bind.annotation.RestController

@ RestController

Public class HelloController {

@ RequestMapping ("/ hello")

Public String sayHello () {

Return "I Love Spring Boot."

}

}

The @ RestController annotation is a combined annotation that contains @ Controller and @ ResponseBody annotations, indicating that it can respond to data in JSON format. The source code of the comments is as follows:

@ RestController Annotation Source Code

@ Target ({ElementType.TYPE})

@ Retention (RetentionPolicy.RUNTIME)

@ Documented

@ Controller

@ ResponseBody

Public @ interface RestController {

@ AliasFor (

Annotation = Controller.class

)

String value () default ""

}

Step 3: add the Spring Boot startup class

To start the path of the class, you need to be able to allow spring boot to scan for other components and add locations:

The code for the SpringBootApp startup class, as shown below:

SpringBootApp startup class

Package com.nx

Import org.springframework.boot.SpringApplication

Import org.springframework.boot.autoconfigure.SpringBootApplication

/ / used to specify the startup class of the Spring Boot application

@ SpringBootApplication

Public class SpringBootApp {

Public static void main (String [] args) {

/ / launch classes in Spring applications from the main method

SpringApplication.run (SpringBootApp.class, args)

}

}

3.4 launch the Spring Boot project

There are three startup methods, as follows:

Start with the Maven command

Start by running the main method

Start with Maven Packaging

Type 1: start with the Maven command

Use the Maven command:

Spring-boot:run

Specify the project path and the Maven command, as follows

Type 2: start by running the main method

Type 3: start with Maven package

You need to run the Spring Boot application as a jar package.

First, in the right column of IDEA, open Maven to see the following page, double-click the package command.

Then, find the target folder under the path of the project, and open it to see the generated jar file.

Finally, from the command line, go to the directory where the jar file is located and execute the java-jar command.

Java-jar.\ SpringDemo-1.0-SNAPSHOT.jar

3.5 access to Spring Boot App

Open a browser and enter the requested address in the address bar:

Http://localhost:8080/hello, and then enter to visit.

Thank you for your reading, the above is the content of "how to build a Spring Boot project", after the study of this article, I believe you have a deeper understanding of how to build a Spring Boot project, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report