Consistent trace logging for init and destroy methods

Issue: SPR-17090
This commit is contained in:
Juergen Hoeller 2018-08-22 14:13:12 +02:00
parent 6ef0938a92
commit f13f041fae
2 changed files with 13 additions and 16 deletions

View File

@ -196,7 +196,6 @@ public class InitDestroyAnnotationBeanPostProcessor
} }
private LifecycleMetadata buildLifecycleMetadata(final Class<?> clazz) { private LifecycleMetadata buildLifecycleMetadata(final Class<?> clazz) {
final boolean debug = logger.isDebugEnabled();
List<LifecycleElement> initMethods = new ArrayList<>(); List<LifecycleElement> initMethods = new ArrayList<>();
List<LifecycleElement> destroyMethods = new ArrayList<>(); List<LifecycleElement> destroyMethods = new ArrayList<>();
Class<?> targetClass = clazz; Class<?> targetClass = clazz;
@ -209,14 +208,14 @@ public class InitDestroyAnnotationBeanPostProcessor
if (this.initAnnotationType != null && method.isAnnotationPresent(this.initAnnotationType)) { if (this.initAnnotationType != null && method.isAnnotationPresent(this.initAnnotationType)) {
LifecycleElement element = new LifecycleElement(method); LifecycleElement element = new LifecycleElement(method);
currInitMethods.add(element); currInitMethods.add(element);
if (debug) { if (logger.isTraceEnabled()) {
logger.debug("Found init method on class [" + clazz.getName() + "]: " + method); logger.trace("Found init method on class [" + clazz.getName() + "]: " + method);
} }
} }
if (this.destroyAnnotationType != null && method.isAnnotationPresent(this.destroyAnnotationType)) { if (this.destroyAnnotationType != null && method.isAnnotationPresent(this.destroyAnnotationType)) {
currDestroyMethods.add(new LifecycleElement(method)); currDestroyMethods.add(new LifecycleElement(method));
if (debug) { if (logger.isTraceEnabled()) {
logger.debug("Found destroy method on class [" + clazz.getName() + "]: " + method); logger.trace("Found destroy method on class [" + clazz.getName() + "]: " + method);
} }
} }
}); });
@ -276,8 +275,8 @@ public class InitDestroyAnnotationBeanPostProcessor
if (!beanDefinition.isExternallyManagedInitMethod(methodIdentifier)) { if (!beanDefinition.isExternallyManagedInitMethod(methodIdentifier)) {
beanDefinition.registerExternallyManagedInitMethod(methodIdentifier); beanDefinition.registerExternallyManagedInitMethod(methodIdentifier);
checkedInitMethods.add(element); checkedInitMethods.add(element);
if (logger.isDebugEnabled()) { if (logger.isTraceEnabled()) {
logger.debug("Registered init method on class [" + this.targetClass.getName() + "]: " + element); logger.trace("Registered init method on class [" + this.targetClass.getName() + "]: " + element);
} }
} }
} }
@ -287,8 +286,8 @@ public class InitDestroyAnnotationBeanPostProcessor
if (!beanDefinition.isExternallyManagedDestroyMethod(methodIdentifier)) { if (!beanDefinition.isExternallyManagedDestroyMethod(methodIdentifier)) {
beanDefinition.registerExternallyManagedDestroyMethod(methodIdentifier); beanDefinition.registerExternallyManagedDestroyMethod(methodIdentifier);
checkedDestroyMethods.add(element); checkedDestroyMethods.add(element);
if (logger.isDebugEnabled()) { if (logger.isTraceEnabled()) {
logger.debug("Registered destroy method on class [" + this.targetClass.getName() + "]: " + element); logger.trace("Registered destroy method on class [" + this.targetClass.getName() + "]: " + element);
} }
} }
} }
@ -301,10 +300,9 @@ public class InitDestroyAnnotationBeanPostProcessor
Collection<LifecycleElement> initMethodsToIterate = Collection<LifecycleElement> initMethodsToIterate =
(checkedInitMethods != null ? checkedInitMethods : this.initMethods); (checkedInitMethods != null ? checkedInitMethods : this.initMethods);
if (!initMethodsToIterate.isEmpty()) { if (!initMethodsToIterate.isEmpty()) {
boolean debug = logger.isDebugEnabled();
for (LifecycleElement element : initMethodsToIterate) { for (LifecycleElement element : initMethodsToIterate) {
if (debug) { if (logger.isTraceEnabled()) {
logger.debug("Invoking init method on bean '" + beanName + "': " + element.getMethod()); logger.trace("Invoking init method on bean '" + beanName + "': " + element.getMethod());
} }
element.invoke(target); element.invoke(target);
} }
@ -316,10 +314,9 @@ public class InitDestroyAnnotationBeanPostProcessor
Collection<LifecycleElement> destroyMethodsToUse = Collection<LifecycleElement> destroyMethodsToUse =
(checkedDestroyMethods != null ? checkedDestroyMethods : this.destroyMethods); (checkedDestroyMethods != null ? checkedDestroyMethods : this.destroyMethods);
if (!destroyMethodsToUse.isEmpty()) { if (!destroyMethodsToUse.isEmpty()) {
boolean debug = logger.isDebugEnabled();
for (LifecycleElement element : destroyMethodsToUse) { for (LifecycleElement element : destroyMethodsToUse) {
if (debug) { if (logger.isTraceEnabled()) {
logger.debug("Invoking destroy method on bean '" + beanName + "': " + element.getMethod()); logger.trace("Invoking destroy method on bean '" + beanName + "': " + element.getMethod());
} }
element.invoke(target); element.invoke(target);
} }

View File

@ -1831,7 +1831,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
if (initMethod == null) { if (initMethod == null) {
if (mbd.isEnforceInitMethod()) { if (mbd.isEnforceInitMethod()) {
throw new BeanDefinitionValidationException("Couldn't find an init method named '" + throw new BeanDefinitionValidationException("Could not find an init method named '" +
initMethodName + "' on bean with name '" + beanName + "'"); initMethodName + "' on bean with name '" + beanName + "'");
} }
else { else {