SpringBoot基础的依赖说明
SpringBoot基础的依赖说明
前⾔
前阵⼦,为了赶进度,项⽬在构建的时候,没有仔细研究相关依赖的特性、⽤处,⽽是囫囵吞枣,这边copy⼀点,那边copy⼀点,结果被⼤佬问及相关依赖的作⽤时,就死机了,实在不应该,项⽬再赶,也需要挤时间去了解。
前提
官⽹给出的推荐如下,但我的maven为3.0.X版本,截⽌⽬前还没遇到过什么差错。
1. JDK 1.8 or later
2. Gradle 4+ or Maven
3.2+
spring-boot-starter-parent
The spring-boot-starter-parent project is a special starter project – that provides default configurations for our application and a complete dependency tree to quickly build our Spring Boot project. It also provides default
configuration for Maven plugins such as maven-failsafe-plugin, maven-jar-plugin, maven-surefire-plugin, maven-war-plugin. Beyond that, it also inherits dependency management from spring-boot-dependencies which is the parent to the spring-boot-starter-parent.
这个依赖是最常见的,通常⽤作⽗模块的依赖,来构建⼀个包含多个⼦模块的SpringBoot应⽤。在官⽹的解释中,它提供Springboot的默认配置,和⼀棵完整的SpringBoot依赖树,还包括maven的打包插件。
通过spring-boot-starter-parent依赖,在各个⼦模块中,我们引申的各个SpringBoot依赖甚⾄连版本号都可以省略,只需要指定spring-boot-starter-parent的依赖。此时,⼦模块的各个SpringBoot依赖会默认为⽗依赖中的版本。
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
这⾥,有⼈可能不喜欢引⽤spring-boot-starter-parent来拓展各个⼦模块,也有⼈是因为需要继承⾃⼰的⽗模块,⽽maven⼜不允许有多个“⽗类”,在官⽹,它也给出了解决⽅案:
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
通过<scope>import</scope>标签,导⼊spring-boot-dependencies依赖,其实就⼀个pom⽂件,该pom⽂件⼜通
过<dependencyManagement>标签去寻各个SpringBoot相关依赖,就可以达到相同的效果。
spring-boot-starter-web
以spring-boot-starter-web:2.0.1.RELEASE版本为例⼦,它包含的依赖如下:
1. spring-boot-starter:
2.0.1.RELEASE
Spring Boot的核⼼启动器,包含了⾃动配置、⽇志和YAML
3. spring-boot-starter-json:2.0.1.RELEASE
Spring Boot 提供了 Jackson 的⾃动配置,⽽JackJson来源于 spring-boot-
starter-json这个包
5. spring-boot-starter-tomcat:2.0.1.RELEASE
SpringBoot封装了Tomcat,并把它作为默认的容器,当然,我们也可以进⾏修改为jetty,两者都是作为servlet容器,据说jetty在长连接上⽐较具有优势:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
</dependencies>
7. hibernate-validator:6.0.9.Final
⼀个验证器,它可以帮我们验证⼀些参数的格式,譬如是否为空,是否为⼿机格式。
9. spring-web:5.0.5.RELEASE
从给出的⽂档中,我们可以知道,该模块会进⾏IOC容器的初始化⼯作,⽽且提供了其他的Web框架和HTTP技术。
10. spring-webmvc:5.0.5.RELEASE
spring-webmvc则是在spring-web上的进⼀步延伸,其实从依赖也可以看出,两者引⼊的依赖有重复的部分,spring-webmvc是Spring MVC的实现,如果我们不想⽤springMVC⽽只是⽤到其他web相关的技术,那我们可以在引⼊的时候将这个依赖排除。
The Web layer consists of the spring-web, spring-webmvc, spring-
websocket,and spring-webmvc-portlet modules.
The spring-web module provides basic web-oriented integration
features such as multipart file upload functionality and the
initialization of the IoC container using Servlet listeners and
a web-oriented application context. It also contains an HTTP client
and the web-related parts of Spring’s remoting support.
The spring-webmvc module (also known as the Web-Servlet module)
contains Spring’s model-view-controller (MVC)and REST Web
Services implementation for web applications. Spring’s MVC framework
provides a clean separation between domain model code and web
forms and integrates with all of the other features of the
Spring Framework.
The spring-webmvc-portlet module (also known as the Web-Portlet
module) provides the MVC implementation to be used in a
Portlet environment and mirrors the functionality of the Servlet-based
spring-webmvc module.
spring-boot-configuration-processor
关于这个依赖,有点⽞学,⾸先,我可以肯定关于的数据源的配置是没有问题的,我的mapper接⼝、xml也都是正确⽆误的,⽽我的数据源配置在了yml⽂件中,springboot本⾝默认的读取⽂件格式就包含了yml⽂件,这怎么就报错了??
15:19:25.150 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'XXXController':
Error creating bean with name 'XXXChainMapper' defined in file:
Unsatisfied dependency expressed through method 'sqlSessionFactory'
parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested ex ception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to de termine a suitable driver class
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
15:19:25.262[main] ERROR o.s.b.d.LoggingFailureAnalysisReporter -
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to auto-configure a DataSource:'spring.datasource.url' is not specified and no embedded datasource could be auto-configured.
通过对⽐之前的项⽬,我发现现在的项⽬中莫名其妙的多出这个依赖,于是在试着把它删除之后,竟然神奇的解决了这个bug。问题到这⼉还没结束,当我写篇⽂章的时候,想着重新复现这个问题的时候,⼜重新将这个依赖加回去时,发现尽然没报错了what?查看了⼀下类路径,配置⽂件这时都被加载进去了??即使clean了⼀遍,重新运⾏也任然ok,百思不得解。
结合官⽅的⽂档,我们可以知道,这个依赖的作⽤是:可以通过注解的⽅式来配置元数据(数据源?)。通过这个包,我们还可以在在类中构造配置信息,不过这种⽅式个⼈不太感冒,它让整个项⽬结构变得有、乱,也不容易修改和拓展。⽬前项⽬⾥头这个依赖已经都去掉了。
You can easily generate your own configuration metadata file from
items annotated with @ConfigurationProperties by using the spring-
boot-configuration-processor jar. The jar includes a Java annotation
processor which is invoked as your project is compiled. To use the
processor, include a dependency on spring-boot-configuration-
processor.
mybatis-spring-boot-starter
项⽬⾥头的持久层⽤的mybatis(也可以是mybatis-plus、hibernate等),所以还要引⼊这个依赖。现在的mybatis不⽤像⽼版本那样⽤很多“累赘”的xml⽂件进⾏配置,⽽是提供了注解的⽅式,也⽀持逆向⼯程,从⽽提⾼开发效率。
druid-spring-boot-starter
数据库连接池采⽤的是druid,⼝碑不错,提供了SQL级别的可视化监控⼯具,只⽀持JDK 6以上版本,更多详细⽂档请移⾄官⽅⽂档链接:
附带⼀些配置和说明
# 申明数据源
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url:
username:
password:
driverClassName: sql.jdbc.Driver
#初始化时建⽴物理连接的个数。初始化发⽣在显⽰调⽤init⽅法,或者第⼀次getConnection时
initialSize:5
#最⼩连接池数量
minIdle:5
#最⼤连接池数量
maxActive:20
#获取连接时最⼤等待时间,单位毫秒。配置了maxWait之后,缺省启⽤公平锁,并发效率会有所下降,
#如果需要可以通过配置useUnfairLock属性为true使⽤⾮公平锁。
maxWait:60000
#Destroy线程会检测连接的间隔时间
#testWhileIdle的判断依据
timeBetweenEvictionRunsMillis:60000
minEvictableIdleTimeMillis:300000
#⽤来检测连接是否有效的sql
validationQuery: SELECT 1
#建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间⼤于
#timeBetweenEvictionRunsMillis,执⾏validationQuery检测连接是否有效。
testWhileIdle:true
#申请连接时执⾏validationQuery检测连接是否有效,做了这个配置会降低性能。
testOnBorrow:false
#归还连接时执⾏validationQuery检测连接是否有效,做了这个配置会降低性能。
testOnReturn:false
#是否缓存preparedStatement,也就是PSCache。PSCache对⽀持游标的数据库性能提升巨⼤,
#⽐如说oracle。在mysql下建议关闭。
poolPreparedStatements:true
#当数据库抛出⼀些不可恢复的异常时,抛弃连接
exceptionSorter:true
#属性类型是字符串,通过别名的⽅式配置扩展插件,常⽤的插件有:
#监控统计⽤的filter:stat
#⽇志⽤的filter:log4j
#防御sql注⼊的filter:wall
filters: stat,wall,log4j
#要启⽤PSCache,必须配置⼤于0,当⼤于0时,poolPreparedStatements⾃动触发修改为true。
#在Druid中,不会存在Oracle下PSCache占⽤内存过多的问题,可以把这个数值配置⼤⼀些,⽐如说100
maxPoolPreparedStatementPerConnectionSize:20
#通过connectProperties属性来打开mergeSql功能;慢SQL记录
#合并执⾏的相同sql,避免因为参数不同⽽统计多条sql语句
connectionProperties: Sql=true;druid.stat.slowSqlMillis=500
#合并多个DruidDataSource的监控数据
useGlobalDataSourceStat:true
Demo
相关配置说明
<build>
<!--配置打包后名字-->
<finalName>web1-service</finalName>
<resources>
<resource>
<!--配置可被访问的资源的位置,不像被访问到如环境配置可⽤excludes标签-->                <directory>src/main/resources</directory>
<includes>
<include>application.properties</include>
<include>**/*.xml</include>
</includes>
<excludes>
<exclude>env/**</exclude>
</excludes>
<filtering>false</filtering>
</resource>
<!--加载的环境配置默认是profiles.active指向的⽂件-->springmvc常用标签
<resource>
<directory>src/main/resources/env/${profiles.active}</directory>
</resource>
</resources>
<!--配置springBoot打包插件-->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<jvmArguments>-ding=UTF-8</jvmArguments>
<mainClass>com.web1.Web1ServerApplication</mainClass>
</configuration>
<executions>
<execution>
<goals>
<!--可以把依赖的包都打包到⽣成的Jar包中 -->
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<!--如果在开发⼀个库,直接使⽤默认的maven-jar-plugin插件即可;-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。