|
1 | 1 | /*
|
2 |
| - * Copyright 2024 the original author or authors. |
| 2 | + * Copyright 2024-2025 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
37 | 37 | import org.springframework.context.Lifecycle;
|
38 | 38 | import org.springframework.core.MethodIntrospector;
|
39 | 39 | import org.springframework.core.annotation.AnnotationUtils;
|
| 40 | +import org.springframework.core.annotation.MergedAnnotations; |
| 41 | +import org.springframework.core.annotation.RepeatableContainers; |
40 | 42 | import org.springframework.expression.Expression;
|
41 | 43 | import org.springframework.expression.ExpressionParser;
|
42 | 44 | import org.springframework.expression.spel.standard.SpelExpressionParser;
|
@@ -115,16 +117,17 @@ public Object postProcessAfterInitialization(Object bean, String beanName) throw
|
115 | 117 | * @param bean the bean for registration
|
116 | 118 | */
|
117 | 119 | public void registerControlBusCommands(String beanName, Object bean) {
|
118 |
| - Class<?> beanClass = bean.getClass(); |
119 |
| - if (bean instanceof Lifecycle || bean instanceof CustomizableThreadCreator |
120 |
| - || AnnotationUtils.findAnnotation(beanClass, ManagedResource.class) != null |
121 |
| - || AnnotationUtils.findAnnotation(beanClass, IntegrationManagedResource.class) != null) { |
122 |
| - |
123 |
| - ReflectionUtils.doWithMethods(beanClass, method -> populateExpressionForCommand(beanName, method), |
| 120 | + if (isBeanEligible(bean)) { |
| 121 | + ReflectionUtils.doWithMethods(bean.getClass(), method -> populateExpressionForCommand(beanName, method), |
124 | 122 | CONTROL_BUS_METHOD_FILTER);
|
125 | 123 | }
|
126 | 124 | }
|
127 | 125 |
|
| 126 | + @Override |
| 127 | + public boolean requiresDestruction(Object bean) { |
| 128 | + return isBeanEligible(bean); |
| 129 | + } |
| 130 | + |
128 | 131 | @Override
|
129 | 132 | public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
|
130 | 133 | this.controlBusCommands.remove(beanName);
|
@@ -221,6 +224,17 @@ private Expression buildExpressionForMethodToCall(CommandMethod commandMethod) {
|
221 | 224 | return buildExpressionForMethodToCall(commandMethod, methodForCommand.get());
|
222 | 225 | }
|
223 | 226 |
|
| 227 | + private static boolean isBeanEligible(Object bean) { |
| 228 | + MergedAnnotations mergedAnnotations = |
| 229 | + MergedAnnotations.from(bean.getClass(), MergedAnnotations.SearchStrategy.TYPE_HIERARCHY, |
| 230 | + RepeatableContainers.none()); |
| 231 | + |
| 232 | + return bean instanceof Lifecycle |
| 233 | + || bean instanceof CustomizableThreadCreator |
| 234 | + || mergedAnnotations.isPresent(ManagedResource.class) |
| 235 | + || mergedAnnotations.isPresent(IntegrationManagedResource.class); |
| 236 | + } |
| 237 | + |
224 | 238 | private static Expression buildExpressionForMethodToCall(CommandMethod commandMethod, Method methodForCommand) {
|
225 | 239 | Assert.isTrue(CONTROL_BUS_METHOD_FILTER.matches(methodForCommand),
|
226 | 240 | () -> "The method '%s' is not valid Control Bus command".formatted(methodForCommand));
|
|
0 commit comments