リファレンス実装でSpringBootAdmin
#の効果を実現。
- 関連する依存性を追加し、起動クラスに追加し
@EnableXXX
ますが、関連する機能を使用することができます。
ケーススタディ#原理-SpringBootAdmin
- マークは、起動クラスにノート
@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
豆の
- 次に見つけ
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
ビーンの数を定義します@ConditionalOnBean(AdminServerMarkerConfiguration.Marker.class)
[フォーカス]、裁判官のコンテナが開始に追加するクラスの一部であり、このBean、持って@EnableAdminServer
そこにコンテナはアクションの、Marker
Beanは、有効にする設定は、コンテナ内に定義されたBeanクラスに追加されます@EnableConfigurationProperties(AdminServerProperties.class)
ブート構成クラスAdminServerProperties
、クラスは、設定ファイルのバインディングで構成されます
@ConfigurationProperties("spring.boot.admin") public class AdminServerProperties { ... } 复制代码
@Import({AdminServerWebConfiguration.class})
別のクラスのインポート構成は、他のBeanコンテナを追加します
次の質問は次のとおりです。それは、このクラス構成、それの顔を掃除し始めたときに春を作る方法を、分析が@EnableAutoConfiguration
実装ノート学ぶことができます。完全なパスが自動的にクラス構成を設定する必要がありますMETA-INF/spring.factories
、春が起動クラスでスキャンしますパスのすべての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
复制代码
コンプリート〜
#もう一つの例:Redisのプロジェクトの自動設定
- Redisの依存関係を追加する方が簡単Redisのを使用してSpringBootプロジェクトを使用することができます
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
复制代码
SpringBootはRedisの依存度がRedisのを使用することができます達成するために追加されたどのように、RedisTemplate
それを何ですか?
原理:
- 永遠ステップ1: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;
}
}
复制代码
- @設定
- @ConditionalOnClass(RedisOperations.class)であり、クラスパスすると
RedisOperations
、このクラスは、このクラスでは、時間依存Redisのガイドを追加しなければならないことは明らかである来て、そして今、このクラスのパスを見てください。 実際、ガイドは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:[email protected]: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})の導入
LettuceConnectionConfiguration
及びJedisConnectionConfiguration
その2つのクライアントのRedisです。だから、それを有効にされ - LettuceConnectionConfiguration
@Configuration
@ConditionalOnClass(RedisClient.class)
class LettuceConnectionConfiguration extends RedisConnectionConfiguration {}
复制代码
- JedisConnectionConfiguration
@Configuration
@ConditionalOnClass({ GenericObjectPool.class, JedisConnection.class, Jedis.class })
class JedisConnectionConfiguration extends RedisConnectionConfiguration {}
复制代码
- それは、クライアントへの応答クラスパスは、自動的に対応するクラスを設定するために有効になっていることが判明した場合には
- デフォルトでは、レタスですSpringBoot