原文网址:SpringBoot--自定义Spring Boot Starter_IT利刃出鞘的博客-CSDN博客
简介
本文介绍starter的作用以及如何自定义SpringBootStarter。
starter的作用
简介
starter是SpringBoot中的一个新发明,它有效的降低了项目开发过程的复杂程度,对于简化开发操作有着非常好的效果。
Spring Boot Starter是在SpringBoot组件中被提出来的一种概念,stackoverflow上面已经有人概括了这个starter是什么东西:java - What are `spring-boot-starter` jars? - Stack Overflow
大概意思就是说starter是一种对依赖的synthesize(合成)。其内容如下:
Starter POMs are a set of convenient dependency descriptors that you can include in your application. You get a one-stop-shop for all the Spring and related technology that you need, without having to hunt through sample code and copy paste loads of dependency descriptors. For example, if you want to get started using Spring and JPA for database access, just include the spring-boot-starter-data-jpa dependency in your project, and you are good to go.
没有starter时的操作
在没有starter之前,假如我想要在Spring中使用jpa,那我可能需要做以下操作:
- 在Maven中引入使用的数据库的依赖(即JDBC的jar)
- 引入jpa的依赖
- 在xxx.xml中配置一些属性信息
- 反复的调试直到可以正常运行
需要注意的是,这里操作在我们每次新建一个需要用到jpa的项目的时候都需要重复的做一次。也许你在第一次自己建立项目的时候是在Google上自己搜索了一番,花了半天时间解决掉了各种奇怪的问题之后,jpa终于能正常运行了。有些有经验的人会在OneNote上面把这次建立项目的过程给记录下来,包括操作的步骤以及需要用到的配置文件的内容,在下一次再创建jpa项目的时候,就不需要再次去Google了,只需要照着笔记来,之后再把所有的配置文件copy&paste就可以了。
像上面这样的操作也不算不行,事实上我们在没有starter之前都是这么干的,但是这样做有几个问题:
- 如果过程比较繁琐,这样一步步操作会增加出错的可能性
- 不停地copy&paste不符合Don’t repeat yourself精神
- 在第一次配置的时候(尤其如果开发者比较小白),需要花费掉大量的时间
starter的逻辑
starter的主要目的就是为了解决上面的这些问题。
上边只是部分内容,为便于维护,本文已迁移到此地址:SpringBoot-自定义Spring Boot Starter - 自学精灵