第一篇:构建第一个SpringBoot工程

作者:唐亚峰

个人QQ:1837307557

battcn开源群(适合新手):391619659

微信公众号(欢迎调戏):battcn

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


SpringBoot的诞生

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

设计目标

  1. 为所有使用 Spring 的开发者提供一个更简单,快速的入门体验
  2. 提供一些常见的功能、如监控、WEB容器,健康,安全等功能
  3. 干掉XML,遵循规范,开箱即用

前提

SpringBoot 为我们提供了一系列的依赖包,所以需要构建工具的支持:Maven 或 Gradle。由于本人更习惯使用Maven所以后续案例都是基于Maven 与 IntelliJ IDEA,同时这里是基于最新的SpringBoot2编写的哦...(在这里我自己用的是spring2.1.2的稳定版)

创建项目

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

点击File -> Project

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

                                         

选择Spring Initializr       

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

                                  

填写项目的基本信息

  • Group: 组织ID,一般分为多个段,这里我只说两段,第一段为域,第二段为公司名称。域又分为 org、com、cn等等,其中 org为非营利组织,com为商业组织。如阿里、淘宝(com.alibaba/com.taobao)
  • Artifact: 唯一标识符,一般是项目名称

                            

选择包

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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.battcn</groupId>
    <artifactId>chapter1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>chapter1</name>
    <description>my first SpringBoot project</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <!-- 默认内嵌了Tomcat容器,如需要切换容器则需要将默认的tomcat容器排除再外-->
            <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>
            <version>1.5.3.RELEASE</version>
        </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>

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

主函数入口

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

初窥配置文件

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

2019-02-11 11:14:42.763  INFO 18240 --- [           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

在启动看看日志

2019-02-11 11:20:01.175  INFO 16664 --- [           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.Assert;
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.MalformedURLException;
import java.net.URL;

import static org.junit.Assert.assertEquals;

import static org.junit.Assert.assertEquals;

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 MalformedURLException {
        // 因为修改了 content-path 所以请求后面要带上
        this.base = new URL("http://localhost:"+port+"/chapter1/demo1");
    }
    @Test
    public void demo1(){
        ResponseEntity<String> response = template.getForEntity(base.toString(),String.class);
        Assert.assertEquals(response.getBody(),"Hello batch");
    }

    @Test
    public void contextLoads() {
    }

}


代码:

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

本人代码:链接:https://pan.baidu.com/s/1e_uEg_vT3tdcwrT0ekdSwA  提取码:egpv 

猜你喜欢

转载自blog.csdn.net/zuoyouzouzou/article/details/86981708