Spring Boot 核心技术Springboot整合

[原创]SpringBoot的自动配置原理与自定义SpringB

2019-11-02  本文已影响0人  垃圾简书_吃枣药丸

以SpringBootAdmin的实现方式作为参考

# 实现的效果:

# 原理-SpringBootAdmin为例

@EnableAdminServer
@SpringBootApplication
public class AdminApplication {
    public static void main(String[] args) {
        SpringApplication.run(AdminApplication.class, args);
    }
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AdminServerMarkerConfiguration.class)
public @interface EnableAdminServer {

}
public class AdminServerMarkerConfiguration {
    @Bean
    public Marker adminServerMarker() {
        return new Marker();
    }

    public static class Marker {
    }
}


@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();
    }
......
}

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

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
image.png

完成~

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

 <dependency>
                 <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
 </dependency>

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

原理:
@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;
    }

}
@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;
        ......
  }
@Configuration
@ConditionalOnClass(RedisClient.class)
class LettuceConnectionConfiguration extends RedisConnectionConfiguration {}
@Configuration
@ConditionalOnClass({ GenericObjectPool.class, JedisConnection.class, Jedis.class })
class JedisConnectionConfiguration extends RedisConnectionConfiguration {}
image.png
上一篇下一篇

猜你喜欢

热点阅读