Spring's CompositeMeterRegistryConfiguration

[spring\-boot/spring\-boot\-project/spring\-boot\-actuator\-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/CompositeMeterRegistryConfiguration\.java](https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/CompositeMeterRegistryConfiguration.java) 呼んだのでリンク集を作る

 
```java
/**
 * Configuration for a {@link CompositeMeterRegistry}.
 *
 * @author Andy Wilkinson
 */
@Configuration(proxyBeanMethods = false)
@Conditional(MultipleNonPrimaryMeterRegistriesCondition.class)
class CompositeMeterRegistryConfiguration {

	@Bean
	@Primary
	CompositeMeterRegistry compositeMeterRegistry(Clock clock, List<MeterRegistry> registries) {
		return new CompositeMeterRegistry(clock, registries);
	}

	static class MultipleNonPrimaryMeterRegistriesCondition extends NoneNestedConditions {

		MultipleNonPrimaryMeterRegistriesCondition() {
			super(ConfigurationPhase.REGISTER_BEAN);
		}

		@ConditionalOnMissingBean(MeterRegistry.class)
		static class NoMeterRegistryCondition {

		}

		@ConditionalOnSingleCandidate(MeterRegistry.class)
		static class SingleInjectableMeterRegistry {

		}

	}

}
```

```java
@Conditional(MultipleNonPrimaryMeterRegistriesCondition.class)
```

この `@Configuration` クラス全体は `MultipleNonPrimaryMeterRegistriesCondition` の条件を満たしたときに発動

```java
MultipleNonPrimaryMeterRegistriesCondition
```

なんと中身が無いが、 `extends NoneNestedConditions` とある [NoneNestedConditions](https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/condition/NoneNestedConditions.html)

```java
/** Condition that will match when none of the nested class conditions match. Can be used to create composite conditions */
public abstract class NoneNestedConditions extends AbstractNestedCondition {
```

`@ConditionalOnMissingBean(MeterRegistry.class)` と `@ConditionalOnSingleCandidate(MeterRegistry.class)` が両方発動したときに呼ばれるようだ。。。

# [ConditionalOnMissingBean](https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/condition/ConditionalOnMissingBean.html)

> @Conditional that only matches when no beans meeting the specified requirements are already contained in the BeanFactory. None of the requirements must be met for the condition to match and the requirements do not have to be met by the same bean.

# [ConditionalOnSingleCandidate](https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/condition/ConditionalOnSingleCandidate.html)

> @Conditional that only matches when a bean of the specified class is already contained in the BeanFactory and a single candidate can be determined.

0 or 1 個 bean があるときの idiom だったという訳で

冒頭に戻ると

```java
@Configuration(proxyBeanMethods = false)
@Conditional(MultipleNonPrimaryMeterRegistriesCondition.class)
class CompositeMeterRegistryConfiguration {

    @Bean
    @Primary
    CompositeMeterRegistry compositeMeterRegistry(Clock clock, List<MeterRegistry> registries) {
        return new CompositeMeterRegistry(clock, registries);
    }
```

これは

* Bean が複数だったら `CompositeMeterRegistry compositeMeterRegistry` を作っている。

まぁ見たまんまだ。