1. spring 框架中的 starter

spring 自动配置的依赖,:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-autoconfigure</artifactId>
  4. </dependency>

springboot 框架本身大量组件的自动配置就是通过spring-boot-autoconfigure 实现的:
image.png
spring.factories 文件 EnableAutoConfiguration 后面配置了大量的自动配置类:

image.png
拿 jpa 的starter 举个例子:

  1. @Configuration
  2. @ConditionalOnBean(DataSource.class)
  3. @ConditionalOnClass(JpaRepository.class)
  4. @ConditionalOnMissingBean({ JpaRepositoryFactoryBean.class,
  5. JpaRepositoryConfigExtension.class })
  6. @ConditionalOnProperty(prefix = "spring.data.jpa.repositories", name = "enabled", havingValue = "true", matchIfMissing = true)
  7. @Import(JpaRepositoriesAutoConfigureRegistrar.class)
  8. @AutoConfigureAfter(HibernateJpaAutoConfiguration.class)
  9. public class JpaRepositoriesAutoConfiguration {
  10. }

spring.factories 配置了上述配置类,JpaRepositoriesAutoConfiguration 会根据各类条件选择性加载。

  1. org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration

通过 conditionOnXXx 注解,先判断spring容器是否已经加载相应的资源,且配置开关是开启的,spring 容器 jpa相关 bean 还没初始化,才会就行jpa配置的初始化;AutoConfigureAfter 注解配置了这个类的初始化顺序

jpa 配置是在HibernateJpaAutoConfiguration初始化之后再执行的:

  1. @Configuration
  2. @ConditionalOnClass({ LocalContainerEntityManagerFactoryBean.class, EntityManager.class })
  3. @Conditional(HibernateEntityManagerCondition.class)
  4. @EnableConfigurationProperties(JpaProperties.class)
  5. @AutoConfigureAfter({ DataSourceAutoConfiguration.class })
  6. @Import(HibernateJpaConfiguration.class)
  7. public class HibernateJpaAutoConfiguration {
  8. @Order(Ordered.HIGHEST_PRECEDENCE + 20)
  9. static class HibernateEntityManagerCondition extends SpringBootCondition {
  10. private static final String[] CLASS_NAMES = {
  11. "org.hibernate.ejb.HibernateEntityManager",
  12. "org.hibernate.jpa.HibernateEntityManager" };
  13. @Override
  14. public ConditionOutcome getMatchOutcome(ConditionContext context,
  15. AnnotatedTypeMetadata metadata) {
  16. ConditionMessage.Builder message = ConditionMessage
  17. .forCondition("HibernateEntityManager");
  18. for (String className : CLASS_NAMES) {
  19. if (ClassUtils.isPresent(className, context.getClassLoader())) {
  20. return ConditionOutcome
  21. .match(message.found("class").items(Style.QUOTE, className));
  22. }
  23. }
  24. return ConditionOutcome.noMatch(message.didNotFind("class", "classes")
  25. .items(Style.QUOTE, Arrays.asList(CLASS_NAMES)));
  26. }
  27. }

2. 自定义 starter

pom 依赖:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. <optional>true</optional>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-configuration-processor</artifactId>
  9. <optional>true</optional>
  10. </dependency>

在 META-INF/spring.factories 中配 org.springframework.boot.autoconfigure.EnableAutoConfiguration =自己的配置类

配置类使用的注解有:
Configuration 注解
EnableConfigurationProperties 注解
NestedConfigurationProperty 注解
import 注解
ConditionalOnProperty注解
ConditionalOnClass注解
ConditionalOnMissingBean 注解
AutoConfigureAfter 注解