提交 97cea7f3 编写于 作者: J Juergen Hoeller

BeanDefinition interface exposes initMethodName and destroyMethodName

Also includes setters for role and description.

Issue: SPR-17275
上级 77887ef7
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -242,28 +242,42 @@ public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement { ...@@ -242,28 +242,42 @@ public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {
return !getPropertyValues().isEmpty(); return !getPropertyValues().isEmpty();
} }
/**
* Set the name of the initializer method.
* @since 5.1
*/
void setInitMethodName(@Nullable String initMethodName);
// Read-only attributes /**
* Return the name of the initializer method.
* @since 5.1
*/
@Nullable
String getInitMethodName();
/** /**
* Return whether this a <b>Singleton</b>, with a single, shared instance * Set the name of the destroy method.
* returned on all calls. * @since 5.1
* @see #SCOPE_SINGLETON
*/ */
boolean isSingleton(); void setDestroyMethodName(@Nullable String destroyMethodName);
/** /**
* Return whether this a <b>Prototype</b>, with an independent instance * Return the name of the destroy method.
* returned for each call. * @since 5.1
* @since 3.0
* @see #SCOPE_PROTOTYPE
*/ */
boolean isPrototype(); @Nullable
String getDestroyMethodName();
/** /**
* Return whether this bean is "abstract", that is, not meant to be instantiated. * 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
*/ */
boolean isAbstract(); void setRole(int role);
/** /**
* Get the role hint for this {@code BeanDefinition}. The role hint * Get the role hint for this {@code BeanDefinition}. The role hint
...@@ -275,12 +289,41 @@ public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement { ...@@ -275,12 +289,41 @@ public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {
*/ */
int getRole(); 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. * Return a human-readable description of this bean definition.
*/ */
@Nullable @Nullable
String getDescription(); String getDescription();
// Read-only attributes
/**
* Return whether this a <b>Singleton</b>, with a single, shared instance
* returned on all calls.
* @see #SCOPE_SINGLETON
*/
boolean isSingleton();
/**
* Return whether this a <b>Prototype</b>, with an independent instance
* returned for each call.
* @since 3.0
* @see #SCOPE_PROTOTYPE
*/
boolean isPrototype();
/**
* Return whether this bean is "abstract", that is, not meant to be instantiated.
*/
boolean isAbstract();
/** /**
* Return a description of the resource that this bean definition * Return a description of the resource that this bean definition
* came from (for the purpose of showing context in case of errors). * came from (for the purpose of showing context in case of errors).
......
...@@ -878,6 +878,7 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess ...@@ -878,6 +878,7 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
* Set the name of the initializer method. * Set the name of the initializer method.
* <p>The default is {@code null} in which case there is no initializer method. * <p>The default is {@code null} in which case there is no initializer method.
*/ */
@Override
public void setInitMethodName(@Nullable String initMethodName) { public void setInitMethodName(@Nullable String initMethodName) {
this.initMethodName = initMethodName; this.initMethodName = initMethodName;
} }
...@@ -885,6 +886,7 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess ...@@ -885,6 +886,7 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
/** /**
* Return the name of the initializer method. * Return the name of the initializer method.
*/ */
@Override
@Nullable @Nullable
public String getInitMethodName() { public String getInitMethodName() {
return this.initMethodName; return this.initMethodName;
...@@ -911,6 +913,7 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess ...@@ -911,6 +913,7 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
* Set the name of the destroy method. * Set the name of the destroy method.
* <p>The default is {@code null} in which case there is no destroy method. * <p>The default is {@code null} in which case there is no destroy method.
*/ */
@Override
public void setDestroyMethodName(@Nullable String destroyMethodName) { public void setDestroyMethodName(@Nullable String destroyMethodName) {
this.destroyMethodName = destroyMethodName; this.destroyMethodName = destroyMethodName;
} }
...@@ -918,6 +921,7 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess ...@@ -918,6 +921,7 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
/** /**
* Return the name of the destroy method. * Return the name of the destroy method.
*/ */
@Override
@Nullable @Nullable
public String getDestroyMethodName() { public String getDestroyMethodName() {
return this.destroyMethodName; return this.destroyMethodName;
...@@ -960,6 +964,7 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess ...@@ -960,6 +964,7 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
/** /**
* Set the role hint for this {@code BeanDefinition}. * Set the role hint for this {@code BeanDefinition}.
*/ */
@Override
public void setRole(int role) { public void setRole(int role) {
this.role = role; this.role = role;
} }
...@@ -975,6 +980,7 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess ...@@ -975,6 +980,7 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
/** /**
* Set a human-readable description of this bean definition. * Set a human-readable description of this bean definition.
*/ */
@Override
public void setDescription(@Nullable String description) { public void setDescription(@Nullable String description) {
this.description = description; this.description = description;
} }
......
...@@ -25,7 +25,6 @@ import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; ...@@ -25,7 +25,6 @@ import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor; import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder; 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.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.support.RootBeanDefinition;
...@@ -255,16 +254,13 @@ public abstract class AnnotationConfigUtils { ...@@ -255,16 +254,13 @@ public abstract class AnnotationConfigUtils {
abd.setDependsOn(dependsOn.getStringArray("value")); abd.setDependsOn(dependsOn.getStringArray("value"));
} }
if (abd instanceof AbstractBeanDefinition) { AnnotationAttributes role = attributesFor(metadata, Role.class);
AbstractBeanDefinition absBd = (AbstractBeanDefinition) abd; if (role != null) {
AnnotationAttributes role = attributesFor(metadata, Role.class); abd.setRole(role.getNumber("value").intValue());
if (role != null) { }
absBd.setRole(role.getNumber("value").intValue()); AnnotationAttributes description = attributesFor(metadata, Description.class);
} if (description != null) {
AnnotationAttributes description = attributesFor(metadata, Description.class); abd.setDescription(description.getString("value"));
if (description != null) {
absBd.setDescription(description.getString("value"));
}
} }
} }
......
...@@ -41,7 +41,6 @@ import org.springframework.beans.factory.FactoryBean; ...@@ -41,7 +41,6 @@ import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter; 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.BeanDefinitionValidationException;
import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition; import org.springframework.beans.factory.support.GenericBeanDefinition;
...@@ -516,16 +515,13 @@ public class ScriptFactoryPostProcessor extends InstantiationAwareBeanPostProces ...@@ -516,16 +515,13 @@ public class ScriptFactoryPostProcessor extends InstantiationAwareBeanPostProces
Signature signature = new Signature(setterName, Type.VOID_TYPE, new Type[] {Type.getType(propertyType)}); Signature signature = new Signature(setterName, Type.VOID_TYPE, new Type[] {Type.getType(propertyType)});
maker.add(signature, new Type[0]); maker.add(signature, new Type[0]);
} }
if (bd instanceof AbstractBeanDefinition) { if (bd.getInitMethodName() != null) {
AbstractBeanDefinition abd = (AbstractBeanDefinition) bd; Signature signature = new Signature(bd.getInitMethodName(), Type.VOID_TYPE, new Type[0]);
if (abd.getInitMethodName() != null) { maker.add(signature, new Type[0]);
Signature signature = new Signature(abd.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]);
if (StringUtils.hasText(abd.getDestroyMethodName())) { maker.add(signature, new Type[0]);
Signature signature = new Signature(abd.getDestroyMethodName(), Type.VOID_TYPE, new Type[0]);
maker.add(signature, new Type[0]);
}
} }
return maker.create(); return maker.create();
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册