0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示

一起聊聊SpringBoot的starter機制

jf_ro2CN3Fa ? 來源:蘇三說技術 ? 作者:蘇三說技術 ? 2022-11-24 10:26 ? 次閱讀


前言

我們都知道,Spring的功能非常強大,但也有些弊端。比如:我們需要手動去配置大量的參數,沒有默認值,需要我們管理大量的jar包和它們的依賴。

為了提升Spring項目的開發(fā)效率,簡化一些配置,Spring官方引入了SpringBoot。

當然,引入SpringBoot還有其他原因,在這里就不過多描述了。

本文重點跟大家一起聊聊SpringBootstarter機制,因為它太重要了。

7b46d5e8-6b9d-11ed-8abf-dac502259ad0.png

基于 Spring Boot + MyBatis Plus + Vue & Element 實現的后臺管理系統(tǒng) + 用戶小程序,支持 RBAC 動態(tài)權限、多租戶、數據權限、工作流、三方登錄、支付、短信、商城等功能

  • 項目地址:https://github.com/YunaiV/ruoyi-vue-pro
  • 視頻教程:https://doc.iocoder.cn/video/

1 為什么要用starter?

SpringBoot還沒有出來之前,我們使用Spring開發(fā)項目。如果程序需要連接數據庫,我們一般會使用HibernateMybatisORM框架,這里我以Mybatis為例,具體的操作步驟如下:

  1. 到maven倉庫去找需要引入的mybatis jar包,選取合適的版本。
  2. 到maven倉庫去找mybatis-spring整合的jar包,選取合適的版本。
  3. 在spring的applicationContext.xml文件中配置dataSource和mybatis相關信息。

當然有些朋友可能會指正,不是還需要引入數據庫驅動包嗎?

確實需要引入,但數據庫驅動有很多,比如:mysql、oracle、sqlserver,這不屬于mybatis的范疇,使用者可以根據項目的實際情況單獨引入。

如果程序只是需要連接數據庫這一個功能還好,按上面的步驟做基本可以滿足需求。但是,連接數據庫可能只是龐大的項目體系中一個環(huán)節(jié),實際項目中往往更復雜,需要引入更多的功能,比如:連接redis、連接mongodb、使用rocketmq、使用excel功能等等。

引入這些功能的話,需要再把上面的步驟再重復一次,工作量無形當中增加了不少,而且有很多重復的工作。

另外,還是有個問題,每次到要到maven中找合適的版本,如果哪次找的mybatis.jar包 和 mybatis-spring.jar包版本不兼容,程序不是會出現問題?

SpringBoot為了解決以上兩個問題引入了starter機制。

基于 Spring Cloud Alibaba + Gateway + Nacos + RocketMQ + Vue & Element 實現的后臺管理系統(tǒng) + 用戶小程序,支持 RBAC 動態(tài)權限、多租戶、數據權限、工作流、三方登錄、支付、短信、商城等功能

  • 項目地址:https://github.com/YunaiV/yudao-cloud
  • 視頻教程:https://doc.iocoder.cn/video/

2 starter有哪些要素?

我們首先一起看看mybatis-spring-boot-starter.jar是如何定義的。

7b6f3088-6b9d-11ed-8abf-dac502259ad0.png

可以看到它的META-INF目錄下只包含了:

  • pom.protperties 配置maven所需的項目version、groupId和artifactId。
  • pom.xml 配置所依賴的jar包。
  • MANIFEST.MF 這個文件描述了該Jar文件的很多信息。
  • spring.provides 配置所依賴的artifactId,給IDE使用的,沒有其他的作用。

注意一下,沒有一行代碼。

我們重點看一下pom.xml,因為這個jar包里面除了這個沒有啥重要的信息


<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-bootartifactId>
<version>1.3.1version>
parent>
<artifactId>mybatis-spring-boot-starterartifactId>
<name>mybatis-spring-boot-startername>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-autoconfigureartifactId>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
dependency>
dependencies>
project>

從上面可以看出,pom.xml文件中會引入一些jar包,其中除了引入spring-boot-starter,之外重點看一下:mybatis-spring-boot-autoconfigure。

我們找到mybatis-spring-boot-autoconfigure.jar文件,打開這個文件。

7b7cbfb4-6b9d-11ed-8abf-dac502259ad0.png

里面包含如下文件:

  • pom.properties 配置maven所需的項目version、groupId和artifactId
  • pom.xml 配置所依賴的jar包
  • additional-spring-configuration-metadata.json 手動添加IDE提示功能
  • MANIFEST.MF 這個文件描述了該Jar文件的很多信息
  • spring.factories SPI會讀取的文件
  • spring-configuration-metadata.json 系統(tǒng)自動生成的IDE提示功能
  • ConfigurationCustomizer 自定義Configuration回調接口
  • MybatisAutoConfiguration mybatis配置類
  • MybatisProperties mybatis屬性類
  • SpringBootVFS 掃描嵌套的jar包中的類

spring-configuration-metadata.jsonadditional-spring-configuration-metadata.json的功能差不多,我們在applicationContext.properties文件中輸入spring時,會自動出現下面的配置信息可供選擇,就是這個功能了。

7ba71ffc-6b9d-11ed-8abf-dac502259ad0.png

來自靈魂的一問:這兩個文件有什么區(qū)別?

答:如果pom.xml中引入了spring-boot-configuration-processor包,則會自動生成spring-configuration-metadata.json。

如果需要手動修改里面的元數據,則可以在additional-spring-configuration-metadata.json中編輯,最終兩個文件中的元數據會合并到一起。

MybatisProperties類是屬性實體類:

@ConfigurationProperties(prefix=MybatisProperties.MYBATIS_PREFIX)
publicclassMybatisProperties{

publicstaticfinalStringMYBATIS_PREFIX="mybatis";
privateStringconfigLocation;
privateString[]mapperLocations;
privateStringtypeAliasesPackage;
privateStringtypeHandlersPackage;
privatebooleancheckConfigLocation=false;
privateExecutorTypeexecutorType;
privatePropertiesconfigurationProperties;
@NestedConfigurationProperty
privateConfigurationconfiguration;

publicStringgetConfigLocation(){
returnthis.configLocation;
}

publicvoidsetConfigLocation(StringconfigLocation){
this.configLocation=configLocation;
}

@Deprecated
publicStringgetConfig(){
returnthis.configLocation;
}

@Deprecated
publicvoidsetConfig(Stringconfig){
this.configLocation=config;
}

publicString[]getMapperLocations(){
returnthis.mapperLocations;
}

publicvoidsetMapperLocations(String[]mapperLocations){
this.mapperLocations=mapperLocations;
}

publicStringgetTypeHandlersPackage(){
returnthis.typeHandlersPackage;
}

publicvoidsetTypeHandlersPackage(StringtypeHandlersPackage){
this.typeHandlersPackage=typeHandlersPackage;
}

publicStringgetTypeAliasesPackage(){
returnthis.typeAliasesPackage;
}

publicvoidsetTypeAliasesPackage(StringtypeAliasesPackage){
this.typeAliasesPackage=typeAliasesPackage;
}

publicbooleanisCheckConfigLocation(){
returnthis.checkConfigLocation;
}

publicvoidsetCheckConfigLocation(booleancheckConfigLocation){
this.checkConfigLocation=checkConfigLocation;
}

publicExecutorTypegetExecutorType(){
returnthis.executorType;
}

publicvoidsetExecutorType(ExecutorTypeexecutorType){
this.executorType=executorType;
}

publicPropertiesgetConfigurationProperties(){
returnconfigurationProperties;
}

publicvoidsetConfigurationProperties(PropertiesconfigurationProperties){
this.configurationProperties=configurationProperties;
}

publicConfigurationgetConfiguration(){
returnconfiguration;
}

publicvoidsetConfiguration(Configurationconfiguration){
this.configuration=configuration;
}

publicResource[]resolveMapperLocations(){
ResourcePatternResolverresourceResolver=newPathMatchingResourcePatternResolver();
Listresources=newArrayList();
if(this.mapperLocations!=null){
for(StringmapperLocation:this.mapperLocations){
try{
Resource[]mappers=resourceResolver.getResources(mapperLocation);
resources.addAll(Arrays.asList(mappers));
}catch(IOExceptione){
//ignore
}
}
}
returnresources.toArray(newResource[resources.size()]);
}
}

可以看到Mybatis初始化所需要的很多屬性都在這里,相當于一個JavaBean。

下面重點看一下MybatisAutoConfiguration的代碼:

@org.springframework.context.annotation.Configuration
@ConditionalOnClass({SqlSessionFactory.class,SqlSessionFactoryBean.class})
@ConditionalOnBean(DataSource.class)
@EnableConfigurationProperties(MybatisProperties.class)
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
publicclassMybatisAutoConfiguration{

privatestaticfinalLoggerlogger=LoggerFactory.getLogger(MybatisAutoConfiguration.class);
privatefinalMybatisPropertiesproperties;
privatefinalInterceptor[]interceptors;
privatefinalResourceLoaderresourceLoader;
privatefinalDatabaseIdProviderdatabaseIdProvider;
privatefinalListconfigurationCustomizers;
publicMybatisAutoConfiguration(MybatisPropertiesproperties,
ObjectProviderinterceptorsProvider,
ResourceLoaderresourceLoader,
ObjectProviderdatabaseIdProvider,
ObjectProvider>configurationCustomizersProvider){
this.properties=properties;
this.interceptors=interceptorsProvider.getIfAvailable();
this.resourceLoader=resourceLoader;
this.databaseIdProvider=databaseIdProvider.getIfAvailable();
this.configurationCustomizers=configurationCustomizersProvider.getIfAvailable();
}

@PostConstruct
publicvoidcheckConfigFileExists(){
if(this.properties.isCheckConfigLocation()&&StringUtils.hasText(this.properties.getConfigLocation())){
Resourceresource=this.resourceLoader.getResource(this.properties.getConfigLocation());
Assert.state(resource.exists(),"Cannotfindconfiglocation:"+resource
+"(pleaseaddconfigfileorcheckyourMybatisconfiguration)");
}
}

@Bean
@ConditionalOnMissingBean
publicSqlSessionFactorysqlSessionFactory(DataSourcedataSource)throwsException{
SqlSessionFactoryBeanfactory=newSqlSessionFactoryBean();
factory.setDataSource(dataSource);
factory.setVfs(SpringBootVFS.class);
if(StringUtils.hasText(this.properties.getConfigLocation())){
factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
}
Configurationconfiguration=this.properties.getConfiguration();
if(configuration==null&&!StringUtils.hasText(this.properties.getConfigLocation())){
configuration=newConfiguration();
}
if(configuration!=null&&!CollectionUtils.isEmpty(this.configurationCustomizers)){
for(ConfigurationCustomizercustomizer:this.configurationCustomizers){
customizer.customize(configuration);
}
}
factory.setConfiguration(configuration);
if(this.properties.getConfigurationProperties()!=null){
factory.setConfigurationProperties(this.properties.getConfigurationProperties());
}
if(!ObjectUtils.isEmpty(this.interceptors)){
factory.setPlugins(this.interceptors);
}
if(this.databaseIdProvider!=null){
factory.setDatabaseIdProvider(this.databaseIdProvider);
}
if(StringUtils.hasLength(this.properties.getTypeAliasesPackage())){
factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
}
if(StringUtils.hasLength(this.properties.getTypeHandlersPackage())){
factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
}
if(!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())){
factory.setMapperLocations(this.properties.resolveMapperLocations());
}

returnfactory.getObject();
}

@Bean
@ConditionalOnMissingBean
publicSqlSessionTemplatesqlSessionTemplate(SqlSessionFactorysqlSessionFactory){
ExecutorTypeexecutorType=this.properties.getExecutorType();
if(executorType!=null){
returnnewSqlSessionTemplate(sqlSessionFactory,executorType);
}else{
returnnewSqlSessionTemplate(sqlSessionFactory);
}
}

publicstaticclassAutoConfiguredMapperScannerRegistrar
implementsBeanFactoryAware,ImportBeanDefinitionRegistrar,ResourceLoaderAware{
privateBeanFactorybeanFactory;
privateResourceLoaderresourceLoader;

@Override
publicvoidregisterBeanDefinitions(AnnotationMetadataimportingClassMetadata,BeanDefinitionRegistryregistry){

ClassPathMapperScannerscanner=newClassPathMapperScanner(registry);
try{
if(this.resourceLoader!=null){
scanner.setResourceLoader(this.resourceLoader);
}

Listpackages=AutoConfigurationPackages.get(this.beanFactory);
if(logger.isDebugEnabled()){
for(Stringpkg:packages){
logger.debug("Usingauto-configurationbasepackage'{}'",pkg);
}
}

scanner.setAnnotationClass(Mapper.class);
scanner.registerFilters();
scanner.doScan(StringUtils.toStringArray(packages));
}catch(IllegalStateExceptionex){
logger.debug("Couldnotdetermineauto-configurationpackage,automaticmapperscanningdisabled.",ex);
}
}

@Override
publicvoidsetBeanFactory(BeanFactorybeanFactory)throwsBeansException{
this.beanFactory=beanFactory;
}

@Override
publicvoidsetResourceLoader(ResourceLoaderresourceLoader){
this.resourceLoader=resourceLoader;
}
}

@org.springframework.context.annotation.Configuration
@Import({AutoConfiguredMapperScannerRegistrar.class})
@ConditionalOnMissingBean(MapperFactoryBean.class)
publicstaticclassMapperScannerRegistrarNotFoundConfiguration{

@PostConstruct
publicvoidafterPropertiesSet(){
logger.debug("No{}found.",MapperFactoryBean.class.getName());
}
}
}

這個類就是一個Configuration(配置類),它里面定義很多bean,其中最重要的就是SqlSessionFactory的bean實例,該實例是Mybatis的核心功能,用它創(chuàng)建SqlSession,對數據庫進行CRUD操作。

除此之外,MybatisAutoConfiguration類還包含了:

  • @ConditionalOnClass 配置了只有包含SqlSessionFactory.class和SqlSessionFactoryBean.class,該配置類才生效。
  • @ConditionalOnBean 配置了只有包含dataSource實例時,該配置類才生效。
  • @EnableConfigurationProperties 該注解會自動填充MybatisProperties實例中的屬性。
  • AutoConfigureAfter 配置了該配置類在DataSourceAutoConfiguration類之后自動配置。

這些注解都是一些輔助功能,決定Configuration是否生效,當然這些注解不是必須的。

接下來,重點看看spring.factories文件有啥內容:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=
org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration

里面只有一行配置,即keyEnableAutoConfiguration,valueMybatisAutoConfiguration。

好了,介紹了這么多東西,現在我們來總結一下,

starter幾個要素如下圖所示:

7bd722a6-6b9d-11ed-8abf-dac502259ad0.png

那么,編寫starter需要哪些步驟?

  • 1.需要定義一個名稱為xxx-spring-boot-starter的空項目,里面不包含任何代碼,可以有pom.xml和pom.properties文件。
  • 2.pom.xml文件中包含了名稱為xxx-spring-boot-autoconfigure的項目。
  • 3.xxx-spring-boot-autoconfigure項目中包含了名稱為xxxAutoConfiguration的類,該類可以定義一些bean實例。當然,Configuration類上可以打一些如:ConditionalOnClass、ConditionalOnBean、EnableConfigurationProperties等注解。
  • 4.需要在spring.factories文件中增加key為EnableAutoConfiguration,value為xxxAutoConfiguration。

我們試著按照這四步,自己編寫一個starter看看能否成功,驗證一下總結的內容是否正確。

3 如何定義自己的starter?

3.1 先創(chuàng)建一個空項目

該項目名稱為id-generate-starter,注意為了方便我把項目重命名了,原本應該是叫id-generate-spring-boot-starter的,如下圖所示:

7beeef76-6b9d-11ed-8abf-dac502259ad0.png

pom.xml文件定義如下:


<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<version>1.3.1version>
<groupId>com.suegroupId>
<artifactId>id-generate-spring-boot-starterartifactId>
<name>id-generate-spring-boot-startername>
<dependencies>
<dependency>
<groupId>com.suegroupId>
<artifactId>id-generate-spring-boot-autoconfigureartifactId>
<version>1.3.1version>
dependency>
dependencies>
project>

我們看到,它只引入了id-generate-spring-boot-autoconfigure。當然如果有需要這里還可以引入多個autoconfigure或者多個其他jar包或者。

3.2 創(chuàng)建id-generate-autoconfigure

同樣為了方便我把項目重命名了,原本是叫id-generate-spring-boot-autoconfigure,如下圖所示:

7c10021a-6b9d-11ed-8abf-dac502259ad0.png

該項目當中包含:pom.xml、spring.factories、IdGenerateAutoConfiguration、IdGenerateService 和 IdProperties 這5個關鍵文件,下面我們逐一看看。

先從pom.xml


<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.4.RELEASEversion>
parent>
<modelVersion>4.0.0modelVersion>
<version>1.3.1version>
<groupId>com.suegroupId>
<artifactId>id-generate-spring-boot-autoconfigureartifactId>
<name>id-generate-spring-boot-autoconfigurename>

<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-autoconfigureartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
<optional>trueoptional>
dependency>
dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<configuration>
<source>1.8source>
<target>1.8target>
configuration>
plugin>
plugins>
build>
project>

我們可以看到,這個文件比較簡單就引入了:

  • spring-boot-starter:springboot的相關jar包。
  • spring-boot-autoconfigure:springboot自動配置相關jar包。
  • spring-boot-configuration-processor:springboot生成IDE提示功能相關jar包。

重點看看spring.factories文件:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.sue.IdGenerateAutoConfiguration

它里面只包含一行配置,其中key是EnableAutoConfiguration,value是IdGenerateAutoConfiguration。

再重點看一下IdGenerateAutoConfiguration

@ConditionalOnClass(IdProperties.class)
@EnableConfigurationProperties(IdProperties.class)
@Configuration
publicclassIdGenerateAutoConfiguration{
@Autowired
privateIdPropertiesproperties;
@Bean
publicIdGenerateServiceidGenerateService(){
returnnewIdGenerateService(properties.getWorkId());
}
}

該類是一個使用了@Configuration注解標記為了配置類,生效的條件是@ConditionalOnClass注解中檢測到包含IdProperties.class。并且使用@EnableConfigurationProperties注解會自動注入IdProperties的實例。

此外,最關鍵的點是該類里面創(chuàng)建了idGenerateService的bean實例,這是自動配置的精髓。

再看看IdGenerateService

publicclassIdGenerateService{
privateLongworkId;
publicIdGenerateService(LongworkId){
this.workId=workId;
}

publicLonggenerate(){
returnnewRandom().nextInt(100)+this.workId;
}
}

我們可以看到它是一個普通的類,甚至都沒有使用@Service注解,里面有個generate方法,根據workId的值和隨機數動態(tài)生成id。

最后看看IdProperties

@ConfigurationProperties(prefix=IdProperties.PREFIX)
publicclassIdProperties{
publicstaticfinalStringPREFIX="sue";
privateLongworkId;
publicLonggetWorkId(){
returnworkId;
}
publicvoidsetWorkId(LongworkId){
this.workId=workId;
}
}

它是一個配置實體類,里面包含了相關的配置文件。使用@ConfigurationProperties注解,會自動把application.properties文件中以sue開通的,參數名稱跟IdProperties中一樣的參數值,自動注入到IdProperties對象中。

3.3 創(chuàng)建id-generate-test

這個項目主要用于測試。

7c2d4f82-6b9d-11ed-8abf-dac502259ad0.png

該項目里面包含:pom.xml、application.properties、Application 和 TestRunner 文件。

先看看pom.xml文件


<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0modelVersion>
<version>1.3.1version>
<groupId>com.suegroupId>
<artifactId>spring-boot-id-generate-testartifactId>
<name>spring-boot-id-generate-testname>
<dependencies>
<dependency>
<groupId>com.suegroupId>
<artifactId>id-generate-spring-boot-starterartifactId>
<version>1.3.1version>
dependency>
dependencies>
project>

由于只測試剛剛定義的id生成功能,所以只引入的id-generate-spring-boot-starter jar包。

application.properties配置資源文件

sue.workId=123

只有一行配置,因為我們的IdProperties中目前只需要這一個參數。

Application是測試程序啟動類

@SpringBootApplication
publicclassApplication{
publicstaticvoidmain(String[]args){
SpringApplication.run(Application.class,args);
}
}

很簡單,就是一個普通的springboot啟動類

TestRunner是我們的測試類

@Component
publicclassTestRunnerimplementsApplicationRunner{
@Autowired
privateIdGenerateServiceidGenerateService;
publicvoidrun(ApplicationArgumentsargs)throwsException{
LongsysNo=idGenerateService.generate();
System.out.println(sysNo);
}
}

它實現了ApplicationRunner接口,所以在springboot啟動的時候會調用該類的run方法。

好了,所有自定義starter的代碼和測試代碼都已經就緒。接下,運行一下Application類的main方法。

運行結果:

176

完美,驗證成功了。

接下來,我們分析一下starter的底層實現原理。

4 starter的底層原理是什么?

通過上面編寫自己的starter的例子,相信大家對starter的認識更進一步了,現在跟大家一起看看starter的底層是如何實現的。

id-generate-starter.jar其實是一個空項目,依賴于id-generate-autoconfiguration.jar。

id-generate-starter.jar是一個入口,我們給他取一個更優(yōu)雅的名字:門面模式,其他業(yè)務系統(tǒng)想引入相應的功能,必須要通過這個門面。

我們重點分析一下 id-generate-autoconfiguration.jar

該jar包核心內容是:IdGenerateConfiguration,這個配置類中創(chuàng)建了IdGenerateService對象,IdGenerateService是我們所需要自動配置的具體功能。

接下來一個最重要的問題:IdGenerateConfiguration為什么會自動加載的呢?

還記得我們定義的spring.factories文件不?

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.sue.IdGenerateAutoConfiguration

它里面只包含一行配置,其中keyEnableAutoConfiguration,valueIdGenerateAutoConfiguration。

要搞明白這個過程,要從Application類的@SpringBootApplication注解開始:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters={
@Filter(type=FilterType.CUSTOM,classes=TypeExcludeFilter.class),
@Filter(type=FilterType.CUSTOM,classes=AutoConfigurationExcludeFilter.class)})
public@interfaceSpringBootApplication{

@AliasFor(annotation=EnableAutoConfiguration.class)
Class[]exclude()default{};

@AliasFor(annotation=EnableAutoConfiguration.class)
String[]excludeName()default{};

@AliasFor(annotation=ComponentScan.class,attribute="basePackages")
String[]scanBasePackages()default{};

@AliasFor(annotation=ComponentScan.class,attribute="basePackageClasses")
Class[]scanBasePackageClasses()default{};
}

從上面可以看出該注解里面包含了@EnableAutoConfiguration注解。

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public@interfaceEnableAutoConfiguration{
StringENABLED_OVERRIDE_PROPERTY="spring.boot.enableautoconfiguration";

Class[]exclude()default{};
String[]excludeName()default{};
}

@EnableAutoConfiguration注解會引入AutoConfigurationImportSelector類。

該類的selectImports方法一個關鍵方法:

@Override
publicString[]selectImports(AnnotationMetadataannotationMetadata){
//配置有沒有配置spring.boot.enableautoconfiguration開關,默認為true
//如果為false,則不執(zhí)行自動配置的功能,直接返回
if(!isEnabled(annotationMetadata)){
returnNO_IMPORTS;
}
//找spring-autoconfigure-metadata.properties中的元素
AutoConfigurationMetadataautoConfigurationMetadata=AutoConfigurationMetadataLoader
.loadMetadata(this.beanClassLoader);
//獲取EnableAutoConfiguration注解中的屬性
AnnotationAttributesattributes=getAttributes(annotationMetadata);
//獲取工程下所有配置key為EnableAutoConfiguration的值,即IdGenerateConfiguration等類。
Listconfigurations=getCandidateConfigurations(annotationMetadata,
attributes);
//刪除重復的值
configurations=removeDuplicates(configurations);
//獲取需要排除的規(guī)則列表
Setexclusions=getExclusions(annotationMetadata,attributes);
//檢查
checkExcludedClasses(configurations,exclusions);
//刪除需要排除的值
configurations.removeAll(exclusions);
//根據配置文件中配置的開關,過濾一部分不滿足條件的值
configurations=filter(configurations,autoConfigurationMetadata);
fireAutoConfigurationImportEvents(configurations,exclusions);
returnStringUtils.toStringArray(configurations);
}

這里就是starter能夠自動配置的秘密。

此外,有些朋友看其他人定義的springboot starter可能會有疑惑。

先看看druid-spring-boot-starter

7c42aa44-6b9d-11ed-8abf-dac502259ad0.png

alibaba定義的druid-spring-boot-starter只有xxx-spring-boot-starter.jar文件,而沒有xxx-spring-boot-autoconfigure.jar文件。

再看看spring-boot-starter-jdbc

7c69f0ae-6b9d-11ed-8abf-dac502259ad0.png

更神奇的是這個文件中連pom.xml都沒有,一臉懵逼。。。。。。。

是不是我講錯了?

答:其實沒有。

SpringBoot的原則是約定優(yōu)于配置

從spring-boot-starter-jdbc內部空實現來看,它的約定是要把xxx-spring-boot-starter.jar和xxx-spring-boot-autoconfigure.jar區(qū)分開的。個人認為,alibaba定義得并不好,沒有遵照springboot的約定,雖然功能不受影響。(這個地方歡迎一起探討一下)

而springboot自己定義的spring-boot-starter-jdbc為什么連pom.xml文件也沒有呢?

它不需要依賴xxx-spring-boot-autoconfigure.jar文件嗎?

因為springboot把所有的自動配置的類都統(tǒng)一放到spring-boot-autoconfigure.jar下面了:

7c9428e2-6b9d-11ed-8abf-dac502259ad0.png

spring.factories文件內容如下:

7cc4641c-6b9d-11ed-8abf-dac502259ad0.png

SpringBoot這樣集中管理自動配置,而不需要從各個子包中遍歷,我個人認為是為了查找效率。

我們最后再看看spring-cloud-starter-openfegin

7ce39b3e-6b9d-11ed-8abf-dac502259ad0.png

明顯看到,它是遵循了我們說的原則的。

除此之外,還有一個原則一順便提一下。

SpringBootSpringCloud系列定義jar包的名稱是:

  • spring-boot-starter-xxx.jar
  • spring-cloud-starter-xxx.jar

而我們自己的項目定義的jar應該是:

  • xxx-spring-boot-starter.jar


審核編輯 :李倩


聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • 數據庫
    +關注

    關注

    7

    文章

    3712

    瀏覽量

    64025
  • 代碼
    +關注

    關注

    30

    文章

    4671

    瀏覽量

    67767
  • spring
    +關注

    關注

    0

    文章

    335

    瀏覽量

    14259
  • SpringBoot
    +關注

    關注

    0

    文章

    172

    瀏覽量

    145

原文標題:天天用SpringBoot寫代碼,結果連Starter是什么都不知道?

文章出處:【微信號:芋道源碼,微信公眾號:芋道源碼】歡迎添加關注!文章轉載請注明出處。

收藏 人收藏

    評論

    相關推薦

    VeriStand的執(zhí)行機制

    本次技術分享介紹VeriStand的執(zhí)行機制以及該機制下信號傳輸的延遲,當仿真測試對信號延遲有定要求時,考慮VeriStand執(zhí)行機制的影響是必要的,現在請跟隨小編的步伐
    的頭像 發(fā)表于 09-11 14:43 ?494次閱讀
    VeriStand的執(zhí)行<b class='flag-5'>機制</b>

    六類網線可以和強電一起走嗎

    六類網線理論上不建議和強電一起走。從布線規(guī)范的角度來看,弱電線路和強電線路通常不建議共用同橋架,以避免潛在的電磁干擾。然而,多年的施工經驗表明,在某些情況下,強電線和弱電網線可能一起
    的頭像 發(fā)表于 04-19 09:55 ?3806次閱讀

    #新開端、新起點,2024一起加油#

    \"新開端、新起點,2024一起加油\" 這句話充滿了積極向上的精神和對未來的期待。新開端和新起點意味著我們有機會摒棄過去的不足,以個全新的姿態(tài)開始新的旅程。而\"
    發(fā)表于 02-26 21:01

    一起聊聊faro orbis-它的商業(yè)價值有哪些

    Orbis 由 GeoSLAM 專有的 SLAM 算法提供支持,對于希望更快地提供高精度交付成果、減少人為錯誤并提高整體效率的行業(yè)專業(yè)人士來說,這是革命性的轉變。利用混合移動掃描與固定式 Flash 掃描功能的強大功能,與 Orbis(應對現代掃描挑戰(zhàn)的終極搭檔)一起踏上轉變項目運作方式的新旅程。
    的頭像 發(fā)表于 02-20 13:31 ?280次閱讀
    <b class='flag-5'>一起</b><b class='flag-5'>聊聊</b>faro orbis-它的商業(yè)價值有哪些

    一起聊聊PCB的阻抗控制

    帶狀線就是條置于兩層導電平面之間的電介質中間的銅帶。如果線的厚度和寬度,介質的介電常數,以及兩層接地平面的距離都是可控的,則線的特性阻抗也是可控的,且在10%之內。
    發(fā)表于 12-19 15:44 ?814次閱讀
    <b class='flag-5'>一起</b><b class='flag-5'>聊聊</b>PCB的阻抗控制

    SpringBoot實現動態(tài)切換數據源

    最近在做業(yè)務需求時,需要從不同的數據庫中獲取數據然后寫入到當前數據庫中,因此涉及到切換數據源問題。本來想著使用Mybatis-plus中提供的動態(tài)數據源SpringBootstarter:dynamic-datasource-spring-boot-
    的頭像 發(fā)表于 12-08 10:53 ?840次閱讀
    <b class='flag-5'>SpringBoot</b>實現動態(tài)切換數據源

    個注解搞定SpringBoot接口防刷

    技術要點:springboot的基本知識,redis基本操作,
    的頭像 發(fā)表于 11-28 10:46 ?341次閱讀

    javaweb和springboot一起用嗎

    JavaWeb 和 SpringBoot 是兩種針對 Java 程序開發(fā)的框架,它們可以在一起使用。在本文中,我將詳細介紹 JavaWeb 和 SpringBoot 的關系,并探討如何結合使用這兩個
    的頭像 發(fā)表于 11-16 10:54 ?1708次閱讀

    詳細聊聊container_of這個宏定義

    大家周末好,我是bug菌~ 今天主要是跟大家詳細聊聊container_of這個宏定義,非常經典的宏,只是直沒有抽時間細細品味,今天就跟大家一起來看看有何神奇之處。
    的頭像 發(fā)表于 11-13 09:45 ?562次閱讀

    單片機的蜂鳴器是否可以與小燈一起使用?

    單片機的蜂鳴器是否可以與小燈一起使用
    發(fā)表于 10-31 06:40

    不同品牌的電容器可以一起使用嗎?

    電容器作為電子設備中不可或缺的元件之,扮演著儲存電荷和調節(jié)電流的重要角色。然而,在面對市面上眾多品牌的電容器時,我們可能會產生個疑問:不同品牌的電容器能否一起使用?
    的頭像 發(fā)表于 10-09 15:14 ?1992次閱讀

    SpringBoot 連接ElasticSearch的使用方式

    SpringBoot,今天我們就以 SpringBoot 整合 ElasticSearch 為例,給大家詳細的介紹 ElasticSearch 的使用! SpringBoot 連接 ElasticSearch,主流
    的頭像 發(fā)表于 10-09 10:35 ?638次閱讀

    SpringBoot 22條最佳實踐

    例如,如果我們添加 spring-boot-starter-web 依賴項,默認情況下它會與 jackson、spring-core、spring-mvc 和 spring-boot-starter-tomcat 依賴項捆綁在一起
    的頭像 發(fā)表于 10-08 17:10 ?549次閱讀
    <b class='flag-5'>SpringBoot</b> 22條最佳實踐

    如何編寫個自己的 starter

    前面給大家介紹了 SpringBoot 的自動裝配功能,相信大家對自動裝配都有了很好的理解,那么今天阿粉通過個示例來給大家演示下如何編寫個自己的
    的頭像 發(fā)表于 10-08 14:42 ?753次閱讀
    如何編寫<b class='flag-5'>一</b>個自己的 <b class='flag-5'>starter</b>

    Spring Boot Starter需要些什么

    前面我們簡單介紹了如何使用消息中間件 Apache Pulsar ,但是在項目中那樣使用,顯然是不太好的,不管從易用性和擴展性來看,都是遠遠不夠, 為了和springboot項目集成,寫
    的頭像 發(fā)表于 09-25 11:35 ?632次閱讀
    Spring Boot <b class='flag-5'>Starter</b>需要些什么