Allow ConditionalOnEnabledEndpoint to be set at class level

See gh-15451
pull/15692/head
Stephane Nicoll 6 years ago
parent 580c73a026
commit 2d2aa96c05

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -92,7 +92,7 @@ import org.springframework.core.env.Environment;
* @see Endpoint * @see Endpoint
*/ */
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD) @Target({ ElementType.METHOD, ElementType.TYPE })
@Documented @Documented
@Conditional(OnEnabledEndpointCondition.class) @Conditional(OnEnabledEndpointCondition.class)
public @interface ConditionalOnEnabledEndpoint { public @interface ConditionalOnEnabledEndpoint {

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -90,15 +90,11 @@ class OnEnabledEndpointCondition extends SpringBootCondition {
private AnnotationAttributes getEndpointAttributes(ConditionContext context, private AnnotationAttributes getEndpointAttributes(ConditionContext context,
AnnotatedTypeMetadata metadata) { AnnotatedTypeMetadata metadata) {
Assert.state( return getEndpointAttributes(getEndpointType(context, metadata));
metadata instanceof MethodMetadata
&& metadata.isAnnotated(Bean.class.getName()),
"OnEnabledEndpointCondition may only be used on @Bean methods");
Class<?> endpointType = getEndpointType(context, (MethodMetadata) metadata);
return getEndpointAttributes(endpointType);
} }
private Class<?> getEndpointType(ConditionContext context, MethodMetadata metadata) { private Class<?> getEndpointType(ConditionContext context,
AnnotatedTypeMetadata metadata) {
Map<String, Object> attributes = metadata Map<String, Object> attributes = metadata
.getAnnotationAttributes(ConditionalOnEnabledEndpoint.class.getName()); .getAnnotationAttributes(ConditionalOnEnabledEndpoint.class.getName());
if (attributes != null && attributes.containsKey("endpoint")) { if (attributes != null && attributes.containsKey("endpoint")) {
@ -107,15 +103,19 @@ class OnEnabledEndpointCondition extends SpringBootCondition {
return target; return target;
} }
} }
// We should be safe to load at this point since we are in the REGISTER_BEAN phase Assert.state(
metadata instanceof MethodMetadata
&& metadata.isAnnotated(Bean.class.getName()),
"OnEnabledEndpointCondition must be used on @Bean methods when the endpoint is not specified");
MethodMetadata methodMetadata = (MethodMetadata) metadata;
try { try {
return ClassUtils.forName(metadata.getReturnTypeName(), return ClassUtils.forName(methodMetadata.getReturnTypeName(),
context.getClassLoader()); context.getClassLoader());
} }
catch (Throwable ex) { catch (Throwable ex) {
throw new IllegalStateException("Failed to extract endpoint id for " throw new IllegalStateException("Failed to extract endpoint id for "
+ metadata.getDeclaringClassName() + "." + metadata.getMethodName(), + methodMetadata.getDeclaringClassName() + "."
ex); + methodMetadata.getMethodName(), ex);
} }
} }

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -148,6 +148,14 @@ public class ConditionalOnEnabledEndpointTests {
.run((context) -> assertThat(context).hasBean("fooBar")); .run((context) -> assertThat(context).hasBean("fooBar"));
} }
@Test
public void outcomeWhenEndpointEnabledPropertyIsFalseOnClassShouldNotMatch() {
this.contextRunner.withPropertyValues("management.endpoint.foo.enabled=false")
.withUserConfiguration(
FooEndpointEnabledByDefaultTrueOnConfigurationConfiguration.class)
.run((context) -> assertThat(context).doesNotHaveBean("foo"));
}
@Endpoint(id = "foo", enableByDefault = true) @Endpoint(id = "foo", enableByDefault = true)
static class FooEndpointEnabledByDefaultTrue { static class FooEndpointEnabledByDefaultTrue {
@ -193,6 +201,17 @@ public class ConditionalOnEnabledEndpointTests {
} }
@Configuration
@ConditionalOnEnabledEndpoint(endpoint = FooEndpointEnabledByDefaultTrue.class)
static class FooEndpointEnabledByDefaultTrueOnConfigurationConfiguration {
@Bean
public FooEndpointEnabledByDefaultTrue foo() {
return new FooEndpointEnabledByDefaultTrue();
}
}
@Configuration @Configuration
static class FooEndpointEnabledByDefaultFalseConfiguration { static class FooEndpointEnabledByDefaultFalseConfiguration {

Loading…
Cancel
Save