Skip to content

Commit e855ed6

Browse files
committed
Find beans for health contributions using the bean factory
1 parent cc87798 commit e855ed6

17 files changed

+70
-93
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/amqp/RabbitHealthContributorAutoConfiguration.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.amqp;
1818

19-
import java.util.Map;
20-
2119
import org.springframework.amqp.rabbit.core.RabbitTemplate;
2220
import org.springframework.boot.actuate.amqp.RabbitHealthIndicator;
2321
import org.springframework.boot.actuate.autoconfigure.health.CompositeHealthContributorConfiguration;
@@ -50,8 +48,8 @@ public RabbitHealthContributorAutoConfiguration() {
5048

5149
@Bean
5250
@ConditionalOnMissingBean(name = { "rabbitHealthIndicator", "rabbitHealthContributor" })
53-
public HealthContributor rabbitHealthContributor(Map<String, RabbitTemplate> rabbitTemplates) {
54-
return createContributor(rabbitTemplates);
51+
public HealthContributor rabbitHealthContributor() {
52+
return createContributor(RabbitTemplate.class);
5553
}
5654

5755
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/cassandra/CassandraHealthContributorConfigurations.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.cassandra;
1818

19-
import java.util.Map;
20-
2119
import com.datastax.oss.driver.api.core.CqlSession;
2220

2321
import org.springframework.boot.actuate.autoconfigure.health.CompositeHealthContributorConfiguration;
@@ -49,8 +47,8 @@ static class CassandraDriverConfiguration
4947

5048
@Bean
5149
@ConditionalOnMissingBean(name = { "cassandraHealthIndicator", "cassandraHealthContributor" })
52-
HealthContributor cassandraHealthContributor(Map<String, CqlSession> sessions) {
53-
return createContributor(sessions);
50+
HealthContributor cassandraHealthContributor() {
51+
return createContributor(CqlSession.class);
5452
}
5553

5654
}
@@ -66,8 +64,8 @@ static class CassandraReactiveDriverConfiguration extends
6664

6765
@Bean
6866
@ConditionalOnMissingBean(name = { "cassandraHealthIndicator", "cassandraHealthContributor" })
69-
ReactiveHealthContributor cassandraHealthContributor(Map<String, CqlSession> sessions) {
70-
return createContributor(sessions);
67+
ReactiveHealthContributor cassandraHealthContributor() {
68+
return createContributor(CqlSession.class);
7169
}
7270

7371
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/couchbase/CouchbaseHealthContributorAutoConfiguration.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.couchbase;
1818

19-
import java.util.Map;
20-
2119
import com.couchbase.client.java.Cluster;
2220

2321
import org.springframework.boot.actuate.autoconfigure.health.CompositeHealthContributorConfiguration;
@@ -55,8 +53,8 @@ public CouchbaseHealthContributorAutoConfiguration() {
5553

5654
@Bean
5755
@ConditionalOnMissingBean(name = { "couchbaseHealthIndicator", "couchbaseHealthContributor" })
58-
public HealthContributor couchbaseHealthContributor(Map<String, Cluster> clusters) {
59-
return createContributor(clusters);
56+
public HealthContributor couchbaseHealthContributor() {
57+
return createContributor(Cluster.class);
6058
}
6159

6260
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/couchbase/CouchbaseReactiveHealthContributorAutoConfiguration.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.couchbase;
1818

19-
import java.util.Map;
20-
2119
import com.couchbase.client.java.Cluster;
2220
import reactor.core.publisher.Flux;
2321

@@ -54,8 +52,8 @@ public CouchbaseReactiveHealthContributorAutoConfiguration() {
5452

5553
@Bean
5654
@ConditionalOnMissingBean(name = { "couchbaseHealthIndicator", "couchbaseHealthContributor" })
57-
public ReactiveHealthContributor couchbaseHealthContributor(Map<String, Cluster> clusters) {
58-
return createContributor(clusters);
55+
public ReactiveHealthContributor couchbaseHealthContributor() {
56+
return createContributor(Cluster.class);
5957
}
6058

6159
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/data/elasticsearch/ElasticsearchReactiveHealthContributorAutoConfiguration.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.data.elasticsearch;
1818

19-
import java.util.Map;
20-
2119
import reactor.core.publisher.Flux;
2220

2321
import org.springframework.boot.actuate.autoconfigure.health.CompositeReactiveHealthContributorConfiguration;
@@ -54,8 +52,8 @@ public ElasticsearchReactiveHealthContributorAutoConfiguration() {
5452

5553
@Bean
5654
@ConditionalOnMissingBean(name = { "elasticsearchHealthIndicator", "elasticsearchHealthContributor" })
57-
public ReactiveHealthContributor elasticsearchHealthContributor(Map<String, ReactiveElasticsearchClient> clients) {
58-
return createContributor(clients);
55+
public ReactiveHealthContributor elasticsearchHealthContributor() {
56+
return createContributor(ReactiveElasticsearchClient.class);
5957
}
6058

6159
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/data/mongo/MongoHealthContributorAutoConfiguration.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.data.mongo;
1818

19-
import java.util.Map;
20-
2119
import org.springframework.boot.actuate.autoconfigure.health.CompositeHealthContributorConfiguration;
2220
import org.springframework.boot.actuate.autoconfigure.health.ConditionalOnEnabledHealthIndicator;
2321
import org.springframework.boot.actuate.data.mongo.MongoHealthIndicator;
@@ -52,8 +50,8 @@ public MongoHealthContributorAutoConfiguration() {
5250

5351
@Bean
5452
@ConditionalOnMissingBean(name = { "mongoHealthIndicator", "mongoHealthContributor" })
55-
public HealthContributor mongoHealthContributor(Map<String, MongoTemplate> mongoTemplates) {
56-
return createContributor(mongoTemplates);
53+
public HealthContributor mongoHealthContributor() {
54+
return createContributor(MongoTemplate.class);
5755
}
5856

5957
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/data/mongo/MongoReactiveHealthContributorAutoConfiguration.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.data.mongo;
1818

19-
import java.util.Map;
20-
2119
import reactor.core.publisher.Flux;
2220

2321
import org.springframework.boot.actuate.autoconfigure.health.CompositeReactiveHealthContributorConfiguration;
@@ -53,8 +51,8 @@ public MongoReactiveHealthContributorAutoConfiguration() {
5351

5452
@Bean
5553
@ConditionalOnMissingBean(name = { "mongoHealthIndicator", "mongoHealthContributor" })
56-
public ReactiveHealthContributor mongoHealthContributor(Map<String, ReactiveMongoTemplate> reactiveMongoTemplates) {
57-
return createContributor(reactiveMongoTemplates);
54+
public ReactiveHealthContributor mongoHealthContributor() {
55+
return createContributor(ReactiveMongoTemplate.class);
5856
}
5957

6058
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/data/redis/RedisHealthContributorAutoConfiguration.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.data.redis;
1818

19-
import java.util.Map;
20-
2119
import org.springframework.boot.actuate.autoconfigure.health.CompositeHealthContributorConfiguration;
2220
import org.springframework.boot.actuate.autoconfigure.health.ConditionalOnEnabledHealthIndicator;
2321
import org.springframework.boot.actuate.data.redis.RedisHealthIndicator;
@@ -53,8 +51,8 @@ public class RedisHealthContributorAutoConfiguration
5351

5452
@Bean
5553
@ConditionalOnMissingBean(name = { "redisHealthIndicator", "redisHealthContributor" })
56-
public HealthContributor redisHealthContributor(Map<String, RedisConnectionFactory> redisConnectionFactories) {
57-
return createContributor(redisConnectionFactories);
54+
public HealthContributor redisHealthContributor() {
55+
return createContributor(RedisConnectionFactory.class);
5856
}
5957

6058
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/data/redis/RedisReactiveHealthContributorAutoConfiguration.java

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.data.redis;
1818

19-
import java.util.Map;
20-
2119
import reactor.core.publisher.Flux;
2220

2321
import org.springframework.boot.actuate.autoconfigure.health.CompositeReactiveHealthContributorConfiguration;
@@ -50,18 +48,14 @@
5048
public class RedisReactiveHealthContributorAutoConfiguration extends
5149
CompositeReactiveHealthContributorConfiguration<RedisReactiveHealthIndicator, ReactiveRedisConnectionFactory> {
5250

53-
private final Map<String, ReactiveRedisConnectionFactory> redisConnectionFactories;
54-
55-
RedisReactiveHealthContributorAutoConfiguration(
56-
Map<String, ReactiveRedisConnectionFactory> redisConnectionFactories) {
51+
RedisReactiveHealthContributorAutoConfiguration() {
5752
super(RedisReactiveHealthIndicator::new);
58-
this.redisConnectionFactories = redisConnectionFactories;
5953
}
6054

6155
@Bean
6256
@ConditionalOnMissingBean(name = { "redisHealthIndicator", "redisHealthContributor" })
6357
public ReactiveHealthContributor redisHealthContributor() {
64-
return createContributor(this.redisConnectionFactories);
58+
return createContributor(ReactiveRedisConnectionFactory.class);
6559
}
6660

6761
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/elasticsearch/ElasticsearchRestHealthContributorAutoConfiguration.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.elasticsearch;
1818

19-
import java.util.Map;
20-
2119
import org.elasticsearch.client.RestClient;
2220

2321
import org.springframework.boot.actuate.autoconfigure.health.CompositeHealthContributorConfiguration;
@@ -52,8 +50,8 @@ public ElasticsearchRestHealthContributorAutoConfiguration() {
5250

5351
@Bean
5452
@ConditionalOnMissingBean(name = { "elasticsearchHealthIndicator", "elasticsearchHealthContributor" })
55-
public HealthContributor elasticsearchHealthContributor(Map<String, RestClient> clients) {
56-
return createContributor(clients);
53+
public HealthContributor elasticsearchHealthContributor() {
54+
return createContributor(RestClient.class);
5755
}
5856

5957
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/hazelcast/HazelcastHealthContributorAutoConfiguration.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.hazelcast;
1818

19-
import java.util.Map;
20-
2119
import com.hazelcast.core.HazelcastInstance;
2220

2321
import org.springframework.boot.actuate.autoconfigure.health.CompositeHealthContributorConfiguration;
@@ -52,8 +50,8 @@ public HazelcastHealthContributorAutoConfiguration() {
5250

5351
@Bean
5452
@ConditionalOnMissingBean(name = { "hazelcastHealthIndicator", "hazelcastHealthContributor" })
55-
public HealthContributor hazelcastHealthContributor(Map<String, HazelcastInstance> hazelcastInstances) {
56-
return createContributor(hazelcastInstances);
53+
public HealthContributor hazelcastHealthContributor() {
54+
return createContributor(HazelcastInstance.class);
5755
}
5856

5957
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/health/AbstractCompositeHealthContributorConfiguration.java

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,10 @@
1919
import java.util.Map;
2020
import java.util.function.Function;
2121

22+
import org.springframework.beans.BeansException;
23+
import org.springframework.beans.factory.BeanFactory;
24+
import org.springframework.beans.factory.BeanFactoryAware;
25+
import org.springframework.beans.factory.ListableBeanFactory;
2226
import org.springframework.util.Assert;
2327

2428
/**
@@ -32,10 +36,12 @@
3236
* @author Phillip Webb
3337
* @since 2.2.0
3438
*/
35-
public abstract class AbstractCompositeHealthContributorConfiguration<C, I extends C, B> {
39+
public abstract class AbstractCompositeHealthContributorConfiguration<C, I extends C, B> implements BeanFactoryAware {
3640

3741
private final Function<B, I> indicatorFactory;
3842

43+
private volatile ListableBeanFactory beanFactory;
44+
3945
/**
4046
* Creates a {@code AbstractCompositeHealthContributorConfiguration} that will use the
4147
* given {@code indicatorFactory} to create health indicator instances.
@@ -46,6 +52,10 @@ protected AbstractCompositeHealthContributorConfiguration(Function<B, I> indicat
4652
this.indicatorFactory = indicatorFactory;
4753
}
4854

55+
protected final C createContributor(Class<B> beanType) {
56+
return createContributor(this.beanFactory.getBeansOfType(beanType, false, false));
57+
}
58+
4959
protected final C createContributor(Map<String, B> beans) {
5060
Assert.notEmpty(beans, "Beans must not be empty");
5161
if (beans.size() == 1) {
@@ -60,4 +70,10 @@ protected I createIndicator(B bean) {
6070
return this.indicatorFactory.apply(bean);
6171
}
6272

73+
@Override
74+
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
75+
Assert.isInstanceOf(ListableBeanFactory.class, beanFactory);
76+
this.beanFactory = (ListableBeanFactory) beanFactory;
77+
}
78+
6379
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/jms/JmsHealthContributorAutoConfiguration.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.jms;
1818

19-
import java.util.Map;
20-
2119
import jakarta.jms.ConnectionFactory;
2220

2321
import org.springframework.boot.actuate.autoconfigure.health.CompositeHealthContributorConfiguration;
@@ -52,8 +50,8 @@ public JmsHealthContributorAutoConfiguration() {
5250

5351
@Bean
5452
@ConditionalOnMissingBean(name = { "jmsHealthIndicator", "jmsHealthContributor" })
55-
public HealthContributor jmsHealthContributor(Map<String, ConnectionFactory> connectionFactories) {
56-
return createContributor(connectionFactories);
53+
public HealthContributor jmsHealthContributor() {
54+
return createContributor(ConnectionFactory.class);
5755
}
5856

5957
}

0 commit comments

Comments
 (0)