slf4j需要什么jar包_「源码解析」凭什么?spring boot 一个 jar 就能开发 web 项目

1239e8b68cf07b1887aabdc89179bee8.png

问题

为什么开发web项目,spring-boot-starter-web 一个jar就搞定了?这个jar做了什么?

通过 spring-boot 工程可以看到所有开箱即用的的引导模块 spring-boot-starter-xxx 都在 spring-boot-starters 子模块中,且所有的 spring-boot-starter-xxx 模块中都没有代码,都是在其他包中就完成对应的功能。首先,分析其依赖

依赖

724a3565cec81ac988ed378be09a614d.png

注意:图中的 Jakarta.xxxx 包是原来的 javax.xxxx 包,Java EE 改名为 Jakarta EE 了,spring-boot-starter-web-2.1.8.RELEASE 版本是直接依赖于hibernate-validator,spring-boot-2.2.0 版本开始使用的是 Jakarta,并用一个新模块 spring-boot-starter-validation 来管理

从依赖图中可以看到,最核心的 spring-boot 依赖于 spring-context 和 spring-core ,因此,正如官方所说,spring-boot 是基于 spring 的。

spring boot可以轻松地创建可运行的、独立的、生产级的基于spring的应用程序。我们对spring平台和第三方库有自己的见解,这样您就可以从最少的麻烦开始了。大多数spring引导应用程序只需要很少的spring配置。

可以使用 Spring Boot 创建Java应用程序,Java应用程序可以通过使用 java -jar 或更传统的war 来部署。我们还提供了一个运行“spring脚本”的命令行工具。

我们首要的目标是:

* 为所有spring开发提供一个更快、更容易获得的入门体验。

* 开箱即用,但要在需求开始偏离默认值时迅速改变。

* 提供一系列对大型项目通用的非功能性特性(如嵌入式服务器、安全性、流量、运行状况检查和外部化配置)。

* 绝对不需要代码生成,也不需要XML配置。

「spring-boot 源码解析」spring-boot 依赖管理

「spring-boot 源码解析」spring-boot 依赖管理梳理图

spring-boot-starter(重要)

此模块是所有 spring-boot-starter-xxxx 引导器核心,非常重要!!!

包含以下模块:

org.springframework.boot spring-bootorg.springframework.boot spring-boot-autoconfigureorg.springframework.boot spring-boot-starter-loggingjavax.annotation javax.annotation-apiorg.springframework spring-coreorg.yaml snakeyaml runtime

spring-boot

spring-boot 内核,spring-boot 特性功能都是在此包实现。

2de2bcfa321c0bbfbd32c059a21fd88f.png

spring-boot-autoconfigure

spring-boot 自动配置,提供一些常用包的默认配置

「源码解析」自动配置的这些细节不知道,别说你会 springboot

spring-boot-starter-logging

spring-boot 默认日志引导器

ch.qos.logback logback-classicorg.apache.logging.log4j log4j-to-slf4jorg.slf4j jul-to-slf4j

什么都没做,就引入了几个依赖,那引入这几个依赖解决了什么问题呢?通过引入这几个依赖,直接或间接的引入了 slf4j、logback日志框架所需jar,以及 log4j、jul日志工具对 Slf4j的适配。因此,引入了这个jar,工程中的日志实现使用 logback。嗯?那 log4j2 呢?原来如果想用 log4j2,还有一个 spring-boot-starter-log4j2 包供我们选择。

日志的具体配置,建议还是用原本的 xml 配置,用 yaml 或 properties 不好配置。如:logback 使用根目录下的 logback-spring.xml 配置。

snakeyaml

支持yaml语法的生成与解析工具包

https://www.jianshu.com/p/d8136c913e52

spring-boot-starter-tomcat

没有做任何处理,直接使用嵌入式 tomcat 相关jar。spring 项目由外部 tomcat 调用 spring框架,而 spring-boot 是由 框架内部去调用嵌入式 tomcat,主被动关系发生了转化。

javax.annotation javax.annotation-apiorg.apache.tomcat.embed tomcat-embed-core org.apache.tomcat tomcat-annotations-api org.apache.tomcat.embed tomcat-embed-elorg.apache.tomcat.embed tomcat-embed-websocket

spring-boot-starter-json

没有做任何处理,使用 jackson 作为默认json工具

org.springframework.boot spring-boot-starterorg.springframework spring-webcom.fasterxml.jackson.core jackson-databindcom.fasterxml.jackson.datatype jackson-datatype-jdk8com.fasterxml.jackson.datatype jackson-datatype-jsr310com.fasterxml.jackson.module jackson-module-parameter-names

spring-boot-starter-validation

2.1.8.RELEASE 版本直接依赖于 hibernate-validator,没有此模块

2.2.0 版本依赖于 Jakarta.validation-api 和 hibernate-validator,并去掉了 hibernate-validator中的 javax.validation-api。

两者在使用的时候没有任何区别,是无感切换的。

validator 自动化校验

jakarta.validation jakarta.validation-apiorg.apache.tomcat.embed tomcat-embed-elorg.hibernate.validator hibernate-validator javax.validation validation-api 

参考资料

  • spring-boot-2.1.8 源码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/D:/develop/maven/apache-maven-3.3.9/repo/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/D:/project/gjyw/ibms/ibms-service/lib/pierce-sdk-1.0.0-with-jdk8.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder] Exception in thread "main" java.lang.IllegalArgumentException: Cannot instantiate interface org.springframework.context.ApplicationListener : org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor at org.springframework.boot.SpringApplication.createSpringFactoriesInstances(SpringApplication.java:439) at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:418) at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:409) at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:261) at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:240) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1242) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1230) at com.sdhsie.ibms.Application.main(Application.java:14) Caused by: java.lang.IllegalArgumentException: class org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor is not assignable to interface org.springframework.context.ApplicationListener at org.springframework.util.Assert.assignableCheckFailed(Assert.java:655) at org.springframework.util.Assert.isAssignable(Assert.java:586) at org.springframework.util.Assert.isAssignable(Assert.java:617) at org.springframework.boot.SpringApplication.createSpringFactoriesInstances(SpringApplication.java:432) ... 7 more 如何解决
最新发布
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值