Allow ConditionalOnEnabledEndpoint to be set at class level
See gh-15451
This commit is contained in:
		
							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");
 | 
			
		||||
 * you may not use this file except in compliance with the License.
 | 
			
		||||
| 
						 | 
				
			
			@ -92,7 +92,7 @@ import org.springframework.core.env.Environment;
 | 
			
		|||
 * @see Endpoint
 | 
			
		||||
 */
 | 
			
		||||
@Retention(RetentionPolicy.RUNTIME)
 | 
			
		||||
@Target(ElementType.METHOD)
 | 
			
		||||
@Target({ ElementType.METHOD, ElementType.TYPE })
 | 
			
		||||
@Documented
 | 
			
		||||
@Conditional(OnEnabledEndpointCondition.class)
 | 
			
		||||
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");
 | 
			
		||||
 * 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,
 | 
			
		||||
			AnnotatedTypeMetadata metadata) {
 | 
			
		||||
		Assert.state(
 | 
			
		||||
				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);
 | 
			
		||||
		return getEndpointAttributes(getEndpointType(context, metadata));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private Class<?> getEndpointType(ConditionContext context, MethodMetadata metadata) {
 | 
			
		||||
	private Class<?> getEndpointType(ConditionContext context,
 | 
			
		||||
			AnnotatedTypeMetadata metadata) {
 | 
			
		||||
		Map<String, Object> attributes = metadata
 | 
			
		||||
				.getAnnotationAttributes(ConditionalOnEnabledEndpoint.class.getName());
 | 
			
		||||
		if (attributes != null && attributes.containsKey("endpoint")) {
 | 
			
		||||
| 
						 | 
				
			
			@ -107,15 +103,19 @@ class OnEnabledEndpointCondition extends SpringBootCondition {
 | 
			
		|||
				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 {
 | 
			
		||||
			return ClassUtils.forName(metadata.getReturnTypeName(),
 | 
			
		||||
			return ClassUtils.forName(methodMetadata.getReturnTypeName(),
 | 
			
		||||
					context.getClassLoader());
 | 
			
		||||
		}
 | 
			
		||||
		catch (Throwable ex) {
 | 
			
		||||
			throw new IllegalStateException("Failed to extract endpoint id for "
 | 
			
		||||
					+ metadata.getDeclaringClassName() + "." + metadata.getMethodName(),
 | 
			
		||||
					ex);
 | 
			
		||||
					+ methodMetadata.getDeclaringClassName() + "."
 | 
			
		||||
					+ 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");
 | 
			
		||||
 * 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"));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@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)
 | 
			
		||||
	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
 | 
			
		||||
	static class FooEndpointEnabledByDefaultFalseConfiguration {
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue