|
2 | 2 | *
|
3 | 3 | * *
|
4 | 4 | * * *
|
5 |
| - * * * * Copyright 2019-2020 the original author or authors. |
| 5 | + * * * * Copyright 2019-2022 the original author or authors. |
6 | 6 | * * * *
|
7 | 7 | * * * * Licensed under the Apache License, Version 2.0 (the "License");
|
8 | 8 | * * * * you may not use this file except in compliance with the License.
|
|
23 | 23 |
|
24 | 24 | package org.springdoc.core;
|
25 | 25 |
|
26 |
| -import java.util.ArrayList; |
27 |
| -import java.util.List; |
28 |
| - |
29 | 26 | import org.springdoc.core.customizers.ActuatorOpenApiCustomizer;
|
30 | 27 | import org.springdoc.core.customizers.ActuatorOperationCustomizer;
|
31 |
| - |
| 28 | +import org.springframework.beans.BeansException; |
32 | 29 | import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
| 30 | +import org.springframework.beans.factory.config.RuntimeBeanReference; |
| 31 | +import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
| 32 | +import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
| 33 | +import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; |
33 | 34 | import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
|
34 | 35 | import org.springframework.boot.context.properties.bind.BindResult;
|
35 | 36 | import org.springframework.boot.context.properties.bind.Binder;
|
36 |
| -import org.springframework.util.CollectionUtils; |
| 37 | +import org.springframework.context.ApplicationContext; |
| 38 | +import org.springframework.context.ApplicationContextAware; |
| 39 | +import org.springframework.util.ObjectUtils; |
37 | 40 |
|
38 | 41 | import static org.springdoc.core.Constants.ACTUATOR_DEFAULT_GROUP;
|
39 | 42 | import static org.springdoc.core.Constants.ALL_PATTERN;
|
|
44 | 47 | /**
|
45 | 48 | * The type Springdoc bean factory configurer.
|
46 | 49 | * @author bnasslahsen
|
| 50 | + * @author christophejan |
47 | 51 | */
|
48 |
| -public class SpringdocActuatorBeanFactoryConfigurer extends SpringdocBeanFactoryConfigurer{ |
| 52 | +public class SpringdocActuatorBeanFactoryConfigurer implements ApplicationContextAware, BeanDefinitionRegistryPostProcessor { |
49 | 53 |
|
50 | 54 | /**
|
51 |
| - * The Grouped open apis. |
| 55 | + * The ApplicationContext. |
52 | 56 | */
|
53 |
| - private List<GroupedOpenApi> groupedOpenApis; |
| 57 | + protected ApplicationContext applicationContext; |
54 | 58 |
|
55 |
| - /** |
56 |
| - * Instantiates a new Springdoc actuator bean factory configurer. |
57 |
| - * |
58 |
| - * @param groupedOpenApis the grouped open apis |
59 |
| - */ |
60 |
| - public SpringdocActuatorBeanFactoryConfigurer(List<GroupedOpenApi> groupedOpenApis) { |
61 |
| - this.groupedOpenApis = groupedOpenApis; |
| 59 | + @Override |
| 60 | + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| 61 | + this.applicationContext = applicationContext; |
62 | 62 | }
|
63 | 63 |
|
64 | 64 | @Override
|
65 |
| - public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { |
66 |
| - final BindResult<WebEndpointProperties> result = Binder.get(environment) |
| 65 | + public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) { |
| 66 | + final BindResult<WebEndpointProperties> result = Binder.get(applicationContext.getEnvironment()) |
67 | 67 | .bind(MANAGEMENT_ENDPOINTS_WEB, WebEndpointProperties.class);
|
68 | 68 | if (result.isBound()) {
|
69 | 69 | WebEndpointProperties webEndpointProperties = result.get();
|
70 | 70 |
|
71 |
| - List<GroupedOpenApi> newGroups = new ArrayList<>(); |
72 |
| - |
73 |
| - ActuatorOpenApiCustomizer actuatorOpenApiCustomiser = new ActuatorOpenApiCustomizer(webEndpointProperties); |
74 |
| - beanFactory.registerSingleton("actuatorOpenApiCustomiser", actuatorOpenApiCustomiser); |
75 |
| - ActuatorOperationCustomizer actuatorCustomizer = new ActuatorOperationCustomizer(); |
76 |
| - beanFactory.registerSingleton("actuatorCustomizer", actuatorCustomizer); |
77 |
| - |
78 |
| - GroupedOpenApi actuatorGroup = GroupedOpenApi.builder().group(ACTUATOR_DEFAULT_GROUP) |
79 |
| - .pathsToMatch(webEndpointProperties.getBasePath() + ALL_PATTERN) |
80 |
| - .pathsToExclude(webEndpointProperties.getBasePath() + HEALTH_PATTERN) |
81 |
| - .addOperationCustomizer(actuatorCustomizer) |
82 |
| - .addOpenApiCustomiser(actuatorOpenApiCustomiser) |
83 |
| - .build(); |
84 |
| - // Add the actuator group |
85 |
| - newGroups.add(actuatorGroup); |
86 |
| - |
87 |
| - if (CollectionUtils.isEmpty(groupedOpenApis)) { |
88 |
| - GroupedOpenApi defaultGroup = GroupedOpenApi.builder().group(DEFAULT_GROUP_NAME) |
89 |
| - .pathsToMatch(ALL_PATTERN) |
90 |
| - .pathsToExclude(webEndpointProperties.getBasePath() + ALL_PATTERN) |
91 |
| - .build(); |
92 |
| - // Register the default group |
93 |
| - newGroups.add(defaultGroup); |
94 |
| - } |
| 71 | + boolean addDefaultGroup = ObjectUtils.isEmpty(applicationContext |
| 72 | + .getBeanNamesForType(GroupedOpenApi.class, true, false)); |
| 73 | + |
| 74 | + registry.registerBeanDefinition("actuatorOpenApiCustomiser", BeanDefinitionBuilder |
| 75 | + .genericBeanDefinition(ActuatorOpenApiCustomizer.class) |
| 76 | + .addConstructorArgValue(webEndpointProperties) |
| 77 | + .getBeanDefinition()); |
| 78 | + |
| 79 | + registry.registerBeanDefinition("actuatorCustomizer", BeanDefinitionBuilder |
| 80 | + .genericBeanDefinition(ActuatorOperationCustomizer.class) |
| 81 | + .getBeanDefinition()); |
95 | 82 |
|
96 |
| - newGroups.forEach(elt -> beanFactory.registerSingleton(elt.getGroup(), elt)); |
| 83 | + // register the actuator group bean definition |
| 84 | + registry.registerBeanDefinition(ACTUATOR_DEFAULT_GROUP, BeanDefinitionBuilder |
| 85 | + .genericBeanDefinition(SpringdocActuatorBeanFactoryConfigurer.class) |
| 86 | + .setFactoryMethod("actuatorGroupFactoryMethod") |
| 87 | + .addConstructorArgValue(webEndpointProperties.getBasePath()) |
| 88 | + .addConstructorArgValue(new RuntimeBeanReference(ActuatorOpenApiCustomizer.class)) |
| 89 | + .addConstructorArgValue(new RuntimeBeanReference(ActuatorOperationCustomizer.class)) |
| 90 | + .getBeanDefinition()); |
| 91 | + |
| 92 | + if (addDefaultGroup) { |
| 93 | + // register the default group bean definition |
| 94 | + registry.registerBeanDefinition(DEFAULT_GROUP_NAME, BeanDefinitionBuilder |
| 95 | + .genericBeanDefinition(SpringdocActuatorBeanFactoryConfigurer.class) |
| 96 | + .setFactoryMethod("defaultGroupFactoryMethod") |
| 97 | + .addConstructorArgValue(webEndpointProperties.getBasePath()) |
| 98 | + .getBeanDefinition()); |
| 99 | + } |
97 | 100 | }
|
98 |
| - initBeanFactoryPostProcessor(beanFactory); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { |
| 105 | + SpringdocBeanFactoryConfigurer.initBeanFactoryPostProcessor(beanFactory); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Actuator {@link GroupedOpenApi} factory method. |
| 110 | + * |
| 111 | + * @param actuatorBasePath the actuator base path |
| 112 | + * @param actuatorOpenApiCustomiser the {@link ActuatorOpenApiCustomizer} |
| 113 | + * @param actuatorOperationCustomizer the {@link ActuatorOperationCustomizer} |
| 114 | + * |
| 115 | + * @return the actuator {@link GroupedOpenApi} |
| 116 | + */ |
| 117 | + public static GroupedOpenApi actuatorGroupFactoryMethod(String actuatorBasePath, |
| 118 | + ActuatorOpenApiCustomizer actuatorOpenApiCustomiser, ActuatorOperationCustomizer actuatorOperationCustomizer) { |
| 119 | + return GroupedOpenApi.builder().group(ACTUATOR_DEFAULT_GROUP) |
| 120 | + .pathsToMatch(actuatorBasePath + ALL_PATTERN) |
| 121 | + .pathsToExclude(actuatorBasePath + HEALTH_PATTERN) |
| 122 | + .addOpenApiCustomiser(actuatorOpenApiCustomiser) |
| 123 | + .addOperationCustomizer(actuatorOperationCustomizer) |
| 124 | + .build(); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Default {@link GroupedOpenApi} factory method. |
| 129 | + * |
| 130 | + * @param actuatorBasePath the actuator base path |
| 131 | + * |
| 132 | + * @return the default {@link GroupedOpenApi} |
| 133 | + */ |
| 134 | + public static GroupedOpenApi defaultGroupFactoryMethod(String actuatorBasePath) { |
| 135 | + return GroupedOpenApi.builder().group(DEFAULT_GROUP_NAME) |
| 136 | + .pathsToMatch(ALL_PATTERN) |
| 137 | + .pathsToExclude(actuatorBasePath + ALL_PATTERN) |
| 138 | + .build(); |
99 | 139 | }
|
100 | 140 |
|
101 | 141 | }
|
0 commit comments