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

創(chuàng)新互聯(lián)主營祁陽網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都App制作,祁陽h5重慶小程序開發(fā)搭建,祁陽網(wǎng)站營銷推廣歡迎祁陽等地區(qū)企業(yè)咨詢
為了提升Spring項(xiàng)目的開發(fā)效率,簡化一些配置,Spring官方引入了SpringBoot。
當(dāng)然,引入SpringBoot還有其他原因,在這里就不過多描述了。
本文重點(diǎn)跟大家一起聊聊SpringBoot的starter機(jī)制,因?yàn)樗匾恕?/p>
1 為什么要用starter?
在SpringBoot還沒有出來之前,我們使用Spring開發(fā)項(xiàng)目。如果程序需要連接數(shù)據(jù)庫,我們一般會(huì)使用Hibernate或Mybatis等ORM框架,這里我以Mybatis為例,具體的操作步驟如下:
- 到maven倉庫去找需要引入的mybatis jar包,選取合適的版本。
- 到maven倉庫去找mybatis-spring整合的jar包,選取合適的版本。
- 在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ī)制。
2 starter有哪些要素?
我們首先一起看看mybatis-spring-boot-starter.jar是如何定義的。
可以看到它的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è)沒有啥重要的信息。
4.0.0
org.mybatis.spring.boot
mybatis-spring-boot
1.3.1
mybatis-spring-boot-starter
mybatis-spring-boot-starter
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-jdbc
org.mybatis.spring.boot
mybatis-spring-boot-autoconfigure
org.mybatis
mybatis
org.mybatis
mybatis-spring
從上面可以看出,pom.xml文件中會(huì)引入一些jar包,其中除了引入spring-boot-starter,之外重點(diǎn)看一下:mybatis-spring-boot-autoconfigure。
我們找到mybatis-spring-boot-autoconfigure.jar文件,打開這個(gè)文件。
里面包含如下文件:
- 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.json和additional-spring-configuration-metadata.json的功能差不多,我們?cè)赼pplicationContext.properties文件中輸入spring時(shí),會(huì)自動(dòng)出現(xiàn)下面的配置信息可供選擇,就是這個(gè)功能了。
來自靈魂的一問:這兩個(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)
public class MybatisProperties {
public static final String MYBATIS_PREFIX = "mybatis";
private String configLocation;
private String[] mapperLocations;
private String typeAliasesPackage;
private String typeHandlersPackage;
private boolean checkConfigLocation = false;
private ExecutorType executorType;
private Properties configurationProperties;
@NestedConfigurationProperty
private Configuration configuration;
public String getConfigLocation() {
return this.configLocation;
}
public void setConfigLocation(String configLocation) {
this.configLocation = configLocation;
}
@Deprecated
public String getConfig() {
return this.configLocation;
}
@Deprecated
public void setConfig(String config) {
this.configLocation = config;
}
public String[] getMapperLocations() {
return this.mapperLocations;
}
public void setMapperLocations(String[] mapperLocations) {
this.mapperLocations = mapperLocations;
}
public String getTypeHandlersPackage() {
return this.typeHandlersPackage;
}
public void setTypeHandlersPackage(String typeHandlersPackage) {
this.typeHandlersPackage = typeHandlersPackage;
}
public String getTypeAliasesPackage() {
return this.typeAliasesPackage;
}
public void setTypeAliasesPackage(String typeAliasesPackage) {
this.typeAliasesPackage = typeAliasesPackage;
}
public boolean isCheckConfigLocation() {
return this.checkConfigLocation;
}
public void setCheckConfigLocation(boolean checkConfigLocation) {
this.checkConfigLocation = checkConfigLocation;
}
public ExecutorType getExecutorType() {
return this.executorType;
}
public void setExecutorType(ExecutorType executorType) {
this.executorType = executorType;
}
public Properties getConfigurationProperties() {
return configurationProperties;
}
public void setConfigurationProperties(Properties configurationProperties) {
this.configurationProperties = configurationProperties;
}
public Configuration getConfiguration() {
return configuration;
}
public void setConfiguration(Configuration configuration) {
this.configuration = configuration;
}
public Resource[] resolveMapperLocations() {
ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
Listresources = new ArrayList ();
if (this.mapperLocations != null) {
for (String mapperLocation : this.mapperLocations) {
try {
Resource[] mappers = resourceResolver.getResources(mapperLocation);
resources.addAll(Arrays.asList(mappers));
} catch (IOException e) {
}
}
}
return resources.toArray(new Resource[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)
public class MybatisAutoConfiguration {
private static final Logger logger = LoggerFactory.getLogger(MybatisAutoConfiguration.class);
private final MybatisProperties properties;
private final Interceptor[] interceptors;
private final ResourceLoader resourceLoader;
private final DatabaseIdProvider databaseIdProvider;
private final ListconfigurationCustomizers;
public MybatisAutoConfiguration(MybatisProperties properties,
ObjectProviderinterceptorsProvider,
ResourceLoader resourceLoader,
ObjectProviderdatabaseIdProvider,
ObjectProvider> configurationCustomizersProvider) {
this.properties = properties;
this.interceptors = interceptorsProvider.getIfAvailable();
this.resourceLoader = resourceLoader;
this.databaseIdProvider = databaseIdProvider.getIfAvailable();
this.configurationCustomizers = configurationCustomizersProvider.getIfAvailable();
}
@PostConstruct
public void checkConfigFileExists() {
if (this.properties.isCheckConfigLocation() && StringUtils.hasText(this.properties.getConfigLocation())) {
Resource resource = this.resourceLoader.getResource(this.properties.getConfigLocation());
Assert.state(resource.exists(), "Cannot find config location: " + resource
+ " (please add config file or check your Mybatis configuration)");
}
}
@Bean
@ConditionalOnMissingBean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setDataSource(dataSource);
factory.setVfs(SpringBootVFS.class);
if (StringUtils.hasText(this.properties.getConfigLocation())) {
factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
}
Configuration configuration = this.properties.getConfiguration();
if (configuration == null && !StringUtils.hasText(this.properties.getConfigLocation())) {
configuration = new Configuration();
}
if (configuration != null && !CollectionUtils.isEmpty(this.configurationCustomizers)) {
for (ConfigurationCustomizer customizer : 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());
}
return factory.getObject();
}
@Bean
@ConditionalOnMissingBean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
ExecutorType executorType = this.properties.getExecutorType();
if (executorType != null) {
return new SqlSessionTemplate(sqlSessionFactory, executorType);
} else {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
public static class AutoConfiguredMapperScannerRegistrar
implements BeanFactoryAware, ImportBeanDefinitionRegistrar, ResourceLoaderAware {
private BeanFactory beanFactory;
private ResourceLoader resourceLoader;
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);
try {
if (this.resourceLoader != null) {
scanner.setResourceLoader(this.resourceLoader);
}
Listpackages = AutoConfigurationPackages.get(this.beanFactory);
if (logger.isDebugEnabled()) {
for (String pkg : packages) {
logger.debug("Using auto-configuration base package '{}'", pkg);
}
}
scanner.setAnnotationClass(Mapper.class);
scanner.registerFilters();
scanner.doScan(StringUtils.toStringArray(packages));
} catch (IllegalStateException ex) {
logger.debug("Could not determine auto-configuration package, automatic mapper scanning disabled.", ex);
}
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
}
@org.springframework.context.annotation.Configuration
@Import({ AutoConfiguredMapperScannerRegistrar.class })
@ConditionalOnMissingBean(MapperFactoryBean.class)
public static class MapperScannerRegistrarNotFoundConfiguration {
@PostConstruct
public void afterPropertiesSet() {
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
里面只有一行配置,即key為EnableAutoConfiguration,value為MybatisAutoConfiguration。
好了,介紹了這么多東西,現(xiàn)在我們來總結(jié)一下。
starter幾個(gè)要素如下圖所示:
那么,編寫starter需要哪些步驟?
- 需要定義一個(gè)名稱為xxx-spring-boot-starter的空項(xiàng)目,里面不包含任何代碼,可以有pom.xml和pom.properties文件。
- pom.xml文件中包含了名稱為xxx-spring-boot-autoconfigure的項(xiàng)目。
- xxx-spring-boot-autoconfigure項(xiàng)目中包含了名稱為xxxAutoConfiguration的類,該類可以定義一些bean實(shí)例。當(dāng)然,Configuration類上可以打一些如:ConditionalOnClass、ConditionalOnBean、EnableConfigurationProperties等注解。
- 需要在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的,如下圖所示:
pom.xml文件定義如下:
4.0.0
1.3.1
com.sue
id-generate-spring-boot-starter
id-generate-spring-boot-starter
com.sue
id-generate-spring-boot-autoconfigure
1.3.1
我們看到,它只引入了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,如下圖所示:
該項(xiàng)目當(dāng)中包含:pom.xml、spring.factories、IdGenerateAutoConfiguration、IdGenerateService 和 IdProperties 這5個(gè)關(guān)鍵文件,下面我們逐一看看。
先從pom.xml
org.springframework.boot
spring-boot-starter-parent
2.0.4.RELEASE
4.0.0
1.3.1
com.sue
id-generate-spring-boot-autoconfigure
id-generate-spring-boot-autoconfigure
orghttp://fisionsoft.com.cn/article/djjpooo.html


咨詢
建站咨詢
