1. 问题描述

springboot的面世,成为Java开发者的一大福音,大大提升了开发的效率,其实springboot只是在maven的基础上,对已有的maven gav进行了封装而已,今天用最简单的代码快速入门springboot。

2. 解决方案

强烈推荐大家使用Idea的付费版(破解感谢下蓝宇),Idea对maven、git等插件支持的更加好。

使用idea自带的spring Initializr(实际调用的是springboot的官网上的initializr),快速新建springboot项目。

2.1 新建Springboot项目

(1)file->new->project
springboot项目快速构建_springboot
(2)点击next(第一个)

创建springboot项目(因为连接的国外的网站,next有时会几秒的延迟),将两个值改成自己的配置,Group:com.laowang ,Artifact:sptest,其他可以不用动,点击ok

springboot项目快速构建_springboot_02
(3)点击next(第二个)

选择web-》spring web starter

springboot项目快速构建_springboot_03
(4)点击next(第三个)

不用做修改,直接finish

springboot项目快速构建_springboot_04

新建springboot项目已经完成。

2.2 springboot默认生成三个文件

默认生成的三个文件

2.2.1. pom.xml
 1<?xml version="1.0" encoding="UTF-8"?>
 2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4    <modelVersion>4.0.0</modelVersion>
 5    <parent>
 6        <groupId>org.springframework.boot</groupId>
 7        <artifactId>spring-boot-starter-parent</artifactId>
 8        <version>2.1.4.RELEASE</version>
 9        <relativePath/> <!-- lookup parent from repository -->
10    </parent>
11    <groupId>com.laowang</groupId>
12    <artifactId>sptest</artifactId>
13    <version>0.0.1-SNAPSHOT</version>
14    <name>sptest</name>
15    <description>Demo project for Spring Boot</description>
16
17    <properties>
18        <java.version>1.8</java.version>
19    </properties>
20
21    <dependencies>
22        <dependency>
23            <groupId>org.springframework.boot</groupId>
24            <artifactId>spring-boot-starter-web</artifactId>
25        </dependency>
26        <dependency>
27            <groupId>org.springframework.boot</groupId>
28            <artifactId>spring-boot-starter-test</artifactId>
29            <scope>test</scope>
30        </dependency>
31    </dependencies>
32
33    <build>
34        <plugins>
35            <plugin>
36                <groupId>org.springframework.boot</groupId>
37                <artifactId>spring-boot-maven-plugin</artifactId>
38            </plugin>
39        </plugins>
40    </build>
41
42</project>

重点就一个gav:spring-boot-starter-web,其他可以删除。

2.2.2 application.properties

该文件默认为空,springboot的默认启动端口号:8080,可以在改文件修改。

2.2.3 启动类文件(SptestApplication.java)
 1package com.laowang.sptest;
 2
 3import org.springframework.boot.SpringApplication;
 4import org.springframework.boot.autoconfigure.SpringBootApplication;
 5
 6@SpringBootApplication
 7public class SptestApplication {
 8
 9    public static void main(String[] args) {
10        SpringApplication.run(SptestApplication.class, args);
11    }
12
13}

重点是标签:@SpringBootApplication

2.3 验证springboot

在com.laowang.sptest报下新建ctroller包,并新建类:HelloController

 1package com.laowang.sptest.controller;
 2
 3import org.springframework.stereotype.Controller;
 4import org.springframework.web.bind.annotation.RequestMapping;
 5import org.springframework.web.bind.annotation.ResponseBody;
 6
 7import java.util.HashMap;
 8
 9@Controller
10public class HelloController {
11
12    @RequestMapping("/")
13    @ResponseBody
14    public String getHello() {
15        return "hello";
16    }
17}

执行效果:

springboot项目快速构建_springboot_05

服务正常启动。

2.4 重点说明

需要说明两点:

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

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

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


 

springboot项目快速构建_springboot_06