Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.

Commit 489a1c8

Browse files
committed
Add native configuration for HealthContributor classes
1 parent e189a72 commit 489a1c8

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.springframework.boot.actuate.endpoint.annotation;
2+
3+
import org.springframework.aot.context.bootstrap.generator.infrastructure.nativex.BeanFactoryNativeConfigurationProcessor;
4+
import org.springframework.aot.context.bootstrap.generator.infrastructure.nativex.NativeConfigurationRegistry;
5+
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
6+
import org.springframework.boot.actuate.health.HealthContributor;
7+
import org.springframework.boot.actuate.health.HealthIndicator;
8+
import org.springframework.nativex.hint.Flag;
9+
import org.springframework.util.ClassUtils;
10+
11+
/**
12+
* A {@link BeanFactoryNativeConfigurationProcessor} that register reflection access for
13+
* actuator health contributor.
14+
*
15+
* @author Olivier Boudet
16+
*/
17+
18+
public class HealthContributorNativeConfigurationProcessor implements BeanFactoryNativeConfigurationProcessor {
19+
@Override
20+
public void process(ConfigurableListableBeanFactory beanFactory, NativeConfigurationRegistry registry) {
21+
if (ClassUtils.isPresent("org.springframework.boot.actuate.health.HealthContributor", beanFactory.getBeanClassLoader())) {
22+
String[] healthContributorBeanNames = beanFactory.getBeanNamesForType(HealthContributor.class);
23+
for (String bean : healthContributorBeanNames) {
24+
registry.reflection().forType(beanFactory.getBean(bean, HealthContributor.class).getClass()).withFlags(Flag.allDeclaredConstructors).build();
25+
}
26+
}
27+
}
28+
}

spring-aot/src/main/resources/META-INF/spring.factories

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ org.springframework.aot.context.bootstrap.generator.bean.DefaultBeanRegistration
1212

1313
org.springframework.aot.context.bootstrap.generator.infrastructure.nativex.BeanFactoryNativeConfigurationProcessor=\
1414
org.springframework.boot.actuate.endpoint.annotation.EndpointNativeConfigurationProcessor,\
15+
org.springframework.boot.actuate.endpoint.annotation.HealthContributorNativeConfigurationProcessor,\
1516
org.springframework.boot.context.properties.ConfigurationPropertiesNativeConfigurationProcessor,\
1617
org.springframework.data.RepositoryDefinitionConfigurationProcessor
1718

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2019-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.endpoint.annotation;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.springframework.aot.context.bootstrap.generator.infrastructure.nativex.NativeConfigurationRegistry;
21+
import org.springframework.aot.context.bootstrap.generator.infrastructure.nativex.NativeReflectionEntry;
22+
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
23+
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
24+
import org.springframework.boot.actuate.health.Health;
25+
import org.springframework.boot.actuate.health.HealthIndicator;
26+
import org.springframework.nativex.hint.Flag;
27+
28+
import java.util.List;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
32+
/**
33+
* Tests for {@link HealthContributorNativeConfigurationProcessor}.
34+
*
35+
* @author Olivier Boudet
36+
*/
37+
class HealthContributorNativeConfigurationProcessorTests {
38+
39+
@Test
40+
void registerHealthIndicator() {
41+
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
42+
beanFactory.registerBeanDefinition("noise", BeanDefinitionBuilder.rootBeanDefinition(String.class).getBeanDefinition());
43+
beanFactory.registerBeanDefinition("healthIndicator", BeanDefinitionBuilder.rootBeanDefinition(TestHealthIndicator.class).getBeanDefinition());
44+
NativeConfigurationRegistry registry = process(beanFactory);
45+
List<NativeReflectionEntry> entries = registry.reflection().getEntries();
46+
assertThat(entries).anySatisfy((entry) -> {
47+
assertThat(entry.getType()).isEqualTo(TestHealthIndicator.class);
48+
assertThat(entry.getFlags()).contains(Flag.allDeclaredConstructors);
49+
});
50+
assertThat(entries).hasSize(1);
51+
}
52+
53+
private NativeConfigurationRegistry process(DefaultListableBeanFactory beanFactory) {
54+
NativeConfigurationRegistry registry = new NativeConfigurationRegistry();
55+
new HealthContributorNativeConfigurationProcessor().process(beanFactory, registry);
56+
return registry;
57+
}
58+
59+
@SuppressWarnings("unused")
60+
static class TestHealthIndicator implements HealthIndicator {
61+
62+
@Override
63+
public Health health() {
64+
return Health.up().build();
65+
}
66+
}
67+
68+
}

0 commit comments

Comments
 (0)