@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(); } ...... } 复制代码
@Configuration
这是一个配置类,可以看到这个类里面通过 @Bean
定义了很多bean @ConditionalOnBean(AdminServerMarkerConfiguration.Marker.class)
[重点],判断容器中有这个bean,这就是上一部分,在启动类上添加 @EnableAdminServer
的作用,当容器中有 Marker
这个bean时,这个Configuration才生效,才会向容器中添加这个类中定义的bean @EnableConfigurationProperties(AdminServerProperties.class)
启动配置类 AdminServerProperties
,将配置类与配置文件绑定 @ConfigurationProperties("spring.boot.admin") public class AdminServerProperties { ... } 复制代码
@Import({AdminServerWebConfiguration.class})
导入另外一个配置类,向容器中添加其他bean 接下来的问题是:如何让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 复制代码
完成~
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> 复制代码
SpringBoot是如何实现添加了redis依赖就可以使用Redis, RedisTemplate
的呢?
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; } } 复制代码
RedisOperations
这个类时,那么很明显,这个类肯定是在添加redis依赖的时候导进来的,现在看一下这个类的路径。
确实是通过redis的起步依赖导进来的,这就是为什么添加了redis的起步依赖,redis就可以直接使用了 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; ...... } 复制代码
LettuceConnectionConfiguration
和 JedisConnectionConfiguration
,这两个都是redis的客户端。 那么启用哪一个呢 @Configuration @ConditionalOnClass(RedisClient.class) class LettuceConnectionConfiguration extends RedisConnectionConfiguration {} 复制代码
@Configuration @ConditionalOnClass({ GenericObjectPool.class, JedisConnection.class, Jedis.class }) class JedisConnectionConfiguration extends RedisConnectionConfiguration {} 复制代码