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

Commit 3e32115

Browse files
committed
Merge pull request #1184 from olivierboudet
* pr/1184: Polish "Add hints for HealthContributors" Add hints for HealthContributors Closes gh-1184
2 parents d7e6bed + da9a0d7 commit 3e32115

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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.nativex.hint.Flag;
8+
import org.springframework.util.ClassUtils;
9+
10+
/**
11+
* A {@link BeanFactoryNativeConfigurationProcessor} that register reflection access for
12+
* actuator health contributor.
13+
*
14+
* @author Olivier Boudet
15+
*/
16+
class HealthContributorNativeConfigurationProcessor implements BeanFactoryNativeConfigurationProcessor {
17+
18+
private static final String HEALTH_CONTRIBUTOR_CLASS_NAME = "org.springframework.boot.actuate.health.HealthContributor";
19+
20+
@Override
21+
public void process(ConfigurableListableBeanFactory beanFactory, NativeConfigurationRegistry registry) {
22+
if (ClassUtils.isPresent(HEALTH_CONTRIBUTOR_CLASS_NAME, beanFactory.getBeanClassLoader())) {
23+
String[] beanNames = beanFactory.getBeanNamesForType(HealthContributor.class);
24+
for (String beanName : beanNames) {
25+
Class<?> beanType = beanFactory.getBeanDefinition(beanName).getResolvableType().toClass();
26+
registry.reflection().forType(beanType).withFlags(Flag.allDeclaredConstructors).build();
27+
}
28+
}
29+
}
30+
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ org.springframework.aot.context.bootstrap.generator.bean.DefaultBeanRegistration
1313
org.springframework.aot.context.bootstrap.generator.infrastructure.nativex.BeanFactoryNativeConfigurationProcessor=\
1414
org.springframework.aot.context.bootstrap.generator.nativex.EnableHttpBeanFactoryNativeConfigurationProcessor,\
1515
org.springframework.boot.actuate.endpoint.annotation.EndpointNativeConfigurationProcessor,\
16+
org.springframework.boot.actuate.endpoint.annotation.HealthContributorNativeConfigurationProcessor,\
1617
org.springframework.boot.context.properties.ConfigurationPropertiesNativeConfigurationProcessor,\
1718
org.springframework.data.RepositoryDefinitionConfigurationProcessor
1819

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
21+
import org.springframework.aot.context.bootstrap.generator.infrastructure.nativex.NativeConfigurationRegistry;
22+
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
23+
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
24+
import org.springframework.boot.actuate.health.HealthContributor;
25+
import org.springframework.boot.actuate.health.PingHealthIndicator;
26+
import org.springframework.nativex.hint.Flag;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
/**
31+
* Tests for {@link HealthContributorNativeConfigurationProcessor}.
32+
*
33+
* @author Olivier Boudet
34+
*/
35+
class HealthContributorNativeConfigurationProcessorTests {
36+
37+
@Test
38+
void registerHealthIndicator() {
39+
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
40+
beanFactory.registerBeanDefinition("noise", BeanDefinitionBuilder.rootBeanDefinition(String.class).getBeanDefinition());
41+
beanFactory.registerBeanDefinition("healthIndicator", BeanDefinitionBuilder.rootBeanDefinition(PingHealthIndicator.class).getBeanDefinition());
42+
NativeConfigurationRegistry registry = process(beanFactory);
43+
assertThat(registry.reflection().getEntries()).singleElement().satisfies((entry) -> {
44+
assertThat(entry.getType()).isEqualTo(PingHealthIndicator.class);
45+
assertThat(entry.getFlags()).contains(Flag.allDeclaredConstructors);
46+
});
47+
}
48+
49+
@Test
50+
void registerHealthContributor() {
51+
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
52+
beanFactory.registerBeanDefinition("noise", BeanDefinitionBuilder.rootBeanDefinition(String.class).getBeanDefinition());
53+
beanFactory.registerBeanDefinition("healthContributor", BeanDefinitionBuilder.rootBeanDefinition(HealthContributor.class).getBeanDefinition());
54+
NativeConfigurationRegistry registry = process(beanFactory);
55+
assertThat(registry.reflection().getEntries()).singleElement().satisfies((entry) -> {
56+
assertThat(entry.getType()).isEqualTo(HealthContributor.class);
57+
assertThat(entry.getFlags()).contains(Flag.allDeclaredConstructors);
58+
});
59+
}
60+
61+
private NativeConfigurationRegistry process(DefaultListableBeanFactory beanFactory) {
62+
NativeConfigurationRegistry registry = new NativeConfigurationRegistry();
63+
new HealthContributorNativeConfigurationProcessor().process(beanFactory, registry);
64+
return registry;
65+
}
66+
67+
}

0 commit comments

Comments
 (0)