日B视频 亚洲,啪啪啪网站一区二区,91色情精品久久,日日噜狠狠色综合久,超碰人妻少妇97在线,999青青视频,亚洲一区二卡,让本一区二区视频,日韩网站推荐

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

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

一起聊聊SpringBoot的starter機(jī)制

jf_ro2CN3Fa ? 來源:蘇三說技術(shù) ? 作者:蘇三說技術(shù) ? 2022-11-24 10:26 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群


前言

我們都知道,Spring的功能非常強(qiáng)大,但也有些弊端。比如:我們需要手動(dòng)去配置大量的參數(shù),沒有默認(rèn)值,需要我們管理大量的jar包和它們的依賴。

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

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

本文重點(diǎn)跟大家一起聊聊SpringBootstarter機(jī)制,因?yàn)樗匾恕?/p> 7b46d5e8-6b9d-11ed-8abf-dac502259ad0.png

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

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

1 為什么要用starter?

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

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

當(dāng)然有些朋友可能會(huì)指正,不是還需要引入數(shù)據(jù)庫驅(qū)動(dòng)包嗎?

確實(shí)需要引入,但數(shù)據(jù)庫驅(qū)動(dòng)有很多,比如:mysql、oracle、sqlserver,這不屬于mybatis的范疇,使用者可以根據(jù)項(xiàng)目的實(shí)際情況單獨(dú)引入。

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

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

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

SpringBoot為了解決以上兩個(gè)問題引入了starter機(jī)制

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

  • 項(xiàng)目地址: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所需的項(xiàng)目version、groupId和artifactId。
  • pom.xml 配置所依賴的jar包。
  • MANIFEST.MF 這個(gè)文件描述了該Jar文件的很多信息。
  • spring.provides 配置所依賴的artifactId,給IDE使用的,沒有其他的作用。

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

我們重點(diǎn)看一下pom.xml,因?yàn)檫@個(gè)jar包里面除了這個(gè)沒有啥重要的信息


<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文件中會(huì)引入一些jar包,其中除了引入spring-boot-starter,之外重點(diǎn)看一下:mybatis-spring-boot-autoconfigure。

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

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

里面包含如下文件:

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

spring-configuration-metadata.jsonadditional-spring-configuration-metadata.json的功能差不多,我們?cè)?code style="font-size:14px;padding:2px 4px;margin-right:2px;margin-left:2px;color:rgb(30,107,184);background-color:rgba(27,31,35,.05);font-family:'Operator Mono', Consolas, Monaco, Menlo, monospace;">applicationContext.properties文件中輸入spring時(shí),會(huì)自動(dòng)出現(xiàn)下面的配置信息可供選擇,就是這個(gè)功能了。

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

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

答:如果pom.xml中引入了spring-boot-configuration-processor包,則會(huì)自動(dòng)生成spring-configuration-metadata.json

如果需要手動(dòng)修改里面的元數(shù)據(jù),則可以在additional-spring-configuration-metadata.json中編輯,最終兩個(gè)文件中的元數(shù)據(jù)會(huì)合并到一起。

MybatisProperties類是屬性實(shí)體類:

@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初始化所需要的很多屬性都在這里,相當(dāng)于一個(gè)JavaBean

下面重點(diǎn)看一下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());
}
}
}

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

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

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

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

接下來,重點(diǎn)看看spring.factories文件有啥內(nèi)容:

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

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

好了,介紹了這么多東西,現(xiàn)在我們來總結(jié)一下,

starter幾個(gè)要素如下圖所示:

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

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

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

我們?cè)囍凑者@四步,自己編寫一個(gè)starter看看能否成功,驗(yàn)證一下總結(jié)的內(nèi)容是否正確。

3 如何定義自己的starter?

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

該項(xiàng)目名稱為id-generate-starter,注意為了方便我把項(xiàng)目重命名了,原本應(yīng)該是叫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。當(dāng)然如果有需要這里還可以引入多個(gè)autoconfigure或者多個(gè)其他jar包或者。

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

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

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

該項(xiàng)目當(dāng)中包含:pom.xml、spring.factories、IdGenerateAutoConfiguration、IdGenerateService 和 IdProperties 這5個(gè)關(guān)鍵文件,下面我們逐一看看。

先從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>

我們可以看到,這個(gè)文件比較簡(jiǎn)單就引入了:

  • spring-boot-starter:springboot的相關(guān)jar包。
  • spring-boot-autoconfigure:springboot自動(dòng)配置相關(guān)jar包。
  • spring-boot-configuration-processor:springboot生成IDE提示功能相關(guān)jar包。

重點(diǎn)看看spring.factories文件:

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

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

再重點(diǎn)看一下IdGenerateAutoConfiguration

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

該類是一個(gè)使用了@Configuration注解標(biāo)記為了配置類,生效的條件是@ConditionalOnClass注解中檢測(cè)到包含IdProperties.class。并且使用@EnableConfigurationProperties注解會(huì)自動(dòng)注入IdProperties的實(shí)例。

此外,最關(guān)鍵的點(diǎn)是該類里面創(chuàng)建了idGenerateService的bean實(shí)例,這是自動(dòng)配置的精髓。

再看看IdGenerateService

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

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

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

最后看看IdProperties

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

它是一個(gè)配置實(shí)體類,里面包含了相關(guān)的配置文件。使用@ConfigurationProperties注解,會(huì)自動(dòng)把application.properties文件中以sue開通的,參數(shù)名稱跟IdProperties中一樣的參數(shù)值,自動(dòng)注入到IdProperties對(duì)象中。

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

這個(gè)項(xiàng)目主要用于測(cè)試。

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

該項(xiàng)目里面包含: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>

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

application.properties配置資源文件

sue.workId=123

只有一行配置,因?yàn)槲覀兊腎dProperties中目前只需要這一個(gè)參數(shù)。

Application是測(cè)試程序啟動(dòng)類

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

很簡(jiǎn)單,就是一個(gè)普通的springboot啟動(dòng)類

TestRunner是我們的測(cè)試類

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

它實(shí)現(xiàn)了ApplicationRunner接口,所以在springboot啟動(dòng)的時(shí)候會(huì)調(diào)用該類的run方法。

好了,所有自定義starter的代碼和測(cè)試代碼都已經(jīng)就緒。接下,運(yùn)行一下Application類的main方法。

運(yùn)行結(jié)果:

176

完美,驗(yàn)證成功了。

接下來,我們分析一下starter的底層實(shí)現(xiàn)原理。

4 starter的底層原理是什么?

通過上面編寫自己的starter的例子,相信大家對(duì)starter的認(rèn)識(shí)更進(jìn)一步了,現(xiàn)在跟大家一起看看starter的底層是如何實(shí)現(xiàn)的。

id-generate-starter.jar其實(shí)是一個(gè)空項(xiàng)目,依賴于id-generate-autoconfiguration.jar。

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

我們重點(diǎn)分析一下 id-generate-autoconfiguration.jar

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

接下來一個(gè)最重要的問題:IdGenerateConfiguration為什么會(huì)自動(dòng)加載的呢?

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

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

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

要搞明白這個(gè)過程,要從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注解會(huì)引入AutoConfigurationImportSelector類。

該類的selectImports方法一個(gè)關(guān)鍵方法:

@Override
publicString[]selectImports(AnnotationMetadataannotationMetadata){
//配置有沒有配置spring.boot.enableautoconfiguration開關(guān),默認(rèn)為true
//如果為false,則不執(zhí)行自動(dòng)配置的功能,直接返回
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);
//刪除重復(fù)的值
configurations=removeDuplicates(configurations);
//獲取需要排除的規(guī)則列表
Setexclusions=getExclusions(annotationMetadata,attributes);
//檢查
checkExcludedClasses(configurations,exclusions);
//刪除需要排除的值
configurations.removeAll(exclusions);
//根據(jù)配置文件中配置的開關(guān),過濾一部分不滿足條件的值
configurations=filter(configurations,autoConfigurationMetadata);
fireAutoConfigurationImportEvents(configurations,exclusions);
returnStringUtils.toStringArray(configurations);
}

這里就是starter能夠自動(dòng)配置的秘密。

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

先看看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

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

是不是我講錯(cuò)了?

答:其實(shí)沒有。

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

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

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

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

因?yàn)閟pringboot把所有的自動(dòng)配置的類都統(tǒng)一放到spring-boot-autoconfigure.jar下面了:

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

spring.factories文件內(nèi)容如下:

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

SpringBoot這樣集中管理自動(dòng)配置,而不需要從各個(gè)子包中遍歷,我個(gè)人認(rèn)為是為了查找效率。

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

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

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

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

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

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

而我們自己的項(xiàng)目定義的jar應(yīng)該是:

  • xxx-spring-boot-starter.jar


審核編輯 :李倩


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

    關(guān)注

    7

    文章

    4085

    瀏覽量

    68567
  • 代碼
    +關(guān)注

    關(guān)注

    30

    文章

    4977

    瀏覽量

    74416
  • spring
    +關(guān)注

    關(guān)注

    0

    文章

    341

    瀏覽量

    16060
  • SpringBoot
    +關(guān)注

    關(guān)注

    0

    文章

    178

    瀏覽量

    718

原文標(biāo)題:天天用SpringBoot寫代碼,結(jié)果連Starter是什么都不知道?

文章出處:【微信號(hào):芋道源碼,微信公眾號(hào):芋道源碼】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。

收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評(píng)論

    相關(guān)推薦
    熱點(diǎn)推薦

    ADA4522與ad8021一起使用 如何用ADA4522補(bǔ)償零點(diǎn)漂移?

    求助!ADA4522與ad8021一起使用 如何用ADA4522補(bǔ)償零點(diǎn)漂移?有償!目前只知道積分的方法也許可以,但是不知道如何實(shí)現(xiàn)AD8021是同相比例放大
    發(fā)表于 04-16 09:26

    如何使用 Python 將 I2C LCD 與 VIsionFive 一起使用?

    我剛剛發(fā)布了篇關(guān)于將 I2C LCD 與 VisionFive 一起使用的新文章。你可以檢查下。 注意:這是基于 libgpiod 庫,我已經(jīng)在使用這個(gè)庫。您可
    發(fā)表于 03-30 06:52

    如何將 VisionFive 2 與業(yè)余無線電一起使用?

    將 VisionFive 2 與業(yè)余無線電一起使用
    發(fā)表于 03-25 07:01

    為什么不能將 USB 磁盤與 exFat 一起使用?

    不能將 USB 磁盤與 exFat 一起使用
    發(fā)表于 03-04 06:27

    阿里狗16.6,層疊表能打開,但是打開后內(nèi)容縮在一起,調(diào)整間距之后,關(guān)閉在打開還是縮在一起,怎么處理呢

    阿里狗16.6,層疊表能打開,但是打開后內(nèi)容縮在一起,調(diào)整間距之后,關(guān)閉在打開還是縮在一起,怎么處理呢 試過初始化窗口,重新破解,還有重新加載補(bǔ)丁,都不行
    發(fā)表于 02-13 11:01

    回望2025:與162萬開發(fā)者一起,讓AI硬件觸手可及

    設(shè)備中。今天,我們想和大家一起回顧這年的成長(zhǎng),也分享我們對(duì)未來的期待。2025,我們跟開發(fā)者一起完成了1、開發(fā)者生態(tài)持續(xù)壯大截至四季度末,涂鴉平臺(tái)累計(jì)注冊(cè)開發(fā)者超
    的頭像 發(fā)表于 02-12 18:59 ?411次閱讀
    回望2025:與162萬開發(fā)者<b class='flag-5'>一起</b>,讓AI硬件觸手可及

    如何使用SpringBoot、Vue2.0、MySQL開發(fā)套云診所系統(tǒng)?

    ? 如何使用Java語言;前端框架:Vue2.0;后端框架:SpringBoot;數(shù) 據(jù) 庫: MySQL 這些技術(shù)開發(fā)套云診所系統(tǒng)? 技術(shù)棧分析: 后端:SpringBoot
    的頭像 發(fā)表于 11-27 16:02 ?416次閱讀
    如何使用<b class='flag-5'>SpringBoot</b>、Vue2.0、MySQL開發(fā)<b class='flag-5'>一</b>套云診所系統(tǒng)?

    N9H20如何將 SPI 閃存與非作系統(tǒng) BSP 一起使用?

    N9H20如何將 SPI 閃存與非作系統(tǒng) BSP 一起使用?
    發(fā)表于 09-01 08:27

    N9H20如何將非作系統(tǒng) NVTFAT 與 SPI 閃存一起使用?

    N9H20如何將非作系統(tǒng) NVTFAT 與 SPI 閃存一起使用?
    發(fā)表于 09-01 06:38

    光纖能與電線一起走嗎

    光纖與電線在特定條件下可以一起布線,但需嚴(yán)格遵守安全規(guī)范和物理隔離要求,以下是詳細(xì)分析: 、光纖與電線的物理特性差異 光纖 傳輸介質(zhì):以光信號(hào)傳輸數(shù)據(jù),不導(dǎo)電,因此不受電磁干擾(EMI
    的頭像 發(fā)表于 07-14 10:40 ?9955次閱讀

    尋開發(fā)伙伴 一起搞細(xì)胞電阻儀,有興趣的朋友來聊聊!

    尋開發(fā)伙伴 一起搞細(xì)胞電阻儀,有興趣的朋友來聊聊
    發(fā)表于 07-10 15:51

    是否可以將客戶端控件與CYW920706WCDEVAL一起使用?

    是否可以將客戶端控件與CYW920706WCDEVAL一起使用? 我想用它來發(fā)現(xiàn)藍(lán)牙 BR/EDR,然后將其與其他設(shè)備配對(duì)。 有客戶端控制的下載鏈接或文檔嗎? 另外,你有 AIROC Connect 藍(lán)牙應(yīng)用程序的文檔嗎?
    發(fā)表于 07-04 07:50

    無法將Jlink調(diào)試器與CYBT263065EVAL COOLDIM_PRG_BOARD連接在一起怎么解決?

    我無法將 Jlink 調(diào)試器與 CYBT263065EVAL COOLDIM_PRG_BOARD連接在一起。
    發(fā)表于 07-03 06:24

    CyU3PDeviceGpioOverride是否僅與LPP引腳一起使用?

    CyU3PDeviceGpioOverride 是否僅與 LPP 引腳一起使用? 如果使用 CyU3PDeviceGpioOverride,則 io_cfg.gpioSimpleEn 跳過還是反之亦然?
    發(fā)表于 05-15 07:33

    如何將FX3與WSL(Linux 的 Windows 子系統(tǒng))一起使用?

    如何將 FX3 與 WSL(Linux 的 Windows 子系統(tǒng))一起使用? 我在 /dev/ 中找不到任何設(shè)備 我有許多項(xiàng)目在 Windows 上使用VISUAL STUDIO項(xiàng)目進(jìn)行操作,因此請(qǐng)驗(yàn)證該設(shè)備是否在 Windows 上運(yùn)行。
    發(fā)表于 05-06 07:11
    临洮县| 工布江达县| 美姑县| 台州市| 黎平县| 泊头市| 宝丰县| 江孜县| 石景山区| 延川县| 加查县| 潮州市| 科技| 中卫市| 会宁县| 石林| 荔波县| 通辽市| 石首市| 温宿县| 凭祥市| 宁都县| 波密县| 清苑县| 大姚县| 上栗县| 板桥市| 昭通市| 河津市| 马山县| 威宁| 洪雅县| 贵南县| 绩溪县| 景泰县| 新营市| 潼南县| 淮阳县| 澄江县| 惠安县| 五莲县|