一起来学 SpringBoot | 第一篇:构建第一个 SpringBoot 工程

2018-06-24 09:14:04 +08:00
 battcn

SpringBoot 是为了简化 Spring 应用的创建、运行、调试、部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的 XML 配置,我们只需遵循规范,引入相关的依赖就可以轻易的搭建出一个 WEB 工程

未接触 SpringBoot 之前,搭建一个普通的 WEB 工程往往需要花费 30 分钟左右,如果遇到点奇葩的问题耽搁的时间会更长一点,但自从用了SpringBoot 后,真正体会到什么叫分分钟搭建一个WEB,让我拥有更多的时间跟我的小伙伴们唠嗑了。使用 SpringBoot 后发现一切是如此的简单(还记得读书那会被 JAR 包,xml 支配的恐惧吗,如今都可以说 good bye

设计的目标

前提

SpringBoot 为我们提供了一系列的依赖包,所以需要构建工具的支持:MavenGradle。由于本人更习惯使用**Maven所以后续案例都是基于MavenIntelliJ IDEA,同时这里是基于最新的SpringBoot2 编写的哦...**

创建项目

初次接触,我们先来看看如何创建一个Spring Boot项目,这里以IntelliJ IDEA为例,其他的 IDE 工具小伙伴们自行搜索创建方式。创建完项目后,各位小伙伴请认真、细心的对比下与传统的 WEB 工程有何区别(如:目录结构)。

点击**File -> Project**

如果用过 Eclipse/IDEA 等工具的,对创建项目肯定不会陌生,但为了照顾第一次使用的我贴上了图文

选择**Spring Initializr**

到这一步选择的时候,如图中选项的是Spring Initializr(官方的构建插件,需要联网),第二个是自己选择Maven构建,为了更好的适合初学者,我们将在本章用插件构建

填写项目基本信息

选择包

Spring Initializr 为我们提供了很多的选项,不同的选项有不同的作用,在初期我们只需要依赖**Web -> Web** 就可以了,选择好依赖包之后点击**Next -> Finish**

目录结果

- src
    -main
        -java
            -package
                #主函数,启动类,运行它如果运行了 Tomcat、Jetty、Undertow 等容器
                -SpringbootApplication	
        -resouces
            #存放静态资源 js/css/images 等
            - statics
            #存放 html 模板文件
            - templates
            #主要的配置文件,SpringBoot 启动时候会自动加载 application.yml/application.properties		
            - application.yml
    #测试文件存放目录		
    -test
 # pom.xml 文件是 Maven 构建的基础,里面包含了我们所依赖 JAR 和 Plugin 的信息
- pom

pom.xml 依赖

因为使用了 Spring Initializr 插件,所以如下的配置都不需要我们自己去写啦,需要注意的是版本要选择 RELEASE ,稳定版本 BUG 少

<?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.battcn</groupId>
	<artifactId>chapter1</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>chapter1</name>
	<description>我的用第一个 SpringBoot 工程</description>

	<!--版本采用的是最新的 2.0.1.RELEASE TODO 开发中请记得版本一定要选择 RELEASE 哦 -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<!-- 默认就内嵌了 Tomcat 容器,如需要更换容器也极其简单-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- 测试包,当我们使用 mvn package 的时候该包并不会被打入,因为它的生命周期只在 test 之内-->
		<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>

其它的依赖可以参考:官方文档

主函数入口

注意事项: 一个项目中切记不要出现多个 main 函数,否在在打包的时候 spring-boot-maven-plugin 将找不到主函数(主动指定打包主函数入口除外...

/**
 * 我的第一个 SpringBoot 程序
 * 其中 @RestController 等同于 (@Controller 与 @ResponseBody )
 *
 * @author Levin
 */
@RestController
@SpringBootApplication
public class Chapter1Application {

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

    @GetMapping("/demo1")
    public String demo1() {
        return "Hello battcn";
    }

    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        // 目的是
        return args -> {
            System.out.println("来看看 SpringBoot 默认为我们提供的 Bean:");
            String[] beanNames = ctx.getBeanDefinitionNames();
            Arrays.sort(beanNames);
            Arrays.stream(beanNames).forEach(System.out::println);
        };
    }
}

初窥配置文件

从启动日志中可以发现,SpringBoot 默认的端口是 8080,那么如果端口被占用了怎么办呢?不要慌,问题不大,配置文件分分钟解决你的困扰...

2018-04-20 16:14:46.725  INFO 11184 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 ( http) with context path ''

修改默认配置

# 默认的 8080 我们将它改成 9090 
server.port=9090
# 未定义上下文路径之前 地址是 http://localhost:8080 定义了后 http://localhost:9090 你能在 tomcat 做的事情,配置文件都可以
server.servlet.context-path=/chapter1

在启动一次看看日志

2018-04-20 16:47:05.716  INFO 12108 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 9090 ( http) with context path '/chapter1'

测试

本次测试采用 junit 进行,当然也可以启动项目后直接访问 http://localhost:9090/chapter1/demo1 进行测试

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

import java.net.URL;

import static org.junit.Assert.assertEquals;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class Chapter1ApplicationTests {


    @LocalServerPort
    private int port;

    private URL base;

    @Autowired
    private TestRestTemplate template;

    @Before
    public void setUp() throws Exception {
        // TODO 因为我们修改了 content-path 所以请求后面要带上
        this.base = new URL("http://localhost:" + port + "/chapter1/demo1");
    }

    @Test
    public void demo1() throws Exception {
        ResponseEntity<String> response = template.getForEntity(base.toString(), String.class);
        assertEquals(response.getBody(), "Hello battcn");
    }
}

拓展知识

自定义 Banner

SpringBoot 启动的时候我们可以看到如下内容,这一块其实是可以自定义的哦,而且在 2.X 版本中,它支持的格式从文本扩展到**banner.txt 、banner.jpg 、banner.gif 、banner.jpeg** 等等,只需要在 resouces 目录下添加指定命名的文件即可

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.1.RELEASE)

总结

目前很多大佬都写过关于 SpringBoot 的教程了,如有雷同,请多多包涵,本教程基于最新的 spring-boot-starter-parent:2.0.1.RELEASE编写,包括新版本的特性都会一起介绍...

说点什么

个人博客:http://blog.battcn.com/

全文代码:https://github.com/battcn/spring-boot2-learning/tree/master/chapter1

3667 次点击
所在节点    Java
11 条回复
Nick2VIPUser
2018-06-24 09:38:27 +08:00
支持一下~正好在学这个
Shynoob
2018-06-24 10:15:32 +08:00
在 spring for all 拜读过您的文章,讲的很全,谢谢
battcn
2018-06-24 10:53:56 +08:00
@Shynoob 哈哈 可以关注下我的公众号
POPOEVER
2018-06-24 10:59:58 +08:00
放链接就好了吧,论坛并不适合长文阅读,看得很累人
qiyuey
2018-06-24 11:14:20 +08:00
内容过为基础,同时文章太长,并不太适合 V2,建议放到个人博客,4#说的很好
battcn
2018-06-24 11:20:32 +08:00
@POPOEVER
@qiyuey 明白了
sonyxperia
2018-06-25 09:28:35 +08:00
这里又不是知乎专栏
98jiang
2018-06-25 11:33:20 +08:00
感觉基础很多都是一样的。。。
siddebian
2018-07-25 07:09:18 +08:00
follow 了,学习。
jack80342
2018-08-19 11:45:55 +08:00
最近翻译了最新的 Spring boot 官方文档,欢迎反馈🙃,https://github.com/jack80342/Spring-Boot-Reference-Guide
jack80342
2018-08-19 11:46:43 +08:00

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/465371

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX