springboot quickly build project

1. Problem Description

springboot the launch, to be a great boon for Java developers, greatly enhance the efficiency of the development, in fact, just on the basis of springboot maven on the existing maven gav is encapsulated it today with the most simple code QuickStart springboot.

2. Solution

Idea is strongly recommended that you use the paid version (thanks to crack down Lan Yu), Idea of ​​maven, git and other plug-in support of better.

Use idea comes spring Initializr (actual call is the official website of springboot of initializr), fast new springboot project.

2.1 New Springboot project

(1)file->new->project

(2) Click next (first)

Creating springboot project (because foreign sites connected, next sometimes a few seconds delay), two values ​​into its own configuration, Group: com.laowang, Artifact: sptest, the other can not move, click ok

(3) Click next (second)

Select web- "spring web starter

(4) Click the next (third)

Do not make changes directly to finish

New springboot projects have been completed.

2.2 springboot three files generated by default

The default file generated three

2.2.1. 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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.laowang</groupId>
    <artifactId>sptest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sptest</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-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

Focus on a gav: spring-boot-starter-web, the other can be deleted.

2.2.2 application.properties

The file is empty by default, springboot default startup port number: 8080, can be modified to change the file.

2.2.3 Start class file (SptestApplication.java)
package com.laowang.sptest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SptestApplication {

    public static void main(String[] args) {
        SpringApplication.run(SptestApplication.class, args);
    }

}

Focus is on the label: @SpringBootApplication

2.3 verify springboot

Ctroller new packet at com.laowang.sptest reported, and new categories: HelloController

package com.laowang.sptest.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

@Controller
public class HelloController {

    @RequestMapping("/")
    @ResponseBody
    public String getHello() {
        return "hello";
    }
}

Implementation of the results:

Services start properly.

2.4 highlights

Note two things:

(1)类文件要放在跟启动类同级或者下一目录下,本项目为:com.laowang.sptest包下面。因为springboot默认扫描加载启动类同级或者下级目录的标签类(@RestController,@Service ,@Configuraion,@Component等),假如真需要加载其他目录的标签类,可以通过在启动上配置标签@ComponentScan(具体包)来进行扫描加载。

(2)资源文件默认放到resources下面,templates默认放的是动态文件,比如:html文件,不过要搭配thymeleaf 使用(pom文件中需新加gav)。

其他也没什么了,springboot主要是通过spring提供的多个starter和一些默认约定,实现项目的快速搭建。


I’m 「软件老王」,如果觉得还可以的话,关注下呗,后续更新秒知!欢迎讨论区、同名公众号留言交流!

Guess you like

Origin www.cnblogs.com/ruanjianlaowang/p/11222395.html