Create SpringBoot project by hand-HelloWorld

Maven usage convention is better than configuration

table of Contents use
${basedir} Store pom.xml and all subdirectories
${basedir}/src/main/java Java source code of the project
${basedir}/src/main/resources Project resources, such as property files, springmvc.xml
${basedir}/src/test/java The test class of the project, such as Junit code
${basedir}/src/test/resources Resources for testing
${basedir}/src/main/webapp/WEB-INF Web application file directory, web project information, such as web.xml, local pictures, jsp view page
${basedir}/target Package output directory
${basedir}/target/classes Compile output directory
${basedir}/target/test-classes Test compile output directory
Test.java Maven will only automatically run test classes that meet the naming rules
~/.m2/repository Maven default local repository directory location

1. Create a maven project

// 创建项目根据目录
~/Desktop$ mkdir MySpringboot

// 按照上面maven的约定创建目录
~/Desktop$ mkdir -p MySpringboot/src/main/java
~/Desktop$ mkdir -p MySpringboot/src/main/resources
~/Desktop$ mkdir -p MySpringboot/src/test/java
~/Desktop$ mkdir -p MySpringboot/src/test/resources

// 在根目录下创建pom.xml文件
~/Desktop$ touch MySpringboot/pom.xml

// 创建好的maven项目目录结构
~/Desktop$ tree MySpringboot
MySpringboot
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   └── resources
    └── test
        ├── java
        └── resources

7 directories, 1 file

2. Import spring boot related dependencies

Add the following configuration to 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.wong</groupId>
    <!--与项目名称一致-->
    <artifactId>MySpringboot</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

The spring-boot-starter-web scene starter will import the dependencies related to web development into the project. For more scene starters, please refer to: springboot scene starter

3. Write the main program: start the Spring Boot application

Create the package name com.wong (actually the directory):

~/Desktop/MySpringboot$ mkdir -p src/main/java/com/wong

Create the main program MainApplication.java in the package com.wong:

package com.wong;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @SpringBootApplication用于标注主程序
*/
@SpringBootApplication
public class MainApplication{

        public static void main(String[] args){
        		// 启动Spring应用
                SpringApplication.run(MainApplication.class,args);
        }
}

4. Write business-related Controller

Create one more package to store the business controller:

~/Desktop/MySpringboot$ mkdir -p src/main/java/com/wong/controller

Create the controller HelloWorldController.java:

package com.wong.controller;

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

@RestController
@RequestMapping("world")
public class HelloWorldController{

        @GetMapping("hi")
        public String index(){
                return "Hello World!";
        }
}

5. Test

Run the program using the mvn spring-boot: run command:

~/Desktop/MySpringboot$ mvn spring-boot:run
[INFO] Scanning for projects...
...
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.5.RELEASE)

2020-03-24 22:13:45.941  INFO 20580 --- [           main] com.wong.MainApplication                 : Starting MainApplication on kyun-HP-348-G3 with PID 20580 (/home/kyun/Desktop/MySpringboot/target/classes started by kyun in /home/kyun/Desktop/MySpringboot)
2020-03-24 22:13:45.944  INFO 20580 --- [           main] com.wong.MainApplication                 : No active profile set, falling back to default profiles: default
2020-03-24 22:13:46.662  INFO 20580 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-03-24 22:13:46.670  INFO 20580 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-03-24 22:13:46.671  INFO 20580 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.31]
2020-03-24 22:13:46.716  INFO 20580 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-03-24 22:13:46.716  INFO 20580 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 720 ms
2020-03-24 22:13:46.852  INFO 20580 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-03-24 22:13:46.967  INFO 20580 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-03-24 22:13:46.969  INFO 20580 --- [           main] com.wong.MainApplication                 : Started MainApplication in 1.298 seconds (JVM running for 1.604)

Enter http: // localhost: 8080 / world / hi in the browser:
 Insert picture description here

7. Simplify deployment

You can release the war package and publish it to the Tomcat container. You can directly type the jar package, and then directly run it with the java -jar <name> .jar command.
First, add the following plug-in to pom.xml:

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

Then execute the following packaging commands:

~/Desktop/MySpringboot$ mvn package spring-boot:repackage

The typed package is in the target directory:

~/Desktop/MySpringboot$ cd target
~/Desktop/MySpringboot/target$ ls
classes                 maven-status
generated-sources       MySpringboot-1.0-SNAPSHOT.jar
generated-test-sources  MySpringboot-1.0-SNAPSHOT.jar.original
maven-archiver          test-classes
~/Desktop/MySpringboot/target$ java -jar MySpringboot-1.0-SNAPSHOT.jar
...

That's all for creating a Spring Boot project.

Published 381 original articles · praised 85 · 80,000 views +

Guess you like

Origin blog.csdn.net/weixin_40763897/article/details/105081785