1. Spring Boot概述

在这一部分,主要了解以下3个问题:

  • 什么是Spring Boot
  • 为什么要学习Spring Boot
  • Spring Boot的特点

1.1 什么是Spring Boot

Spring Boot是Spring项目中的一个子工程,与我们所熟知的Spring-framework 同属于spring的产品:
首页Spring Boot简介可以看到下面的一段介绍:

Spring Boot is designed to get you up and running as quickly as possible, with minimal upfront configuration of Spring. Spring Boot takes an opinionated view of building production-readyapplications.

翻译一下:

Spring Boot的设计目的是让您尽可能快地启动和运行,而无需预先配置Spring。Spring Boot以一种固定的方式来构建可用于生产级别的应用程序。

一般把Spring Boot称为搭建程序的脚手架 或者说是便捷搭建 基于Spring的工程 脚手架。其最主要作用就是帮助开发人员快速的构建庞大的spring项目,并且尽可能的减少一切xml配置,做到开箱即用,迅速上手,让开发人员关注业务而非配置。

1.2 为什么要学习Spring Boot

java一直被人诟病的一点就是臃肿、麻烦。当我们还在辛苦的搭建项目时,可能Python程序员已经把功能写好了,究其原因注意是两点:

  • 复杂的配置
    项目各种配置其实是开发时的损耗, 因为在思考 Spring 特性配置和解决业务问题之间需要进行思维切换,所以写配置挤占了写应用程序逻辑的时间。

  • 一个是混乱的依赖管理
    项目的依赖管理也是件吃力不讨好的事情。决定项目里要用哪些库就已经够让人头痛的了,你还要知道这些库的哪个版本和其他库不会有冲突,这难题实在太棘手。并且,依赖管理也是一种损耗,添加依赖不是写应用程序代码。一旦选错了依赖的版本,随之而来的不兼容问题毫无疑问会是生产力杀手。

而Spring Boot让这一切成为过去!

Spring Boot 简化了基于Spring的应用开发,只需要“run”就能创建一个独立的、生产级别的Spring应用。Spring Boot为Spring平台及第三方库提供开箱即用的设置(提供默认设置,存放默认配置的包就是启动器starter),这样我们就可以简单的开始。多数Spring Boot应用只需要很少的Spring配置。

我们可以使用Spring Boot创建java应用,并使用java –jar 启动它,就能得到一个生产级别的web工程。

1.3 Spring Boot的特点

Spring Boot 主要特点是:

  • 创建独立的Spring应用,为所有 Spring 的开发者提供一个非常快速的、广泛接受的入门体验
  • 直接嵌入应用服务器,如tomcat、jetty、undertow等;不需要去部署war包
  • 提供固定的启动器依赖去简化组件配置;实现开箱即用(启动器starter-其实就是Spring Boot提供的一个jar包),通过自己设置参数(.properties或.yml的配置文件),即可快速使用。
  • 自动地配置Spring和其它有需要的第三方依赖
  • 提供了一些大型项目中常见的非功能性特性,如内嵌服务器、安全、指标,健康检测、外部化配置等
  • 绝对没有代码生成,也无需 XML 配置。

更多细节,大家可以到官网查看。

2. 快速入门

利用Spring Boot搭建一个web工程,体会一下Spring Boot的魅力所在!

2.1 创建工程

新建一个maven jar工程:

  1. New Project

  2. Next

  3. 基础信息

  4. Finish

2.2 添加依赖

  • 看到这里很多同学会有疑惑,前面说传统开发的问题之一就是依赖管理混乱,怎么这里我们还需要管理依赖呢?难道Spring Boot不帮我们管理吗?

  • 别着急,现在我们的项目与Spring Boot还没有什么关联。Spring Boot提供了一个名为spring-boot-starter-parent的工程,里面已经对各种常用依赖(并非全部)的版本进行了管理,我们的项目需要以这个项目为父工程,这样我们就不用操心依赖的版本问题了,需要什么依赖,直接引入坐标即可!

2.2.1 添加父工程坐标

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.5.RELEASE</version>
</parent>

2.2.2 添加web启动器

为了让Spring Boot帮我们完成各种自动配置,我们必须引入Spring Boot提供的自动配置依赖,我们称为启动器。因为我们是web项目,这里我们引入web启动器,在pom.xml文件中加入如下依赖:

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

需要注意的是,我们并没有在这里指定版本信息。因为Spring Boot的父工程已经对版本进行了管理了。

这个时候,我们会发现项目中多出了大量的依赖。

那些依赖都是Spring Boot根据spring-boot-starter-web这个依赖自动引入的,而且所有的版本都已经管理好,不会出现冲突。

2.2.3 管理jdk版本

如果我们想要修改Spring Boot项目的jdk版本,只需要简单的添加以下属性即可,如果没有需求,则不添加。同样的在pom.xml文件中添加如下:

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

2.2.4 完整pom文件

<?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.itheima</groupId><artifactId>heima-springboot</artifactId><version>1.0-SNAPSHOT</version><parent><artifactId>spring-boot-starter-parent</artifactId><groupId>org.springframework.boot</groupId><version>2.1.5.RELEASE</version></parent><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies>
</project>

2.3 启动类

Spring Boot项目通过main函数即可启动,我们需要创建一个启动类:

package com.itheima;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** spring boot 工程都有一个启动引导类,这是工程的入口类* 并在引导类上添加@SpringBootApplication*/
@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}

spring boot 工程都有一个启动引导类,这是工程的入口类。并在引导类上添加@SpringBootApplication。

2.4 编写controller

接下来,就可以像以前那样开发SpringMVC的项目了!
编写heima-springboot\src\main\java\com\itheima\controller\HelloController.java

代码如下:

package com.itheima.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class HelloController {@GetMapping("hello")public String hello() {return "Hello,Spring boot";}
}

2.5 启动测试

接下来,运行main函数,查看控制台:

并且可以看到监听的端口信息:

  • 监听的端口是8080
  • SpringMVC的项目路径是:空
  • /hello 路径已经映射到了HelloController 中的hello() 方法

打开页面访问:http://localhost:8080/hello

测试成功了!

2.6 小结

  1. Spring Boot工程可以通过添加启动器依赖和创建启动引导类实现快速创建web工程。
  2. spring-boot-starter-web默认的应用服务器端口是8080。

3. Java配置应用

在入门案例中,我们没有任何的配置,就可以实现一个SpringMVC的项目了,快速、高效!

但是疑问,如果没有任何的xml,那么我们如果要配置一个Bean该怎么办?比如我们要配置一个数据库连接池,以前会这么配置:

<!-- 配置连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" />
</bean>

现在该怎么做呢?

3.1 Spring配置历史

事实上,在Spring3.0开始,Spring官方就已经开始推荐使用java配置来代替传统的xml配置了,我们不妨来回顾一下Spring的历史:

  • Spring1.0时代
    在此时因为jdk1.5刚刚出来,注解开发并未盛行,因此一切Spring配置都是xml格式,想象一下所有的bean都用xml配置,细思极恐啊,心疼那个时候的程序员2秒
  • Spring2.0时代
    Spring引入了注解开发,但是因为并不完善,因此并未完全替代xml,此时的程序员往往是把xml与注解进行结合,貌似我们之前都是这种方式。
  • Spring3.0及以后
    3.0以后Spring的注解已经非常完善了,因此Spring推荐大家使用完全的java配置来代替以前的xml,不过似乎在国内并未推广盛行。然后当Spring Boot来临,人们才慢慢认识到java配置的优雅。

有句古话说的好:拥抱变化,拥抱未来。所以我们也应该顺应时代潮流,做时尚的弄潮儿,一起来学习下java配置的玩法。

3.2 尝试Java配置

Java配置主要靠Java类和一些注解,比较常用的注解有:

  • @Configuration :声明一个类作为配置类,代替xml文件
  • @Bean :声明在方法上,将方法的返回值加入Bean容器,代替 标签
  • @Value :属性注入
  • @PropertySource :指定外部属性文件

我们接下来用java配置来尝试实现连接池配置:

  1. pom.xml 文件中添加Druid连接池依赖如下

    <dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.6</version>
    </dependency>
    

  2. 使用数据操作工具创建数据库 springboot_test

  3. 然后在项目中创建heima-springboot\src\main\resources\jdbc.properties文件,内容如下

    jdbc.driverClassName=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/springboot_test?characterEncoding=utf-8&serverTimezone=UTC&useSSL=false
    jdbc.username=root
    jdbc.password=131415
    

  4. 编写 heima-springboot\src\main\java\com\itheima\config\JdbcConfig.java 如下

    package 1 com.itheima.config;import com.alibaba.druid.pool.DruidDataSource;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;import javax.sql.DataSource;@Configuration
    @PropertySource("classpath:jdbc.properties")
    public class JdbcConfig {@Value("${jdbc.url}")String url;@Value("${jdbc.driverClassName}")String driverClassName;@Value("${jdbc.username}")String username;@Value("${jdbc.password}")String password;@Beanpublic DataSource dataSource(){DruidDataSource dataSource = new DruidDataSource();dataSource.setDriverClassName(driverClassName);dataSource.setUrl(url);dataSource.setUsername(username);dataSource.setPassword(password);return dataSource;}
    }
    

    解读:

    • @Configuration :声明我们JdbcConfig 是一个配置类
    • @PropertySource :指定属性文件的路径是: classpath:jdbc.properties
    • 通过@Value为属性注入值
    • 通过@BeandataSource() 方法声明为一个注册Bean的方法,Spring会自动调用该方法,将方法的返回值加入Spring容器中。

    然后我们就可以在任意位置通过@Autowired 注入DataSource了!

  5. 在HelloController 中注入DataSource进行测试,改造代码如下:

    package com.itheima.controller;import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;import javax.sql.DataSource;@RestController
    public class HelloController {@Autowiredprivate DataSource dataSource;@GetMapping("hello")public String hello() {System.out.println("Datasource = " + dataSource);return "Hello,Spring boot";}
    }

    然后打断点,Debug运行并查看:
    属性注入成功了!

3.3 Spring Boot的属性注入

属性文件的名称有变化,默认的文件名必须是:application.properties或application.yml

在上面的案例中,我们实验了java配置方式。不过属性注入使用的是@Value注解。这种方式虽然可行,但是不够强大,因为它只能注入基本类型值。

在Spring Boot中,提供了一种新的属性注入方式,支持各种java基本数据类型及复杂类型的注入。

  1. 新建heimaspringboot\src\main\java\com\itheima\config\JdbcProperties.java,用于进行属性注入:

    package com.itheima.config;import org.springframework.boot.context.properties.ConfigurationProperties;/*** ConfigurationProperties:从application配置文件中读取配置项* prefix 表示 配置项的前缀* 配置项类中的变量名必须要与前缀之后的配置项名称保持 松散绑定(相同)*/
    @ConfigurationProperties(prefix = "jdbc")
    public class JdbcProperties {private String url;private String driverClassName;private String username;private String password;public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}public String getDriverClassName() {return driverClassName;}public void setDriverClassName(String driverClassName) {this.driverClassName = driverClassName;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}
    }
    • 在类上通过@ConfigurationProperties注解声明当前类为属性读取类

    • prefix="jdbc" 读取属性文件中,前缀为jdbc的值。

    • 在类上定义各个属性,名称必须与属性文件中jdbc.后面部分一致

    • 需要注意的是,这里我们并没有指定属性文件的地址,所以我们需要把jdbc.properties名称改为application.properties,这是Spring Boot默认读取的属性文件名:
      【注意】如果出现如下提示,项目也可以运行;如果要去掉上述的提示,则可以在pom.xml文件中添加如下依赖:

      <dependency><groupId> org.springframework.boot </groupId><artifactId>spring-boot-configuration-processor</artifactId><!--不传递依赖--><optional>true</optional>
      </dependency>
      
  2. JdbcConfig 类原来全部注释掉或删除,修改为如下内容:

    package com.itheima.config;import com.alibaba.druid.pool.DruidDataSource;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;import javax.sql.DataSource;@Configuration
    @EnableConfigurationProperties(JdbcProperties.class)
    public class JdbcConfig {@Beanpublic DataSource dataSource(JdbcProperties jdbcProperties) {DruidDataSource dataSource = new DruidDataSource();dataSource.setDriverClassName(jdbcProperties.getDriverClassName());dataSource.setUrl(jdbcProperties.getUrl());dataSource.setUsername(jdbcProperties.getUsername());dataSource.setPassword(jdbcProperties.getPassword());return dataSource;}
    }

    • 通过@EnableConfigurationProperties(JdbcProperties.class) 来声明要使用JdbcProperties 这个类的对象
    • 然后要使用配置的话;可以通过以下方式注入JdbcProperties:
      ① @Autowired注入
      ② 构造函数注入
      ③ 声明有@Bean的方法参数注入
  3. 测试结果:与前面测试一样

大家会觉得这种方式似乎更麻烦了,事实上这种方式有更强大的功能,也是Spring Boot推荐的注入方式。与@Value对比关系:
优势:Relaxed binding:松散绑定

  • 不严格要求属性文件中的属性名与成员变量名一致。支持驼峰,中划线,下划线等等转换,甚至支持对象引导。比如:user.friend.name:代表的是user对象中的friend属性中的name属性,显然friend也是对象。@value注解就难以完成这样的注入方式。
  • meta-data support:元数据支持,帮助IDE生成属性提示(写开源框架会用到)

3.4 更优雅的注入

事实上,如果一段属性只有一个Bean需要使用,我们无需将其注入到一个类(JdbcProperties,将该类上的所有注解去掉)中。而是直接在需要的地方声明即可;再次修改JdbcConfig类为如下代码:

@Configuration
public class JdbcConfig {@Bean// 声明要注入的属性前缀,Spring Boot会自动把相关属性通过set方法注入到DataSource中@ConfigurationProperties(prefix = "jdbc")public DataSource dataSource() {return new DruidDataSource();}
}

我们直接把@ConfigurationProperties(prefix = "jdbc") 声明在需要使用的@Bean 的方法上,然后Spring Boot就会自动调用这个Bean(此处是DataSource)的set方法,然后完成注入。使用的前提是:该类必须有对应属性的set方法!

我们将jdbc.properties 文件中的jdbc.url中的数据库名改成:/heima,再次测试:

3.5 Yaml配置文件

配置文件除了可以使用application.properties类型,还可以使用后缀名为:.yml或者.yaml的类型,也就是:application.yml或者application.yaml

基本格式:

jdbc:driverClassName: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/springboot_test?characterEncoding=utf-8&serverTimezone=UTC&useSSL=falseusername: rootpassword: 131415

把application.properties修改为application.yml进行测试。

如果两个配置文件都有,会把两个文件的配置合并,如果有重复属性,以properties中的为准。
如果是配置数组、list、set等结构的内容,那么在yml文件中格式为:

yml配置文件的特征:

  1. 树状层级结构展示配置项
  2. 配置项之间如果有关系的话需要分行空两格
  3. 配置项如果有值的话,需要在之后空一格再写配置项值

3.6 多个Yaml配置文件

当一个项目中有多个yml配置文件的时候,可以以application-**.yml命名;在application.yml中配置项目使用激活这些配置文件即可。
创建application-abc.yml文件如下:

itcast:url: http://www.itcast.cn

创建application-def.yml 文件如下:

itheima:url: http://www.itheima.com

application.yml 文件中添加如下配置:

#加载其它配置文件
spring:profiles:active: abc,def

多个文件名只需要写application-之后的名称,在多个文件之间使用,隔开。
修改代码测试:

4. 自动配置原理

使用Spring Boot之后,一个整合了SpringMVC的WEB工程开发,变的无比简单,那些繁杂的配置都消失不见了,这是如何做到的?

一切魔力的开始,都是从我们的main函数来的,所以我们再次来看下启动类:

我们发现特别的地方有两个:

  • 注解:@SpringBootApplication
  • run方法:SpringApplication.run()

我们分别来研究这两个部分。

4.1 了解@SpringBootApplication

点击进入,查看源码:

这里重点的注解有3个:

  • @SpringBootConfiguration
  • @EnableAutoConfiguration
  • @ComponentScan

4.1.1 @SpringBootConfiguration

我们继续点击查看源码:
通过这段我们可以看出,在这个注解上面,又有一个@Configuration 注解。通过上面的注释阅读我们知道:这个注解的作用就是声明当前类是一个配置类,然后Spring会自动扫描到添加了@Configuration 的类,并且读取其中的配置信息。而@SpringBootConfiguration 是来声明当前类是SpringBoot应用的配置类,项目中只能有一个。所以一般我们无需自己添加。

4.1.2 @EnableAutoConfiguration

关于这个注解,官网上有一段说明:

The second class-level annotation is @EnableAutoConfiguration . This annotation tells Spring Boot to“guess” how you want to configure Spring, based on the jar dependencies that you have added. Since spring-boot-starter-web added Tomcat and Spring MVC, the auto-configuration assumes that you aredeveloping a web application and sets up Spring accordingly.

简单翻译以下:

第二级的注解@EnableAutoConfiguration ,告诉Spring Boot基于你所添加的依赖,去“猜测”你想要如何配置Spring。比如我们引入了spring-boot-starter-web ,而这个启动器中帮我们添加了tomcat 、SpringMVC的依赖。此时自动配置就知道你是要开发一个web应用,所以就帮你完成了web及SpringMVC的默认配置了!

总结,Spring Boot内部对大量的第三方库或Spring内部库进行了默认配置,这些配置是否生效,取决于我们是否引入了对应库所需的依赖,如果有那么默认配置就会生效。

所以,我们使用SpringBoot构建一个项目,只需要引入所需框架的依赖,配置就可以交给SpringBoot处理了。除非你不希望使用SpringBoot的默认配置,它也提供了自定义配置的入口。

4.1.3 @ComponentScan

我们跟进源码:
并没有看到什么特殊的地方。我们查看注释:
大概的意思:

配置组件扫描的指令。提供了类似与<context:component-scan> 标签的作用
通过basePackageClasses或者basePackages属性来指定要扫描的包。如果没有指定这些属性,那么将从声明
这个注解的类所在的包开始,扫描包及子包

而我们的@SpringBootApplication注解声明的类就是main函数所在的启动类,因此扫描的包是该类所在包及其子包。因此,一般启动类会放在一个比较前的包目录中

4.2 默认配置原理

4.2.1 spring.factories

在SpringApplication类构建的时候,有这样一段初始化代码:

跟进去:
这里发现会通过loadFactoryNames尝试加载一些FactoryName,然后利用createSpringFactoriesInstances将这些加载到的类名进行实例化。

继续跟进loadFactoryNames方法:

发现此处会利用类加载器加载某个文件: FACTORIES_RESOURCE_LOCATION ,然后解析其内容。我们找到这个变量的声明:


可以发现,其地址是:META-INF/spring.factories ,我们知道,ClassLoader默认是从classpath下读取文件,因此,SpringBoot会在初始化的时候,加载所有classpath:META-INF/spring.factories文件,包括jar包当中的。而在Spring的一个依赖包:spring-boot-autoconfigure中,就有这样的文件:

以后我们引入的任何第三方启动器,只要实现自动配置,也都会有类似文件。

4.2.2 默认配置类

我们打开刚才的spring.factories文件:

可以发现以EnableAutoConfiguration接口为key的一系列配置,key所对应的值,就是所有的自动配置类,可以在当前的jar包中找到这些自动配置类:
非常多,几乎涵盖了现在主流的开源框架,例如:

  • redis
  • jms
  • amqp
  • jdbc
  • jackson
  • mongodb
  • jpa
  • solr
  • elasticsearch
  • …等等

我们来看一个我们熟悉的,例如SpringMVC,查看mvc 的自动配置类:
打开WebMvcAutoConfiguration:
我们看到这个类上的4个注解:

  • @Configuration :声明这个类是一个配置类
  • @ConditionalOnWebApplication(type = Type.SERVLET)
    ConditionalOn,翻译就是在某个条件下,此处就是满足项目的类是是Type.SERVLET类型,也就是一个普通web工程,显然我们就是
  • @ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
    这里的条件是OnClass,也就是满足以下类存在:Servlet、DispatcherServlet、WebMvcConfigurer,其中Servlet只要引入了tomcat依赖自然会有,后两个需要引入SpringMVC才会有。这里就是判断你是否引入了相关依赖,引入依赖后该条件成立,当前类的配置才会生效!
  • @ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
    这个条件与上面不同,OnMissingBean,是说环境中没有指定的Bean这个才生效。其实这就是自定义配置的入口,也就是说,如果我们自己配置了一个WebMVCConfigurationSupport的类,那么这个默认配置就会失效!

接着,我们查看该类中定义了什么:

视图解析器:
处理器适配器(HandlerAdapter):
还有很多,这里就不一一截图了。

4.2.3 默认配置属性

另外,这些默认配置的属性来自哪里呢?
我们看到,这里通过@EnableAutoConfiguration注解引入了两个属性:WebMvcProperties和
ResourceProperties。这不正是SpringBoot的属性注入玩法嘛。

我们查看这两个属性类:
如果我们要覆盖这些默认属性,只需要在application.properties中定义与其前缀prefix和字段名一致的属性即可。

4.3 总结

SpringBoot为我们提供了默认配置,而默认配置生效的步骤:

  • @EnableAutoConfiguration注解会去寻找META-INF/spring.factories 文件,读取其中以EnableAutoConfiguration为key的所有类的名称,这些类就是提前写好的自动配置类
  • 这些类都声明了@Configuration注解,并且通过@Bean 注解提前配置了我们所需要的一切实例
  • 但是,这些配置不一定生效,因为有@ConditionalOn注解,满足一定条件才会生效。比如条件之一:是一些相关的类要存在
  • 类要存在,我们只需要引入了相关依赖(启动器),依赖有了条件成立,自动配置生效。
  • 如果我们自己配置了相关Bean,那么会覆盖默认的自动配置的Bean
  • 我们还可以通过配置application.yml文件,来覆盖自动配置中的属性

1)启动器

所以,我们如果不想配置,只需要引入依赖即可,而依赖版本我们也不用操心,因为只要引入了SpringBoot提供的stater(启动器),就会自动管理依赖及版本了。

因此,玩SpringBoot的第一件事情,就是找启动器,SpringBoot提供了大量的默认启动器

2)全局配置

另外,SpringBoot的默认配置,都会读取默认属性,而这些属性可以通过自定义application.properties文件来进行覆盖。这样虽然使用的还是默认配置,但是配置中的值改成了我们自定义的。

因此,玩SpringBoot的第二件事情,就是通过application.properties 来覆盖默认属性值,形成自定义配置。我们需要知道SpringBoot的默认属性key,非常多,可以再idea中自动提示

属性文件支持两种格式,application.properties和application.yml

yml的语法实例:

jdbc:driverClassName: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/springboot_test?characterEncoding=utf-8&serverTimezone=UTC&useSSL=falseusername: rootpassword: 131415server:port: 80

如果properties和yml文件都存在,如果有重叠属性,默认以Properties优先。遇到需要修改的组件的配置项流程为:

5. Spring Boot实践

接下来,我们来看看如何用SpringBoot来整合SSM,在数据库中引入一张用户表tb_user和实体类User。

tb_user表

创建heima-springboot\src\main\java\com\itheima\pojo\User.java 如下:

package com.itheima.pojo;import java.util.Date;public class User {// idprivate Long id;// 用户名private String userName;// 密码private String password;// 姓名private String name;// 年龄private Integer age;// 性别,1男性,2女性private Integer sex;// 出生日期private Date birthday;// 创建时间private Date created;// 更新时间private Date updated;public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Integer getSex() {return sex;}public void setSex(Integer sex) {this.sex = sex;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public Date getCreated() {return created;}public void setCreated(Date created) {this.created = created;}public Date getUpdated() {return updated;}public void setUpdated(Date updated) {this.updated = updated;}public String getNote() {return note;}public void setNote(String note) {this.note = note;}// 备注private String note;
}

5.1 Lombok

我们编写pojo时,经常需要编写构造函数和getter、setter方法,属性多的时候,就非常浪费时间,使用lombok插件可以解决这个问题:

  1. 在IDEA中安装lombok插件
    在IDEA中安装lombok插件,不安装插件在IDEA中使用lombok的注解虽然编译能通过,但是源码会报错。所以为了让IDEA更好的辨别lombok注解则才安装插件。

  2. 添加lombok对应的依赖到项目pom.xml
    需要在maven工程中的pom.xml文件引入依赖:

    <dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId>
    </dependency>
    

  3. 改造实体类使用lombok注解
    然后可以在Bean上使用:
    ① @Data :自动提供getter和setter、hashCode、equals、toString等方法e
    ② @Getter:自动提供getter方法
    ③ @Setter:自动提供setter方法
    ④ @Slf4j:自动在bean中提供log变量,其实用的是slf4j的日志功能。
    例如;在javabean上加@Data,那么就可以省去getter和setter等方法的编写,lombok插件会自动生成。

5.2 整合SpringMVC

虽然默认配置已经可以使用SpringMVC了,不过我们有时候需要进行自定义配置。

可以在application.yml文件中配置日志级别控制:

logging:level:com.itheima: debugorg.springframework: info

5.2.1 修改端口

查看SpringBoot的全局属性可知,端口通过以下方式配置:
修改 application.yml配置文件,添加如下配置:

# 映射端口
server:port: 80


重启服务后测试:

5.2.2 访问静态资源

现在,我们的项目是一个jar工程,那么就没有webapp,我们的静态资源该放哪里呢?

回顾我们在上面看的源码,有一个叫做ResourceProperties的类,里面就定义了静态资源的默认查找路径:


默认的静态资源路径为:

  • classpath:/META-INF/resources/
  • classpath:/resources/
  • classpath:/static/
  • classpath:/public

只要静态资源放在这些目录中任何一个,SpringMVC都会帮我们处理。

我们习惯会把静态资源放在classpath:/static/目录下。我们创建目录 static ,并且从 资料文件夹中复制itcast.giftest.js如下:重启项目后测试:

注意:如果访问图片时候没有显示;可以先将项目先clean再启动,或者创建 public、resources 文件夹,然后图片放置到public或resources中。

5.2.3 添加拦截器

拦截器也是我们经常需要使用的,在SpringBoot中该如何配置呢?

拦截器不是一个普通属性,而是一个类,所以就要用到java配置方式了。在SpringBoot官方文档中有这么一段说明:

If you want to keep Spring Boot MVC features and you want to add additional MVC configuration(interceptors, formatters, view controllers, and other features), you can add your own @Configuration class of type WebMvcConfigurer but without @EnableWebMvc . If you wish to provide custom instances of RequestMappingHandlerMapping , RequestMappingHandlerAdapter , or ExceptionHandlerExceptionResolver , you can declare a WebMvcRegistrationsAdapter instance to provide such components.

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated
with @EnableWebMvc.

总结:通过实现WebMvcConfigurer 并添加@Configuration 注解来实现自定义部分SpringMvc配置。

  1. 创建 heima-springboot\src\main\java\com\itheima\interceptor\MyInterceptor.java 拦截器,内容如下:

    package com.itheima.interceptor;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    @Slf4j
    public class MyInterceptor implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler) throws Exception {log.debug("这是MyInterceptor拦截器的preHandle方法");return true;}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response,Object handler, ModelAndView modelAndView) throws Exception {log.debug("这是MyInterceptor拦截器的postHandle方法");}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponseresponse, Object handler, Exception ex) throws Exception {log.debug("这是MyInterceptor拦截器的afterCompletion方法");}
    }
    

  2. 定义配置类 heima-springboot\src\main\java\com\itheima\config\MvcConfig.java ,用于注册拦截
    器,内容如下:

    package com.itheima.config;
    import com.itheima.interceptor.MyInterceptor;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    @Configuration
    public class MvcConfig implements WebMvcConfigurer {/*** 将拦截器注册到spring ioc容器* @return myInterceptor*/@Beanpublic MyInterceptor myInterceptor(){return new MyInterceptor();}/*** 重写该方法;往拦截器链添加自定义拦截器* @param registry 拦截器链*/@Overridepublic void addInterceptors(InterceptorRegistry registry) {//通过registry添加myInterceptor拦截器,并设置拦截器路径为 /*registry.addInterceptor(myInterceptor()).addPathPatterns("/*");}
    }
    

  3. 结构如下:

  4. 接下来访问http://localhost/hello 并查看日志:

5.3 整合jdbc和事务

spring中的jdbc连接和事务是配置中的重要一环,在SpringBoot中该如何处理呢?

答案是不需要处理,我们只要找到SpringBoot提供的启动器即可,在pom.xml文件中添加如下依赖:

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

当然,不要忘了数据库驱动,SpringBoot并不知道我们用的什么数据库,这里我们选择MySQL;同样的在pom.xml文件中添加如下依赖:

<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.22</version>
</dependency>

至于事务,SpringBoot中通过注解来控制。就是我们熟知的@Transactional使用的时候设置在对应的类或方法上即可。
创建heima-springboot\src\main\java\com\itheima\service\UserService.java业务类如下:

package com.itheima.service;import com.itheima.pojo.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;@Service
public class UserService {// 根据id查询public User queryById(Long id) {//根据id查询return new User();}// 保存用户@Transactionalpublic void saveUser(User user) {System.out.println("新增用户...");}
}

5.4 整合连接池

其实,在刚才引入jdbc启动器的时候,SpringBoot已经自动帮我们引入了一个连接池:HikariCP应该是目前速度最快的连接池了,我们看看它与c3p0的对比:
因此,我们只需要指定连接池参数即可;打开application.yml 添加修改配置如下:

spring:datasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/springboot_testusername: rootpassword: root

【注意】

把 JdbcConfig 类中的druid的配置删除或注释;
在配置完hikari数据库连接池后的application.yml 文件如下:

启动项目,访问 http://localhost/hello ;查看后台输出,一样可以在HelloController中获取到datasource。

5.5 整合MyBatis

5.5.1 MyBatis

  1. SpringBoot官方并没有提供Mybatis的启动器,不过Mybatis官网自己实现了。在项目的pom.xml 文件中加入如下依赖:

    <!--mybatis -->
    <dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.0.1</version>
    </dependency>
    

  2. 配置application.yml,常用配置如下:

    # mybatis配置
    mybatis:# 实体类别名包路径type-aliases-package: com.itheima.pojo# 映射文件路径# mapper-locations: classpath:mappers/*.xmlconfiguration:# 控制台输出执行sqllog-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    

  3. 配置Mapper扫描
    需要注意,这里没有配置mapper接口扫描包,因此我们需要给每一个Mapper接口添加@Mapper 注解,才能被识别。

    @Mapper
    public interface UserMapper {}
    

    或者,我们也可以不加注解,而是在启动类上添加扫描包注解(推荐):

    package com.itheima;import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;/*** spring boot 工程都有一个启动引导类,这是工程的入口类* 并在引导类上添加@SpringBootApplication*/
    @SpringBootApplication
    @MapperScan("com.itheima.mapper")
    public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
    }

    以下代码示例中,我们将采用@MapperScan扫描方式进行。

5.5.2 通用mapper

通用mapper:可以实现自动拼接sql语句;所有的mapper都不需要编写任何方法也就是不用编写sql语句。

  1. 通用Mapper的作者也为自己的插件编写了启动器,我们直接引入即可。在项目的pom.xml 文件中加入如下依赖:

    <!-- 通用mapper -->
    <dependency><groupId>tk.mybatis</groupId><artifactId>mapper-spring-boot-starter</artifactId><version>2.1.5</version>
    </dependency>
    

    注意:一旦引入了通用Mapper的启动器,会覆盖Mybatis官方启动器的功能,因此需要移除对官方Mybatis启动器的依赖

  2. 编写UserMapper
    无需任何配置就可以使用了。如果有特殊需要,可以到通用mapper官网查看
    编写heima-springboot\src\main\java\com\itheima\mapper\UserMapper.java 如下:

    package com.itheima.mapper;import com.itheima.pojo.User;
    import tk.mybatis.mapper.common.Mapper;public interface UserMapper extends Mapper<User> {}
    

  3. 把启动类上的@MapperScan注解修改为通用mapper中自带的:引用tk开头的

  4. 在User实体类上加JPA注解
    修改heima-springboot\src\main\java\com\itheima\pojo\User.jav 如下:
    如果数据库字段名称和实体名称差别很大,需要用Column注解指定字段对应。

    package com.itheima.pojo;import lombok.Data;
    import tk.mybatis.mapper.annotation.KeySql;import javax.persistence.Column;
    import javax.persistence.Id;
    import javax.persistence.Table;
    import java.util.Date;@Data
    @Table(name = "tb_user")
    public class User {@Id// 主键回填@KeySql(useGeneratedKeys = true)private Long id;// 如果数据库字段名称和实体名称差别很大,需要用Column注解指定// @Column(name = "abc")private String userName;private String password;private String name;private Integer age;private Integer sex;private Date birthday;private Date created;private Date updated;private String note;
    }

  5. UserService的代码进行简单改造

    package com.itheima.service;import com.itheima.mapper.UserMapper;
    import com.itheima.pojo.User;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;@Service
    public class UserService {@Autowiredprivate UserMapper userMapper;// 根据id查询public User queryById(Long id) {//根据id查询//return new User();return userMapper.selectByPrimaryKey(id);}// 保存用户@Transactionalpublic void saveUser(User user) {System.out.println("新增用户... ");//选择性新增;如果属性为空则该属性不会出现在insert语句上userMapper.insertSelective(user);}
    }
    

5.6 启动测试

HelloController进行简单改造:

package com.itheima.controller;import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;import javax.sql.DataSource;@RestController
public class HelloController {@Autowiredprivate DataSource dataSource;@Value("${itcast.url}")private String itcastUrl;@Value("${itheima.url}")private String itheimaUrl;@Autowiredprivate UserService userService;/*** 根据用户id查询用户* @param id 用户id* @return 用户*/@GetMapping("/user/{id}")public User queryById(@PathVariable Long id){return userService.queryById(id);}@GetMapping("hello")public String hello(){System.out.println(" itcastUrl = " + itcastUrl);System.out.println(" itheimaUrl = " + itheimaUrl);System.out.println(" DataSource = " + dataSource);return "Hello, Spring Boot!";}}

我们启动项目,查看:

5.7 Junit测试

Ctrl+shift+t快速给一个类添加测试类

  1. 在springboot项目中如果要使用Junit进行单元测试,则需要添加如下的依赖:

    <!--Junit-->
    <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    

  2. 在测试包下编写测试类
    在测试类上面必须要添加 @SpringBootTest注解。
    编写测试类heima-springboot\src\test\java\com\itheima\service\UserServiceTest.java

    package com.itheima.service;import com.itheima.pojo.User;
    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.test.context.junit4.SpringRunner;import java.util.Date;import static org.junit.Assert.*;@RunWith(SpringRunner.class)
    @SpringBootTest
    public class UserServiceTest {@Autowiredprivate UserService userService;@Testpublic void queryById() {User user = userService.queryById(8L);System.out.println(user);}@Testpublic void saveUser() {User user = new User();user.setId(13L);user.setAge(18);user.setUsername("kai");user.setPassword("123");user.setBirthday(new Date());userService.saveUser(user);}
    }
    

    测试代码结构如:

在Spring Boot项目中,如果要编写测试类,则必须要在类上面添加@SpringBootTest

5.8 整合Redis

redis五种数据类型:string、list、set、hash、sort set

  1. pom.xml文件中添加如下依赖;

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

  2. 配置application.yml文件;

    spring:redis:host: localhostport: 6379
    

  3. 编写src\test\java\com\itheima\redis\RedisTest.java 测试代码;

    package com.itheima.redis;
    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.data.redis.core.RedisTemplate;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import org.springframework.test.context.junit4.SpringRunner;
    import java.util.List;
    import java.util.Set;
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class RedisTest {@Autowiredprivate RedisTemplate redisTemplate;@Testpublic void test(){//string字符串//redisTemplate.opsForValue().set("str", "heima");redisTemplate.boundValueOps("str").set("heima");System.out.println("str = " + redisTemplate.opsForValue().get("str"));//hash散列redisTemplate.boundHashOps("h_key").put("name", "黑马");redisTemplate.boundHashOps("h_key").put("age", 13);//获取所有域对应的值Set set = redisTemplate.boundHashOps("h_key").keys();System.out.println("hash散列所有的域:" + set);List list = redisTemplate.boundHashOps("h_key").values();System.out.println("hash散列所有的域值:" + list);//list列表redisTemplate.boundListOps("l_key").leftPush("c");redisTemplate.boundListOps("l_key").leftPush("b");redisTemplate.boundListOps("l_key").leftPush("a");list = redisTemplate.boundListOps("l_key").range(0, -1);System.out.println("列表的值:" + list);//set集合redisTemplate.boundSetOps("set_key").add("a", "b", "c");set = redisTemplate.boundSetOps("set_key").members();System.out.println("集合的元素:" + set);//sorted set有序集合redisTemplate.boundZSetOps("z_key").add("a", 30);redisTemplate.boundZSetOps("z_key").add("b", 20);redisTemplate.boundZSetOps("z_key").add("c", 10);set = redisTemplate.boundZSetOps("z_key").range(0, -1);System.out.println("有序集合的元素:" + set);}
    }
    
  4. 运行上述代码测试

6. Spring Boot项目部署

6.1 项目打包

  1. 添加项目的pom.xml插件;在pom.xml要显式的加入插件spring-boot-maven-plugin,否则无法产生 jar 清单
    文件,导致打出来的 jar 无法使用命令运行;

    <build><plugins><!-- 打jar包时如果不配置该插件,打出来的jar包没有清单文件 --><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins>
    </build>
    

  2. 使用maven的命令package打包;

    之后在项目下的target目录中将有如下jar包:

【注意】在查看打出的 jar 的时候,将发现 jar 包里面包含 jar 包;这样的包称为 fatJar(肥包)

6.2 运行

运行打出来的包;使用命令:java –jar 包全名 或者写一个 bat 文件,里面包含 java –jar 包全名;这样就可以双击启动应用。

如执行上述打出来的jar的命令为:

java -jar heima-springboot-1 1.0-SNAPSHOT.jar


测试则可使用浏览器访问:http://localhost/user/8

7. 附录—插件安装

在应用spring boot工程的时候;一般情况下都需要创建启动引导类Application.java和application.yml配置文件,而且内容都是一样的;为了便捷可以安装一个IDEA的插件 JBLSpringBootAppGen 在项目上右击之后可以自动生成启动引导类Application.java和application.yml配置文件。

7.1 安装插件

7.2 应用插件

在IDEA中任意一个maven项目或src目录上 右击,选择JBLSpringBootAppGen 即可。

在如下的界面中输入 启动引导类的名称并根据需要勾选是否要生成application.yml配置文件。

点击 OK 之后,在项目中将发现如下内容:

8. Spring Boot小结

1. Spring Boot概述

  1. Spring Boot是一个搭建基于spring工程的脚手架
  2. 简化配置、依赖管理
  3. 特点:快速搭建、内嵌应用服务器、自动配置、无代码生成、也没有xml配置

2. 快速入门

  1. 引入父依赖,指定spring boot版本2.1.5
  2. 添加启动器依赖
  3. 编写启动引导类
  4. 编写处理器

3. Spring Boot配置

  1. @ConfigurationProperties:将配置文件中的配置项读取到类中

  2. yaml配置文件

    ① 是一种树状层级的配置文件

    ② 功能与properties配置文件一致

    ③ 在Spring Boot项目中配置文件可以是:

    1. application.properties
    2. application.yaml
    3. application.yml

    ④ 可以在Spring Boot项目中使用多个yml配置文件,需要激活

4. 自动配置原理

  1. 所有的自动配置类都在spring.factories文件中定义,根据启动器依赖实例化

  2. 配置流程

    ① 查找spring-boot-autoconfigure-***.jar

    ② 查找当前组件对应在上述jar包中的package

    ③ 查看**Properties配置项类

    ④ 到Spring Boot的application.yml配置文件中修改配置项

5. Spring Boot整合

  1. lombok:可以在实体类中简化各种方法的get/set/toString

  2. 静态资源的放置

  3. jdbc和事务

    ① 默认连接池 hikari

    ② @Transactional

  4. MyBatis

    ① 添加MyBatis官方的启动器

    ② 配置启动引导类中的mapper扫描

    ③ 通用mapper

  5. junit:需要在测试类上添加@SpringBootTest

  6. redis:可以使用redis Template操作redis

6. 项目打包部署

  1. 利用插件将项目打包成一个jar包(跳过测试):使用maven的package
  2. java -jar 报名运行项目

Java进阶:SpringBoot相关推荐

  1. 超仪电子 java面试_全靠这份阿里大佬的“Java进阶面试手册”助我收获蚂蚁金服offer!...

    都2021年了,你的工资涨了吗? 对于即将到来的金三银四跳槽涨薪季,我想很多小伙伴都已经摩拳擦掌了吧!当然,我猜也有很多小伙伴是下图的状态吧!毕竟面试一年比一年难[落泪] 不得不说现在面试确实很难,现 ...

  2. Java进阶,Set集合,Map集合

    Java进阶,Set集合,Map集合 一.Set系列集合 1.Set系列集系概述 Set系列集合特点 无序:存取顺序不一致 不重复:可以去除重复 无索引:没有带索引的方法,所以不能使用普通for循环遍 ...

  3. Java进阶3 - 易错知识点整理(待更新)

    Java进阶3 - 易错知识点整理(待更新) 该章节是Java进阶2- 易错知识点整理的续篇: 在前一章节中介绍了 ORM框架,中间件相关的面试题,而在该章节中主要记录关于项目部署中间件,监控与性能优 ...

  4. 2018.7-2019.7一周年Java进阶架构师技术文章整理 建议收藏

    其实师长的公众号从2017年就开始发技术文章了,但是因为某些原因(就是懒)太监了许久,直到2018.7的时候才恢复更新.不知不觉中,已经更新了一年的广告,在没广告的日子里,顺带更新技术文章,截至201 ...

  5. 【转】【Books】史上最全的Java进阶书籍推荐

    [转载] 学习Java,书籍是必不可少的学习工具之一,尤其是对于自学者而言.废话不多说,下边就给大家推荐一些Java进阶的好书. 第一部分:Java语言篇 1.<Java编程规范> 适合对 ...

  6. 【书籍学习】史上最全的Java进阶书籍推荐

    学习Java,书籍是必不可少的学习工具之一,尤其是对于自学者而言.废话不多说,下边就给大家推荐一些Java进阶的好书. 第一部分:Java语言篇 1.<Java编程规范> 适合对象:初级. ...

  7. 阿里P8大能倾力编撰的“Java 进阶面试手册”,助力跳槽外包毕业生秋招收获大厂offer

    先来一波致命四连问! 都快2022年9月份了,你的工资涨了吗?跳槽成功了吗?秋招面试收到offer了吗?找着对象了吗? 这波问题问的是无语凝咽呀,对于即将到来的金九银十跳槽涨薪季,我想很多小伙伴都已经 ...

  8. 哪吒社区Java技能树|springboot中的常用任务

    前言 给大家推荐一个知识交流社区 哪吒社区 在这里新手小白可以得到十分有力度技术依靠, 进阶的小伙伴可以在大佬的文章中得到提升 学习较深的大佬们可以互相讨论技术的心得 springboot中的常用任务 ...

  9. java B2B2C Springboot电子商务平台源码-Feign 基本使用

    1. [microcloud-consumer-feign]为了可以使用到 feign 支持,需要修改 pom.xml 配置文件,引入相关依赖包:需要JAVA Spring Cloud大型企业分布式微 ...

  10. java B2B2C Springboot仿淘宝电子商城系统-负载均衡之ribbon+feign

    一. feign简介 Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单.使用Feign,只需要创建一个接口并注解.它具有可插拔的注解特性,可使用Feign注解和JAX-RS注 ...

最新文章

  1. Myeclipse 8.0 +Flash builder 4 plugin 的实现
  2. 脉冲神经网络在目标检测的首次尝试,性能堪比CNN | AAAI 2020
  3. python和c哪个好学-零基础学C好还是python?
  4. 如何写优雅的SQL原生语句?
  5. python第10天(上)
  6. 小冰发布全球首款人工智能Office,沈向洋:我们不走寻常路
  7. 选择不相交区间(贪心算法) By ACReaper
  8. RestFul风格学习
  9. vue3.0项目服务器部署
  10. 毛玻璃效果 php,CSS3如何实现磨砂玻璃背景效果
  11. (转)Inno Setup入门(三)——指定压缩方式
  12. Windows配置MinGW环境变量
  13. 软件设计师教程笔记整理
  14. 短视频视频数据分析 5个解析
  15. 了解React Native组件,模板和工具
  16. Android系统架构和应用程序基本概念详解
  17. BCM wifi分析
  18. 项目管理过程-5个管理过程组、10大管理知识域以及对应输入、工具技术和输出
  19. Mac虚拟机实现ios UI自动化教程-最新版本(MacOS 12.1,ios15.1)
  20. ev3编程变量模块_英文视频教学翻译-机器人ev3编程学习的第二十讲:举例讲解数据变量模块编...

热门文章

  1. 3. LAMP 安装与配置
  2. 9.打开ZF的错误提示
  3. 21. PHP 表单验证 - 完成表单实例
  4. Python 判断字符属于数字、字母、空格
  5. duilib学习 --- 360demo 学习
  6. Visual Studio中创建混合移动应用程序解决方案Xamarin Portable Razor
  7. 允许更新此预编译站点的作用
  8. 3个开源TTS(三)flite的简要分析与espeak的选择
  9. Ajax请求生成中文乱码问题
  10. 群晖通过Cloud Sync套件进行文件同步