微服务 SpringBoot 2.0(一):简单入门构建

我作好了从入门到放弃的准备,你却告诉我炒鸡简单 —— Java面试必修html

引言

SpringBoot是由Pivotal团队提供的全新框架,从最根本上来说,Spring Boot就是简化开发人员从0构建项目的繁琐步骤,巧妙的封装了不少插件模块,让开发人员再也不担忧版本依赖或复杂的三方依赖问题,它可以被任意项目的构建系统所使用。java

入门项目

接下来,咱们什么都先不谈,本文着重介绍SpringBoot简单配置与服务搭建,预计花费您5分钟的阅读时间,动起来吧,很是很是简单噢。web

工具

SpringBoot版本:2.0.4
开发工具:IDEA 2018
Maven:3.3 9
JDK:1.8面试

项目快速创建方式一:

首先咱们在SPRING INITIALIZR 上建一个简单项目,并导入到IDEA中,以下图:redis

 
详见网址

 

 
下一步,导入到工程
 
下一步,选择已有模块
 
下一步,更改maven变量
 
下一步,选择JDK1.8目录
 
简单项目创建完成
项目快速创建方式二(经常使用):

步骤 File—>New—>Projectspring

 
手动快速创建

 

 
输入maven信息
 
选择web,下一步

下一步,而后直接完成,选择new window便可数据库

 
建立完成,下面咱们解析刚刚涉及到的几个点

 

工程结构

DemoApplication.java:应用程序启动入口,可直接Run启动服务,相似于tomcat的start.sh
DemoApplicationTests.java:Junit测试类,已自动注入加载了SpringBoot容器的上下文
application.properties:配置属性空文件,可改成application.yml文件,SpringBoot都能识别
pom.xml:maven工程定义文件,代表该项目的maven坐标信息json

疑问解析

  1. 构建项目时为什么选择了Spring Initializr
    答:spring initializr 是Spring 官方提供的一个很好的工具,用来初始化一个Spring boot 的项目
  2. spring initializr有两种用法。一是在官网建立而后导入到编辑器,二是直接File->New->Project

SpringBoot 之pom.xml

如下简称xml,xml中与普通maven项目的xml无太多差别,以下:浏览器

 

 
SpringBoot的pom.xml
pom差别解析

差别一. 引入了该parent说明具有了SpringBoot的基本功能,可直接依赖其父工程(SpringBoot)的包,如差别二(无需声明版本号)缓存

差别二. web应用启动核心jar,解压出来里面除了些依赖什么都没有,因此Starter主要用来简化依赖用的,好比咱们以前作MVC时要引入日志组件,那么须要去找到log4j的版本,而后引入,如今有了Starter以后,直接用这个以后,log4j就自动引入了,也不用关心版本这些问题,注:若想更改其下某一个jar(如log4j)的版本,则可自行进行升降

差别三. 可以将Spring Boot应用打包为可执行的jar或war文件,而后以一般的方式运行Spring Boot应用

独特实现(不经常使用)

若是你不想使用spring-boot-starter-parent,或您本身有一套parent依赖标准,您仍然能够经过使用scope = import依赖关系来保持依赖关系管理:

<dependencyManagement>
     <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.0.4.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

 

该设置不容许您使用如上所述的属性(properties)覆盖各个依赖项,要实现相同的结果,您须要在spring-boot-dependencies项以前的项目的dependencyManagement中添加一个配置,例如,要升级到另外一个Spring Data版本系列,您能够将如下内容添加到本身的pom.xml中。

<dependencyManagement>
    <dependencies>
        <!-- Override Spring Data release train provided by Spring Boot -->

        <!--Spring Data版本更改至Kay-SR9 |变动部分  start-->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-releasetrain</artifactId>
            <version>Kay-SR9</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
        <!--注意:需啊哟在spring-boot-dependencies以前加入需更改的   |变动部分 end -->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.0.4.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>


    </dependencies>
</dependencyManagement>

 

此处详见官方文档:https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-yaml-shortcomings 模块:13.2.2 Using Spring Boot without the Parent POM

经常使用依赖模块

Spring Boot提供了不少已封装好的模块,相似于插件,拿来即用,大多都是spring-boot-starter-xx风格,若是要用直接引入便可,就像组装电脑,组装i3仍是装i5的CPU看你 本身,下面咱们随便举例几个:

<!--快速web应用开发-->
<artifactId>spring-boot-starter-web</artifactId>

<!--redis缓存服务-->
<artifactId>spring-boot-starter-redis</artifactId>

<!--应用日志-->
<artifactId>spring-boot-starter-logging</artifactId>

<!--容器层约定和定制-->
<artifactId>spring-boot-starter-jetty</artifactId>
<artifactId>spring-boot-starter-undertow</artifactId>

<!--数据库访问-->
<artifactId>spring-boot-starter-jdbc</artifactId>

<!--面向切面-->
<artifactId>spring-boot-starter-aop</artifactId>

<!--应用安全-->
<artifactId>spring-boot-starter-security</artifactId>

 

应用演示

以咱们刚刚新建的DemoApplication.java为例
1.pom.xml文件加入web服务插件(呀,是谁这么聪明,之前弄个springmvc一套下来10来个jar,如今只管一个了)

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

 

  1. 咱们直接在注解上面加入@RestController,而且加入一个RequestMapping方法,启动服务器以后,咱们访问这个方法便可看到效果
package com.ron.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class DemoApplication {

    @RequestMapping("/index")
    public String index(){
        return "Hello Spring Boot";
    }

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

    }
}

 

  1. @RestController注解等价于@Controller+@ResponseBody的结合,使用这个注解的类里面的方法都以json格式输出
  2. @SpringBootApplication是Sprnig Boot项目的核心注解,主要目的是开启自动配置。后续讲解原理的时候深刻介绍。
  3. main方法这是一个标准的Java应用的main的方法,主要做用是做为项目启动的入口。
run运行
 
启动成功
打开浏览器访问
 
成功访问

单元测试场景

找到项目目录了src/test/下的测试入口,编写简单的http请求来测试;使用mockmvc进行,利用MockMvcResultHandlers.print()打印出执行结果。

package com.ron.demo;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {

    private MockMvc mvc;
    @Before
    public void setUp() throws Exception {   <!--此处为须要测试的Controller类-->
        mvc = MockMvcBuilders.standaloneSetup(new DemoApplication()).build();
    }

    @Test
    public void contextLoads() throws Exception {
        mvc.perform(MockMvcRequestBuilders.get("/index").accept(MediaType.APPLICATION_JSON))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(MockMvcResultHandlers.print())
                .andReturn();

    }

}

 

直接在DemoApplicationTests 中 Ctrl+Shift+F10运行便可看到以下运行结果,若报错请仔细检查@Before方法

 
控制台运行结果

热部署配置(会重启)

工欲善其事,必先利其器。在开发的时候,不免会反复进行修改调试,就目前而言,修改了代码后是没法直接编译生效,因此须要咱们添加如下依赖,添加后必定要确保已经依赖噢

  1. 添加以下依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>

 

2.plugin中加入以下

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

                <!--加入部分 start-->
                <configuration>
                    <fork>true</fork>
                </configuration>
                <!--加入部分 end-->

            </plugin>
        </plugins>
    </build>

 

  1. 第三步修改IDE


     
    settings
 
Registry

设置完成后重启IDEA便可,本操做在修改代码以后只会作到自动启动服务

总结

会使用SpringBoot以后,老板不再用担忧我写代码的速度,总结下来就是简单、快速、方便!平时若是咱们须要搭建一个spring web项目的时候准备依赖包都要很大一部分时间,如今都不用啦。


做者有话说:喜欢的话就请移步Java面试必修网 https://www.itmsbx.com ,请自备水,更多干、干、干货等着你

相关文章
相关标签/搜索