springboot項目快速構建

1. 問題描述

springboot的面世,成爲Java開發者的一大福音,大大提高了開發的效率,其實springboot只是在maven的基礎上,對已有的maven gav進行了封裝而已,今天用最簡單的代碼快速入門springboot。html

2. 解決方案

強烈推薦你們使用Idea的付費版(破解感謝下藍宇),Idea對maven、git等插件支持的更加好。java

使用idea自帶的spring Initializr(實際調用的是springboot的官網上的initializr),快速新建springboot項目。git

2.1 新建Springboot項目

(1)file->new->project

(2)點擊next(第一個)

建立springboot項目(由於鏈接的國外的網站,next有時會幾秒的延遲),將兩個值改爲本身的配置,Group:com.laowang ,Artifact:sptest,其餘能夠不用動,點擊okweb

(3)點擊next(第二個)

選擇web-》spring web starterspring

(4)點擊next(第三個)

不用作修改,直接finishapache

新建springboot項目已經完成。springboot

2.2 springboot默認生成三個文件

默認生成的三個文件app

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>

重點就一個gav:spring-boot-starter-web,其餘能夠刪除。maven

2.2.2 application.properties

該文件默認爲空,springboot的默認啓動端口號:8080,能夠在改文件修改。ide

2.2.3 啓動類文件(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);
    }

}

重點是標籤:@SpringBootApplication

2.3 驗證springboot

在com.laowang.sptest報下新建ctroller包,並新建類: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";
    }
}

執行效果:

服務正常啓動。

2.4 重點說明

須要說明兩點:

(1)類文件要放在跟啓動類同級或者下一目錄下,本項目爲:com.laowang.sptest包下面。由於springboot默認掃描加載啓動類同級或者下級目錄的標籤類(@RestController,@Service ,@Configuraion,@Component等),假如真須要加載其餘目錄的標籤類,能夠經過在啓動上配置標籤@ComponentScan(具體包)來進行掃描加載。

(2)資源文件默認放到resources下面,templates默認放的是動態文件,好比:html文件,不過要搭配thymeleaf 使用(pom文件中需新加gav)。

其餘也沒什麼了,springboot主要是經過spring提供的多個starter和一些默認約定,實現項目的快速搭建。


I’m 「軟件老王」,若是以爲還能夠的話,關注下唄,後續更新秒知!歡迎討論區、同名公衆號留言交流!

原文出處:https://www.cnblogs.com/ruanjianlaowang/p/11222395.html

相關文章
相關標籤/搜索