转载

SpringBoot的自动配置原理与自定义SpringBootStarter

  • 在启动类上标记注解 @EnableAdminServer
@EnableAdminServer
@SpringBootApplication
public class AdminApplication {
    public static void main(String[] args) {
        SpringApplication.run(AdminApplication.class, args);
    }
}
复制代码
  • 点进去发现只是导入了一个类 AdminServerMarkerConfiguration.class
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AdminServerMarkerConfiguration.class)
public @interface EnableAdminServer {

}
复制代码
  • 点进这个类 AdminServerMarkerConfiguration 发现只是向容器中添加了一个bean Marker
public class AdminServerMarkerConfiguration {
    @Bean
    public Marker adminServerMarker() {
        return new Marker();
    }

    public static class Marker {
    }
}
复制代码
  • 所以这一部分的总结就是:添加 @EnableAdminServer 注解,向容器中添加一个类型为 Marker 的bean
  • 接下来找到 AdminServerAutoConfiguration 这个类,一般默认SpringBoot的自动配置类都是以 XXXAutoConfiguration 命名,所以很容易找到,例如 RedisAutoConfiguration ...
@Configuration
@ConditionalOnBean(AdminServerMarkerConfiguration.Marker.class)
@EnableConfigurationProperties(AdminServerProperties.class)
@Import({AdminServerWebConfiguration.class})
public class AdminServerAutoConfiguration {
 @Bean
    @ConditionalOnMissingBean
    public InstanceRegistry instanceRegistry(InstanceRepository instanceRepository,
                                             InstanceIdGenerator instanceIdGenerator) {
        return new InstanceRegistry(instanceRepository, instanceIdGenerator);
    }

    @Bean
    @ConditionalOnMissingBean
    public InstanceIdGenerator instanceIdGenerator() {
        return new HashingInstanceUrlIdGenerator();
    }
......
}
复制代码
  • 解读这个类的注解可发现以下几点:
    1. @Configuration 这是一个配置类,可以看到这个类里面通过 @Bean 定义了很多bean
    2. @ConditionalOnBean(AdminServerMarkerConfiguration.Marker.class) [重点],判断容器中有这个bean,这就是上一部分,在启动类上添加 @EnableAdminServer 的作用,当容器中有 Marker 这个bean时,这个Configuration才生效,才会向容器中添加这个类中定义的bean
    3. @EnableConfigurationProperties(AdminServerProperties.class) 启动配置类 AdminServerProperties ,将配置类与配置文件绑定
    @ConfigurationProperties("spring.boot.admin")
          public class AdminServerProperties {
            ...
          }
    复制代码
    1. @Import({AdminServerWebConfiguration.class}) 导入另外一个配置类,向容器中添加其他bean

接下来的问题是:如何让Spring在启动的时候扫面到这个配置类呢,通过分析 @EnableAutoConfiguration 注解的实现可得知:将需要自动配置的类的全路径配置在 META-INF/spring.factories ,Spring会在启动的时候扫描类路径下所有的 META-INF/spring.factories 文件

  • 如SpringBootAdmin的配置:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=/
  de.codecentric.boot.admin.server.config.AdminServerAutoConfiguration,/
  de.codecentric.boot.admin.server.config.AdminServerNotifierAutoConfiguration,/
  de.codecentric.boot.admin.server.config.AdminServerHazelcastAutoConfiguration,/
  de.codecentric.boot.admin.se  rver.config.AdminServerCloudFoundryAutoConfiguration
复制代码
SpringBoot的自动配置原理与自定义SpringBootStarter

完成~

# 另外一个例子: 项目中Redis的自动配置

  • SpringBoot项目中使用Redis更加简单,添加Redis依赖即可使用
<dependency>
                 <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
 </dependency>
复制代码

SpringBoot是如何实现添加了redis依赖就可以使用Redis, RedisTemplate 的呢?

原理:

  • 永远第一步:找到Redis的自动配置类 RedisAutoConfiguration
@Configuration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })
public class RedisAutoConfiguration {

	@Bean
	@ConditionalOnMissingBean(name = "redisTemplate")
	public RedisTemplate<Object, Object> redisTemplate(
			RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
		RedisTemplate<Object, Object> template = new RedisTemplate<>();
		template.setConnectionFactory(redisConnectionFactory);
		return template;
	}

	@Bean
	@ConditionalOnMissingBean
	public StringRedisTemplate stringRedisTemplate(
			RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
		StringRedisTemplate template = new StringRedisTemplate();
		template.setConnectionFactory(redisConnectionFactory);
		return template;
	}

}
复制代码
  • @Configuration
  • @ConditionalOnClass(RedisOperations.class) 当类路径下有 RedisOperations 这个类时,那么很明显,这个类肯定是在添加redis依赖的时候导进来的,现在看一下这个类的路径。
    SpringBoot的自动配置原理与自定义SpringBootStarter
    确实是通过redis的起步依赖导进来的,这就是为什么添加了redis的起步依赖,redis就可以直接使用了
  • @EnableConfigurationProperties(RedisProperties.class) 激活配置类 RedisProperties
@ConfigurationProperties(prefix = "spring.redis")
public class RedisProperties {
/**
	 * Database index used by the connection factory.
	 */
	private int database = 0;

	/**
	 * Connection URL. Overrides host, port, and password. User is ignored. Example:
	 * redis://user:password@example.com:6379
	 */
	private String url;

	/**
	 * Redis server host.
	 */
	private String host = "localhost";

	/**
	 * Login password of the redis server.
	 */
	private String password;

	/**
	 * Redis server port.
	 */
	private int port = 6379;
        ......
  }
复制代码
  • @Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class }) 导入 LettuceConnectionConfigurationJedisConnectionConfiguration ,这两个都是redis的客户端。 那么启用哪一个呢
  • LettuceConnectionConfiguration
@Configuration
@ConditionalOnClass(RedisClient.class)
class LettuceConnectionConfiguration extends RedisConnectionConfiguration {}
复制代码
  • JedisConnectionConfiguration
@Configuration
@ConditionalOnClass({ GenericObjectPool.class, JedisConnection.class, Jedis.class })
class JedisConnectionConfiguration extends RedisConnectionConfiguration {}
复制代码
  • 可以看到当类路径下有响应的客户端时,就启用相应的自动配置类
SpringBoot的自动配置原理与自定义SpringBootStarter
  • 而SpringBoot默认使用的是lettuce
原文  https://juejin.im/post/5dbd03f35188251d805ff14c
正文到此结束
Loading...