SpringBoot introductory tutorial 01-build the first SpringBoot application

SpringBoot introductory tutorial 01-build the first SpringBoot application

Introduction

Spring Boot is a new framework provided by the Pivotal team. Its design purpose is to simplify the initial setup and development process of new Spring applications. The framework uses a specific method for configuration, so that developers no longer need to define a prototype configuration, and through a series of Starter POMs definition, integration of various functions, making dependency management easier.

Build the app

surroundings:

  • jdk 1.8 or above
  • idea community edition
  • maven (idea comes with it, set up Alibaba Cloud Central Warehouse)
  • Spring Assistant plugin (idea MarketPlace installation, free)

Open idea -> new Project -> Spring Assistant -> fill in Group Id, Artifact Id -> (recommended not required) Check Spring Boot DevTools, Lombok, Spring Configuration Processor under Devloper Tools -> Check Spring Web under Web- > Click Next.

Project catalog

Insert picture description here

  • The pom file is the basic dependency management file
  • resouces resource file
    • statics static resources
    • templates template resources
    • application.properties configuration file
  • SpringbootDemoApplication program entry

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.henry</groupId>
	<artifactId>springboot-demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springboot-demo</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

Demo

After the application is built, run the main method of SpringbootDemoApplication, and the springboot application will start

Let's write a Controller to test the springboot application

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    
    

    @RequestMapping("/")
    public String index(){
    
    
        return "Welcome to Spring Boot!";
    }
}

Start the main method of SpringbootDemoApplication, open the browser localhost:8080, the browser displays:

Welcome to Spring Boot!

PS: If you check the developer tool Spring Boot DevTools when building the application, the idea will be automatically reloaded every time the code is written, and the application can be tested without restarting the application.

The magic

  • You did not do any web.xml configuration
  • You did not do any sping mvc configuration-springboot did it for you
  • You did not configure tomcat-springboot embeds tomcat.

Pack and run

Since this project uses the built-in maven of idea, the package is packaged through the idea tool
Insert picture description here

As shown

  1. Click the ⚡ symbol to skip the test (one-time operation, applicable to all items)
  2. Then click clean under Lifecycle
  3. Click install under Lifecycle

After the packaging is complete, open the Terminal tool of idea and enter the command

cd target
java -jar springboot-demo-0.0.1-SNAPSHOT.jar

Guess you like

Origin blog.csdn.net/l229568441/article/details/106893659