一直在构建版本_构建系统与代码结构SpringBoot
从今天开始,我们进入SpringBoot的使用环节,这一部分包含了构建系统,自动配置,如何运行应用程序,自然也包括了一些使用SpringBoot的最佳实践。关于SpringBoot的介绍,Anders在上一次的分享中已经说得很详细了,这里我就不多介绍了。SpringBoot使用起来很方便,很快,走位也很拉风,开箱即用。特别适合这个追求快骚猛的高效开发浪潮。
构建系统与依赖管理
Spring Boot官方强烈建议我们使用具有依赖管理特性的构建系统,并且它也建议我们选择Maven或Gradle。当然,你也可以选择其他诸如Ant之类的构建系统来使用Spring Boot,只不过支持的不是特别好。
- 每一个Spring Boot的稳定版都会提供一组它所支持的依赖清单。在开发的时候,你没必要在你的构建配置中提供那些依赖的版本,因为Spring Boot会为你管理依赖的版本信息。而当你更新Spring Boot版本的时候,这些依赖的版本也会以一种很方便的形式更新。当然,如果有需求的话,你也可以指定然后覆盖Spring Boot建议的版本。
- Spring Boot 的策略清单里不但包含了你在使用Spring Boot时会用到的所有Spring的模块,也包含了一些比较优秀的第三方插件库。这一切的素材对Maven和Gradle来说,都是可用的。每一个稳定的Spring Boot版本都会关联一个特定的Spring版本,高度建议不要改变Spring的版本。
Maven用户可以继承spring-boot-starter-parent来获取一些合理的默认配置。父项目提供了以下的一些配置:
- JDK8作为默认编译
- UTF8作为编码字符集
- 父级项目包含了一系列被普遍使用的类库的版本,它帮你管理了这些依赖的版本,你再也不用在你的pom中配置version了
- 包含了一些比较智能的资源过滤配置
- 还有一些插件配置
- 也允许你在配置文件(application.properties,application.yml)里配置你的过滤规则它的配置文件也支持Spring风格的变量占位符${},而Maven的占位符则变为了@..@,你也可以用resource.delimiter来覆盖
Maven构建的两种方式
如果我们跟着文档一步步走的话,那么毫不犹豫会选择继承这样的一种方式来搭建Spring Boot项目,我们只需要在项目的pom中添加这样一组代码就可以了:
<!-- Inherit defaults from Spring Boot -->
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.5.RELEASE</version>
</parent>
在这里,你需要指定SpringBoot的版本,当然如果你在依赖中导入了其他的Starter的话,那么你可以不用指定.
如果你需要覆盖Starter或SpringBoot中的一些类库的版本时,你只需要在pom的Properties里添加相应的属性来指定版本即可:
<properties><spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version>
</properties>
可以检查 spring-boot-dependencies pom 里的列表,来找到对应模块的配置属性名称
当然,并不是每个人都想要继承Spring-boot-starter-parent的pom,可能基于以下的原因,你不想直接继承spring-boot-starter-parent的pom:
- 你有你自己的团队协作标准,类库依赖标准等等
- 你想要自己精确的掌控项目中的Maven配置,不想让人随意更改项目中类库的版本号
此时,如果大家熟悉Spring的BOM,就知道BOM是可以对Spring版本进行控制的,它可以帮助我们统一我们所使用的Spring相关的版本号.SpringBoot作为Spring出的一款产品,自然也就支持BOM的形式.
如果你不想使用spring-boot-starter-parent
, 你就可以通过使用scope=import
来让自己的项目使用Spring Boot.
<dependencyManagement><dependencies><dependency><!-- Import dependency management from Spring Boot --><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.0.5.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies>
</dependencyManagement>
我们刚刚提到,使用继承的方式,允许我们来改变某一个组件的版本号,并覆盖默认的,当时我们是使用属性配置的形式,我们刚刚又说bom在版本上给了父级项目更大的权限,那么是否是说SpringBoot再使用BOM的时候就绝对无法覆盖默认版本呢?当然不是的.我们可以使用以下方式来实现同样的效果.我们观察到在这里我们在spring-boot-depencies的前方添加了我们想要替换的组件.此时就可以进行覆盖了.并且在使用BOM的时候,只可以使用这样的形式覆盖.
<dependencyManagement><dependencies><!-- Override Spring Data release train provided by Spring Boot --><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-releasetrain</artifactId><version>Fowler-SR2</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.0.5.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies>
</dependencyManagement>
关于SpringBoot的可执行jar
在之前的项目开发中,如果我们想要部署一个web项目,是需要依赖web服务器的,比如tomcat,或者apache,但SpringBoot则不同,他提供了一个maven插件,允许我们直接将项目打包成一个可执行的jar文件.大家如果感兴趣,可以用命令行看看这两个文件的内容,或许会有"惊喜"发现.这里不给你卖关子了,你会发现这两个一个是普通的jar文件,另一个则是一个可执行的二进制文件.spring的maven插件配置如下:
<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins>
</build>
SpringBoot中的Starters
我们刚刚提到使用Spring Boot搭建项目很快,为什么呢?因为它有Starters.那什么是Starters呢?SpringBoot非常人性化的将常用技术分类,并把他们提供成一个公共的模块,而你只需要引入一个公共的模块,这个模块所依赖的相关类库就会自动的引入到项目中.有点儿类似于我们的项目模版.,这是SpringBoot非常方便的一个地方.也因此,用SpringBoot搭建一个项目,要比用传统方式快很多。
关于Starters,有几点要说的,首先就是名字.SpringBoot不但自己提供了Starters,它也允许我们定义自己的Starters.官方定义的一般都是spring-boot-starter-*,而如果是我们自定义的或者是第三方的名字则为:*-spring-boot-starter
这里最明显也是最有参考价值的,应该就是mybatis了.mybatis一直都不怎么受Spring待见,所以别的(如hibernate)都是Spring出适配,而到了Mybatis这里,几乎都是Mybatis出适配,所以我们在使用的时候看到的mybatis-spring而不是spring-mybatis.这里也是,在SpringBoot提供的众多组件中,仍然没有Mybatis的,但Mybatis则出了一个可以适配Spring-boot的starters:mybatis-spring-boot .
SpringBoot官方定义的Starters.这些你可以不用死记硬背
NameDescriptionPom
spring-boot-starter
Core starter, including auto-configuration support, logging and YAML
Pom
spring-boot-starter-activemq
Starter for JMS messaging using Apache ActiveMQ
Pom
spring-boot-starter-amqp
Starter for using Spring AMQP and Rabbit MQ
Pom
spring-boot-starter-aop
Starter for aspect-oriented programming with Spring AOP and AspectJ
Pom
spring-boot-starter-artemis
Starter for JMS messaging using Apache Artemis
Pom
spring-boot-starter-batch
Starter for using Spring Batch
Pom
spring-boot-starter-cache
Starter for using Spring Framework’s caching support
Pom
spring-boot-starter-cloud-connectors
Starter for using Spring Cloud Connectors which simplifies connecting to services in cloud platforms like Cloud Foundry and Heroku
Pom
spring-boot-starter-data-cassandra
Starter for using Cassandra distributed database and Spring Data Cassandra
Pom
spring-boot-starter-data-cassandra-reactive
Starter for using Cassandra distributed database and Spring Data Cassandra Reactive
Pom
spring-boot-starter-data-couchbase
Starter for using Couchbase document-oriented database and Spring Data Couchbase
Pom
spring-boot-starter-data-couchbase-reactive
Starter for using Couchbase document-oriented database and Spring Data Couchbase Reactive
Pom
spring-boot-starter-data-elasticsearch
Starter for using Elasticsearch search and analytics engine and Spring Data Elasticsearch
Pom
spring-boot-starter-data-jpa
Starter for using Spring Data JPA with Hibernate
Pom
spring-boot-starter-data-ldap
Starter for using Spring Data LDAP
Pom
spring-boot-starter-data-mongodb
Starter for using MongoDB document-oriented database and Spring Data MongoDB
Pom
spring-boot-starter-data-mongodb-reactive
Starter for using MongoDB document-oriented database and Spring Data MongoDB Reactive
Pom
spring-boot-starter-data-neo4j
Starter for using Neo4j graph database and Spring Data Neo4j
Pom
spring-boot-starter-data-redis
Starter for using Redis key-value data store with Spring Data Redis and the Lettuce client
Pom
spring-boot-starter-data-redis-reactive
Starter for using Redis key-value data store with Spring Data Redis reactive and the Lettuce client
Pom
spring-boot-starter-data-rest
Starter for exposing Spring Data repositories over REST using Spring Data REST
Pom
spring-boot-starter-data-solr
Starter for using the Apache Solr search platform with Spring Data Solr
Pom
spring-boot-starter-freemarker
Starter for building MVC web applications using FreeMarker views
Pom
spring-boot-starter-groovy-templates
Starter for building MVC web applications using Groovy Templates views
Pom
spring-boot-starter-hateoas
Starter for building hypermedia-based RESTful web application with Spring MVC and Spring HATEOAS
Pom
spring-boot-starter-integration
Starter for using Spring Integration
Pom
spring-boot-starter-jdbc
Starter for using JDBC with the HikariCP connection pool
Pom
spring-boot-starter-jersey
Starter for building RESTful web applications using JAX-RS and Jersey. An alternative to spring-boot-starter-web
Pom
spring-boot-starter-jooq
Starter for using jOOQ to access SQL databases. An alternative to spring-boot-starter-data-jpa
or spring-boot-starter-jdbc
Pom
spring-boot-starter-json
Starter for reading and writing json
Pom
spring-boot-starter-jta-atomikos
Starter for JTA transactions using Atomikos
Pom
spring-boot-starter-jta-bitronix
Starter for JTA transactions using Bitronix
Pom
spring-boot-starter-jta-narayana
Starter for JTA transactions using Narayana
Pom
spring-boot-starter-mail
Starter for using Java Mail and Spring Framework’s email sending support
Pom
spring-boot-starter-mustache
Starter for building web applications using Mustache views
Pom
spring-boot-starter-quartz
Starter for using the Quartz scheduler
Pom
spring-boot-starter-security
Starter for using Spring Security
Pom
spring-boot-starter-test
Starter for testing Spring Boot applications with libraries including JUnit, Hamcrest and Mockito
Pom
spring-boot-starter-thymeleaf
Starter for building MVC web applications using Thymeleaf views
Pom
spring-boot-starter-validation
Starter for using Java Bean Validation with Hibernate Validator
Pom
spring-boot-starter-web
Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container
Pom
spring-boot-starter-web-services
Starter for using Spring Web Services
Pom
spring-boot-starter-webflux
Starter for building WebFlux applications using Spring Framework’s Reactive Web support
Pom
spring-boot-starter-websocket
Starter for building WebSocket applications using Spring Framework’s WebSocket support
Pom
SpringBoot实践中的最佳代码结构
每一种技术都有其对应的组织代码的方式,比如我们在我们所熟悉的mvc里,我们通常会将实体类放入到Model包里,控制层,我们则放入到Controller里,然后视图层则相应的放入到View里.业务层放到Service层中,而数据库操作一般放入到dao层或者mapper里.为什么这么放呢?其实并没有什么强制性的要求,而是大家达成的一种约定.最后,这样的约定大于配置,就成了项目组织方式的一种最佳实践.说起约定大于配置,Maven就是最佳的例子.
虽然SpringBoot没有强制使用某一种代码布局,但他也推荐了关于这方面的最佳实践.
- 首先,关于包,默认包这种方式应该被避免.因为这个会导致在使用SpringBoot的@ComponentScan, @EntityScan, 或 @SpringBootApplication注解的时候出现一些比较奇葩的问题.关于包,SpringBoot建议我们使用Java里面的那种方式,域名的倒写.
- 其次,SpringBoot建议我们将SpringBoot的加载器放到我们的根目录下,与model和controller同级,然后 @SpringBootApplication这个注解是要放在SpringBoot加载器的class上,@SpringBootApplication可以简单的被理解为是@EnableAutoConfiguration 和 @ComponentScan的合集
这个是代码的目录组织方式:
com+- example+- myapplication+- Application.java|+- customer| +- Customer.java| +- CustomerController.java| +- CustomerService.java| +- CustomerRepository.java|+- order+- Order.java+- OrderController.java+- OrderService.java+- OrderRepository.java
这个是SpringBoot的加载器.
package com.example.myapplication;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class Application { public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}
后记
SpringBoot,很多人喜欢他主要是因为它的快与它的高效.这也是Spring家族一贯的特色,这也是它的成功之处.它不但将自己的一些模块像MVC,JPA,Security等整合了起来,也整合了一些优秀的第三库,并用starter这样的形式进行组织,使得我们可以很方便的构建出适合自己应用场景的项目骨架,这是非常迷人的.并且很少有架构能够像SpringBoot一样,把灵活与简单做的如此极致.
相信随着深入的学习,你会越来越喜欢SpringBoot
2019年最新java入门到架构,资源整合大礼包关注微信公众号“熵增学院”即可领取~
一线架构师整理全套学习视频和资料,都在这里等你!
一直在构建版本_构建系统与代码结构SpringBoot相关推荐
- 前端构建工具_构建工具
前端构建工具 深度JavaScript (Deep JavaScript) Choosing a development tool based on its popularity isn't a ba ...
- App Store 上传app后不能构建版本,构建版本发现不了已上传app , 没有➕号 一定要查看App Store账号邮箱
1.首先要看用什么工具上传的 第一次往App Store上传app最好用Application Loader 不要用xcode直接上传因为 xcode直接上传如果app当中有问题不会报错,比如icon ...
- 用户管理界面开源代码_商城系统开源代码对于企业有利还是有弊?
对于商家来说,商城系统或许了解得比较多,却极少听说过"源码",面对互联网的冲击,传统企业发展不景气,只能痛定思痛进行转型,大多数传统企业选择了开源代码搭建商城网站,那么有很多的人会 ...
- 一直在构建版本_球鞋 | 一鞋两穿?AJ1十孔版本登场,拉链设计还是真香了?
• 全新十孔版本 Air Jordan 1 即将登场 随着近几年 Air Jordan 1 诞生了越来多的配色,也满足了越来越多人的需求.除此之外,此前推出的 Air Jordan 1 Zoom Co ...
- 一直在构建版本_教你如提升Gradle90%的构建速度
一.第一个最有效的办法 检查你使用的Gradle插件版本,是不是最新的.如果不是最新的,请升级到Gradle插件的最新版本. Gradle在升级过程中一直对构建速度做了优化,升级到Gradle最新版本 ...
- 一直在构建版本_升级成2.0版本的自己,生活会有什么不一样
李笑来老师在<把时间当作朋友>的序言中有段话: 人们生活在同一个世界,却又各自生活在自己的那个版本之中,改变自己,就意味着属于自己那个版本的世界也会随之变化,其中包括时间的属性,当同样的时 ...
- 华为开源构建工具_构建开源软件长达5年并以故事为生
华为开源构建工具 I've been working on open-source software for 5 years now and I'm still going. It's not som ...
- jenkins构建记录日志_构建企业日志记录层的清单
jenkins构建记录日志 在有关云迁移服务,数据中心和微服务等大型主题的任何讨论中,企业日志记录主题都可以归结为事后想法. 但是这样做的后果自负,因为如果不进行日志记录,就无法对服务进行有效的诊断和 ...
- 怎么查电脑系统版本_电脑系统垃圾清理利器,专业、小巧且免费,有这一个就够了...
图层 互联网IT技巧分享平台 大家好,我是你们的三凸 电脑使用一段时间会变慢 这是因为系统中驻留了大量的垃圾信息 安装了软件如果没有彻底清除 也会残留很多没用的注册表占据内存 还有,软件有时候会安装失 ...
最新文章
- eclipse搭建 tomcat、
- python sslerror_python3中SSLError错误处理
- 在linux oracle 10g/11g x64bit环境中,goldengate随os启动而自己主动启动的脚本
- wordpress漏洞上传php文件夹,WordPress Asset-Manager PHP文件上传漏洞
- Linux系统的优缺点
- 云计算实战系列二(Linux-用户管理)
- 优秀的程序员都应当知道的11个警句
- python常用代码入门-Python基础总结成千行代码,让Python入门更简单!
- python3 _笨方法学Python_日记_DAY4
- Power BI连接至Amazon Redshift
- oracle授权with,Oracle With 语句语法及示例
- matlab三次方程求根,如何用matlab求一元三次方程的最小正根?
- win7如何设置wifi热点_Win7家庭版如何给文件夹设置密码?Win7文件夹加密方法
- Python程序员必备的四款开发工具
- mysql 联合主键 加锁_MySQL 加锁处理分析
- 短视频底层实操课,让你迅速从短视频新手变成高手
- 【极客日常】解决UE4中FJsonObject转USTRUCT的坑
- Windows下查看端口占用情况
- Mysql常用函数(一)
- 删除下拉框只找23火星软件_下拉框关键词软件只找23火星下拉框词工具佳选火星...
热门文章
- SAP Spartacus storefront.component.html 里的 SkipLinkComponent 如何创建的
- SAP Spartacus里的product carousel控件
- JavaScript和ABAP的MD5计算源代码
- 小技巧,找出所有check table设置为某个数据库表的数据库表
- SAP UI5 Model destroy
- SAP UI5 busy dialog - SVG
- demo4 debug - create class instance - component works as type
- BDOC generated after customer product id is changed in CRM - CUST_MAT_INF
- CDS view delivered in CRM EHP4
- deactivate Data synchronization