原文地址:http://www.javashuo.com/article/p-xcgmctpe-dh.htmlhtml
咱们都知道可使用SpringBoot快速的开发基于Spring框架的项目。因为围绕SpringBoot存在不少开箱即用的Starter依赖,使得咱们在开发业务代码时可以很是方便的、不须要过多关注框架的配置,而只须要关注业务便可。java
例如我想要在SpringBoot项目中集成Redis,那么我只须要加入spring-data-redis-starter的依赖,并简单配置一下链接信息以及Jedis链接池配置就能够。这为咱们省去了以前不少的配置操做。甚至有些功能的开启只须要在启动类或配置类上增长一个注解便可完成。web
那么若是咱们想要本身实现本身的Starter须要作些什么呢?下面就开始介绍如何实现本身的SpringBoot-xxx-starter。redis
首先说说原理,咱们知道使用一个公用的starter的时候,只须要将相应的依赖添加的Maven的配置文件当中便可,免去了本身须要引用不少依赖类,而且SpringBoot会自动进行类的自动配置。那么 SpringBoot 是如何知道要实例化哪些类,并进行自动配置的呢? 下面简单说一下。spring
第一步,SpringBoot 在启动时会去依赖的starter包中寻找 resources/META-INF/spring.factories
文件,而后根据文件中配置的类路径去扫描项目所依赖的Jar包,这相似于 Java 的 SPI 机制。(spi介绍参阅:Java的spi介绍和简单应用)mongodb
第二步,根据 spring.factories
配置加载AutoConfigure
类。shell
最后,根据 @Conditional
注解的条件,进行自动配置并将Bean注入Spring Context 上下文当中。数据库
咱们也可使用@ImportAutoConfiguration({MyServiceAutoConfiguration.class})
指定自动配置哪些类。编程
终于到了代码实现的步骤,接下来就开始编码咱们本身的SpringBoot-starter。json
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency>
其中 spring-boot-configuration-processor
的做用是编译时生成 spring-configuration-metadata.json
,此文件主要给IDE使用。如当配置此jar相关配置属性在 application.yml
,你能够用ctlr+鼠标左键点击属性名,IDE会跳转到你配置此属性的类中。
咱们平常使用的Spring官方的Starter通常采起spring-boot-starter-{name}
的命名方式,如 spring-boot-starter-web
。
而非官方的Starter,官方建议 artifactId
命名应遵循{name}-spring-boot-starter
的格式。 例如:mybatis-spring-boot-starter
。
<dependency> <groupId>com.meng.starter</groupId> <artifactId>demo07-spring-boot-starter</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
这里讲一下咱们的Starter要实现的功能,很简单,提供一个Service
,包含一个可以将配置文件中配置的字符串根据传入的字符进行分割的方法String[] split(String separatorChar)
。
public class DemoService { private String str; public DemoService(String str) { this.str = str; } public String[] split(String tag){ return StringUtils.split(str, tag); } }
@ConfigurationProperties("demo.service") public class DemoProperties { private String str; public String getStr() { return str; } public void setStr(String str) { this.str = str; } }
AutoConfiguration
类 ,这步是关键点@Configuration @ConditionalOnClass(DemoService.class) @EnableConfigurationProperties(DemoProperties.class) public class DemoAutoConfiguration { @Autowired private DemoProperties demoProperties; @Bean @ConditionalOnMissingBean @ConditionalOnProperty(prefix = "demo.service", value = "enabled", havingValue = "true") DemoService demoService(){ return new DemoService(demoProperties.getStr()); } }
解释一下代码中用到的几个注解:
@ConditionalOnClass
,当classpath
下发现该类的状况下进行自动配置。@ConditionalOnMissingBean
,当Spring Context
中不存在该Bean
时。@ConditionalOnProperty(prefix = "demo.service",value = "enabled",havingValue = "true")
,当配置文件中example.service.enabled=true
时。@ConditionalOnBean:当容器中有指定的Bean的条件下
@ConditionalOnClass:当类路径下有指定的类的条件下
@ConditionalOnExpression:基于SpEL表达式做为判断条件
@ConditionalOnJava:基于JVM版本做为判断条件
@ConditionalOnJndi:在JNDI存在的条件下查找指定的位置
@ConditionalOnMissingBean:当容器中没有指定Bean的状况下
@ConditionalOnMissingClass:当类路径下没有指定的类的条件下
@ConditionalOnNotWebApplication:当前项目不是Web项目的条件下
@ConditionalOnProperty:指定的属性是否有指定的值
@ConditionalOnResource:类路径下是否有指定的资源
@ConditionalOnSingleCandidate:当指定的Bean在容器中只有一个,或者在有多个Bean的状况下,用来指定首选的Bean @ConditionalOnWebApplication:当前项目是Web项目的条件下
resources/META-INF/
下建立spring.factories
文件,并添加以下内容:# Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.meng.starter.DemoAutoConfiguration
至此,咱们的一个Starter代码部分就是完成了,下面将项目安装到本地Maven仓库中。
在项目根目录执行 mvn install
进行打包安装。
将Starter项目的依赖添加到咱们本身的SpringBoot项目中
<dependency> <groupId>com.meng.starter</groupId> <artifactId>demo07-spring-boot-starter</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
在application.yml
配置文件中添加配置信息:
demo:
service:
enabled: true
str: Ctrl+Shift+Alt+N,查找类中的方法或变量
测试
@Autowired DemoService demoService; @GetMapping("/demo") public String[] demo(String tag){ return demoService.split(tag); }
Spring Boot应用启动器基本的一共有44种,具体以下:
1)spring-boot-starter
这是Spring Boot的核心启动器,包含了自动配置、日志和YAML。
2)spring-boot-starter-actuator
帮助监控和管理应用。
3)spring-boot-starter-amqp
经过spring-rabbit来支持AMQP协议(Advanced Message Queuing Protocol)。
4)spring-boot-starter-aop
支持面向方面的编程即AOP,包括spring-aop和AspectJ。
5)spring-boot-starter-artemis
经过Apache Artemis支持JMS的API(Java Message Service API)。
6)spring-boot-starter-batch
支持Spring Batch,包括HSQLDB数据库。
7)spring-boot-starter-cache
支持Spring的Cache抽象。
8)spring-boot-starter-cloud-connectors
支持Spring Cloud Connectors,简化了在像Cloud Foundry或Heroku这样的云平台上链接服务。
9)spring-boot-starter-data-elasticsearch
支持ElasticSearch搜索和分析引擎,包括spring-data-elasticsearch。
10)spring-boot-starter-data-gemfire
支持GemFire分布式数据存储,包括spring-data-gemfire。
11)spring-boot-starter-data-jpa
支持JPA(Java Persistence API),包括spring-data-jpa、spring-orm、Hibernate。
12)spring-boot-starter-data-mongodb
支持MongoDB数据,包括spring-data-mongodb。
13)spring-boot-starter-data-rest
经过spring-data-rest-webmvc,支持经过REST暴露Spring Data数据仓库。
14)spring-boot-starter-data-solr
支持Apache Solr搜索平台,包括spring-data-solr。
15)spring-boot-starter-freemarker
支持FreeMarker模板引擎。
16)spring-boot-starter-groovy-templates
支持Groovy模板引擎。
17)spring-boot-starter-hateoas
经过spring-hateoas支持基于HATEOAS的RESTful Web服务。
18)spring-boot-starter-hornetq
经过HornetQ支持JMS。
19)spring-boot-starter-integration
支持通用的spring-integration模块。
20)spring-boot-starter-jdbc
支持JDBC数据库。
21)spring-boot-starter-jersey
支持Jersey RESTful Web服务框架。
22)spring-boot-starter-jta-atomikos
经过Atomikos支持JTA分布式事务处理。
23)spring-boot-starter-jta-bitronix
经过Bitronix支持JTA分布式事务处理。
24)spring-boot-starter-mail
支持javax.mail模块。
25)spring-boot-starter-mobile
支持spring-mobile。
26)spring-boot-starter-mustache
支持Mustache模板引擎。
27)spring-boot-starter-redis
支持Redis键值存储数据库,包括spring-redis。
28)spring-boot-starter-security
支持spring-security。
29)spring-boot-starter-social-facebook
支持spring-social-facebook
30)spring-boot-starter-social-linkedin
支持pring-social-linkedin
31)spring-boot-starter-social-twitter
支持pring-social-twitter
32)spring-boot-starter-test
支持常规的测试依赖,包括JUnit、Hamcrest、Mockito以及spring-test模块。
33)spring-boot-starter-thymeleaf
支持Thymeleaf模板引擎,包括与Spring的集成。
34)spring-boot-starter-velocity
支持Velocity模板引擎。
35)spring-boot-starter-web
S支持全栈式Web开发,包括Tomcat和spring-webmvc。
36)spring-boot-starter-websocket
支持WebSocket开发。
37)spring-boot-starter-ws
支持Spring Web Services。
Spring Boot应用启动器面向生产环境的还有2种,具体以下:
1)spring-boot-starter-actuator
增长了面向产品上线相关的功能,好比测量和监控。
2)spring-boot-starter-remote-shell
增长了远程ssh shell的支持。
最后,Spring Boot应用启动器还有一些替换技术的启动器,具体以下:
1)spring-boot-starter-jetty
引入了Jetty HTTP引擎(用于替换Tomcat)。
2)spring-boot-starter-log4j
支持Log4J日志框架。
3)spring-boot-starter-logging
引入了Spring Boot默认的日志框架Logback。
4)spring-boot-starter-tomcat
引入了Spring Boot默认的HTTP引擎Tomcat。
5)spring-boot-starter-undertow 引入了Undertow HTTP引擎(用于替换Tomcat)。