From 97cea7f36e264c2b193ae71609a44b6bd34e31b7 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 13 Sep 2018 20:24:36 +0200 Subject: [PATCH] BeanDefinition interface exposes initMethodName and destroyMethodName Also includes setters for role and description. Issue: SPR-17275 --- .../beans/factory/config/BeanDefinition.java | 77 +++++++++++++++---- .../support/AbstractBeanDefinition.java | 6 ++ .../annotation/AnnotationConfigUtils.java | 18 ++--- .../support/ScriptFactoryPostProcessor.java | 18 ++--- 4 files changed, 80 insertions(+), 39 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/config/BeanDefinition.java b/spring-beans/src/main/java/org/springframework/beans/factory/config/BeanDefinition.java index e798b8a399f..cf07e65c02e 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/config/BeanDefinition.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/config/BeanDefinition.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -242,6 +242,65 @@ public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement { return !getPropertyValues().isEmpty(); } + /** + * Set the name of the initializer method. + * @since 5.1 + */ + void setInitMethodName(@Nullable String initMethodName); + + /** + * Return the name of the initializer method. + * @since 5.1 + */ + @Nullable + String getInitMethodName(); + + /** + * Set the name of the destroy method. + * @since 5.1 + */ + void setDestroyMethodName(@Nullable String destroyMethodName); + + /** + * Return the name of the destroy method. + * @since 5.1 + */ + @Nullable + String getDestroyMethodName(); + + /** + * Set the role hint for this {@code BeanDefinition}. The role hint + * provides the frameworks as well as tools with an indication of + * the role and importance of a particular {@code BeanDefinition}. + * @since 5.1 + * @see #ROLE_APPLICATION + * @see #ROLE_SUPPORT + * @see #ROLE_INFRASTRUCTURE + */ + void setRole(int role); + + /** + * Get the role hint for this {@code BeanDefinition}. The role hint + * provides the frameworks as well as tools with an indication of + * the role and importance of a particular {@code BeanDefinition}. + * @see #ROLE_APPLICATION + * @see #ROLE_SUPPORT + * @see #ROLE_INFRASTRUCTURE + */ + int getRole(); + + /** + * Set a human-readable description of this bean definition. + * @since 5.1 + */ + void setDescription(@Nullable String description); + + /** + * Return a human-readable description of this bean definition. + */ + @Nullable + String getDescription(); + // Read-only attributes @@ -265,22 +324,6 @@ public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement { */ boolean isAbstract(); - /** - * Get the role hint for this {@code BeanDefinition}. The role hint - * provides the frameworks as well as tools with an indication of - * the role and importance of a particular {@code BeanDefinition}. - * @see #ROLE_APPLICATION - * @see #ROLE_SUPPORT - * @see #ROLE_INFRASTRUCTURE - */ - int getRole(); - - /** - * Return a human-readable description of this bean definition. - */ - @Nullable - String getDescription(); - /** * Return a description of the resource that this bean definition * came from (for the purpose of showing context in case of errors). diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinition.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinition.java index f0c463b9120..84b748b0b0b 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinition.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinition.java @@ -878,6 +878,7 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess * Set the name of the initializer method. *

The default is {@code null} in which case there is no initializer method. */ + @Override public void setInitMethodName(@Nullable String initMethodName) { this.initMethodName = initMethodName; } @@ -885,6 +886,7 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess /** * Return the name of the initializer method. */ + @Override @Nullable public String getInitMethodName() { return this.initMethodName; @@ -911,6 +913,7 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess * Set the name of the destroy method. *

The default is {@code null} in which case there is no destroy method. */ + @Override public void setDestroyMethodName(@Nullable String destroyMethodName) { this.destroyMethodName = destroyMethodName; } @@ -918,6 +921,7 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess /** * Return the name of the destroy method. */ + @Override @Nullable public String getDestroyMethodName() { return this.destroyMethodName; @@ -960,6 +964,7 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess /** * Set the role hint for this {@code BeanDefinition}. */ + @Override public void setRole(int role) { this.role = role; } @@ -975,6 +980,7 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess /** * Set a human-readable description of this bean definition. */ + @Override public void setDescription(@Nullable String description) { this.description = description; } diff --git a/spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigUtils.java b/spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigUtils.java index 72a4d8a1b1e..9741a30ab55 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigUtils.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigUtils.java @@ -25,7 +25,6 @@ import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinitionHolder; -import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.RootBeanDefinition; @@ -255,16 +254,13 @@ public abstract class AnnotationConfigUtils { abd.setDependsOn(dependsOn.getStringArray("value")); } - if (abd instanceof AbstractBeanDefinition) { - AbstractBeanDefinition absBd = (AbstractBeanDefinition) abd; - AnnotationAttributes role = attributesFor(metadata, Role.class); - if (role != null) { - absBd.setRole(role.getNumber("value").intValue()); - } - AnnotationAttributes description = attributesFor(metadata, Description.class); - if (description != null) { - absBd.setDescription(description.getString("value")); - } + AnnotationAttributes role = attributesFor(metadata, Role.class); + if (role != null) { + abd.setRole(role.getNumber("value").intValue()); + } + AnnotationAttributes description = attributesFor(metadata, Description.class); + if (description != null) { + abd.setDescription(description.getString("value")); } } diff --git a/spring-context/src/main/java/org/springframework/scripting/support/ScriptFactoryPostProcessor.java b/spring-context/src/main/java/org/springframework/scripting/support/ScriptFactoryPostProcessor.java index 356eddca46b..f6b4a5cd28f 100644 --- a/spring-context/src/main/java/org/springframework/scripting/support/ScriptFactoryPostProcessor.java +++ b/spring-context/src/main/java/org/springframework/scripting/support/ScriptFactoryPostProcessor.java @@ -41,7 +41,6 @@ import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter; -import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionValidationException; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.GenericBeanDefinition; @@ -516,16 +515,13 @@ public class ScriptFactoryPostProcessor extends InstantiationAwareBeanPostProces Signature signature = new Signature(setterName, Type.VOID_TYPE, new Type[] {Type.getType(propertyType)}); maker.add(signature, new Type[0]); } - if (bd instanceof AbstractBeanDefinition) { - AbstractBeanDefinition abd = (AbstractBeanDefinition) bd; - if (abd.getInitMethodName() != null) { - Signature signature = new Signature(abd.getInitMethodName(), Type.VOID_TYPE, new Type[0]); - maker.add(signature, new Type[0]); - } - if (StringUtils.hasText(abd.getDestroyMethodName())) { - Signature signature = new Signature(abd.getDestroyMethodName(), Type.VOID_TYPE, new Type[0]); - maker.add(signature, new Type[0]); - } + if (bd.getInitMethodName() != null) { + Signature signature = new Signature(bd.getInitMethodName(), Type.VOID_TYPE, new Type[0]); + maker.add(signature, new Type[0]); + } + if (StringUtils.hasText(bd.getDestroyMethodName())) { + Signature signature = new Signature(bd.getDestroyMethodName(), Type.VOID_TYPE, new Type[0]); + maker.add(signature, new Type[0]); } return maker.create(); }