首页  

spring-boot-starter原理     所属分类 springboot 浏览量 1225
spring-boot-starter是一个通过maven完成自包含并通过annotation配置使得可被spring上下文发现并实例化的一个可插拔的组件或服务。

两种starter 内部支持的和第三方的
内部  spring-boot-starter-xxx  譬如 spring-boot-starter-web
外部  xxx-spring-boot-starter  譬如 mybatis-spring-boot-starter

内部支持starter 通过 @ConditionalOnClass 来决定是否实例化
当在classpath发现依赖的类时实例化


spring-boot-autoconfigure-2.0.0.RELEASE.jar

嵌入式web server 启动
tomcat jetty undertow

org.springframework.boot.autoconfigure.web.embedded
org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration.class
org.springframework.boot.autoconfigure.web.embedded.JettyWebServerFactoryCustomizer.class
org.springframework.boot.autoconfigure.web.embedded.TomcatWebServerFactoryCustomizer.class
org.springframework.boot.autoconfigure.web.embedded.UndertowWebServerFactoryCustomizer.class



H2Console 的实例化

org.springframework.boot.autoconfigure.h2
H2ConsoleAutoConfiguration
H2ConsoleProperties

@Configuration
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass(WebServlet.class)
@ConditionalOnProperty(prefix = "spring.h2.console", name = "enabled", havingValue = "true", matchIfMissing = false)
@EnableConfigurationProperties(H2ConsoleProperties.class)
public class H2ConsoleAutoConfiguration {

@ConfigurationProperties(prefix = "spring.h2.console")
public class H2ConsoleProperties {



第三方starter mybatis

org.mybatis.spring.boot
mybatis-spring-boot-starter

这个依赖是一个pom,声明了依赖,其中一个就是mybatis的autoConfigure,通过@bean等annotation完成对mybatis的实例化配置


<project xmlns="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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot</artifactId>
    <version>1.3.2</version>
  </parent>
  <artifactId>mybatis-spring-boot-starter</artifactId>
  <name>mybatis-spring-boot-starter</name>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-autoconfigure</artifactId>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
    </dependency>
  </dependencies>
</project>


mybatis-spring-boot-autoconfigure-1.3.2.jar

org.mybatis.spring.boot.autoconfigure
org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer.class
org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration.class
org.mybatis.spring.boot.autoconfigure.MybatisProperties.class
org.mybatis.spring.boot.autoconfigure.SpringBootVFS.class


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


@Bean
@ConditionalOnMissingBean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
   
   
  @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);
    }
  }


starter实现要点
starter 命名 
自动配置类,用来初始化相关的 bean 
spring.factories  配置自动配置类名
自定义属性类 


完整代码
https://gitee.com/dyyx/demos/tree/master/springbootstarter

上一篇     下一篇
单例模式几种实现方式

Mybatis工作原理简介

springboot集成mybatis

top使用技巧

netty4耗时业务处理实例

netty性能优化点