Skip to content

Commit 97cea7f

Browse files
committed
BeanDefinition interface exposes initMethodName and destroyMethodName
Also includes setters for role and description. Issue: SPR-17275
1 parent 77887ef commit 97cea7f

File tree

4 files changed

+76
-35
lines changed

4 files changed

+76
-35
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/config/BeanDefinition.java

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -242,28 +242,42 @@ default boolean hasPropertyValues() {
242242
return !getPropertyValues().isEmpty();
243243
}
244244

245+
/**
246+
* Set the name of the initializer method.
247+
* @since 5.1
248+
*/
249+
void setInitMethodName(@Nullable String initMethodName);
245250

246-
// Read-only attributes
251+
/**
252+
* Return the name of the initializer method.
253+
* @since 5.1
254+
*/
255+
@Nullable
256+
String getInitMethodName();
247257

248258
/**
249-
* Return whether this a <b>Singleton</b>, with a single, shared instance
250-
* returned on all calls.
251-
* @see #SCOPE_SINGLETON
259+
* Set the name of the destroy method.
260+
* @since 5.1
252261
*/
253-
boolean isSingleton();
262+
void setDestroyMethodName(@Nullable String destroyMethodName);
254263

255264
/**
256-
* Return whether this a <b>Prototype</b>, with an independent instance
257-
* returned for each call.
258-
* @since 3.0
259-
* @see #SCOPE_PROTOTYPE
265+
* Return the name of the destroy method.
266+
* @since 5.1
260267
*/
261-
boolean isPrototype();
268+
@Nullable
269+
String getDestroyMethodName();
262270

263271
/**
264-
* Return whether this bean is "abstract", that is, not meant to be instantiated.
272+
* Set the role hint for this {@code BeanDefinition}. The role hint
273+
* provides the frameworks as well as tools with an indication of
274+
* the role and importance of a particular {@code BeanDefinition}.
275+
* @since 5.1
276+
* @see #ROLE_APPLICATION
277+
* @see #ROLE_SUPPORT
278+
* @see #ROLE_INFRASTRUCTURE
265279
*/
266-
boolean isAbstract();
280+
void setRole(int role);
267281

268282
/**
269283
* Get the role hint for this {@code BeanDefinition}. The role hint
@@ -275,12 +289,41 @@ default boolean hasPropertyValues() {
275289
*/
276290
int getRole();
277291

292+
/**
293+
* Set a human-readable description of this bean definition.
294+
* @since 5.1
295+
*/
296+
void setDescription(@Nullable String description);
297+
278298
/**
279299
* Return a human-readable description of this bean definition.
280300
*/
281301
@Nullable
282302
String getDescription();
283303

304+
305+
// Read-only attributes
306+
307+
/**
308+
* Return whether this a <b>Singleton</b>, with a single, shared instance
309+
* returned on all calls.
310+
* @see #SCOPE_SINGLETON
311+
*/
312+
boolean isSingleton();
313+
314+
/**
315+
* Return whether this a <b>Prototype</b>, with an independent instance
316+
* returned for each call.
317+
* @since 3.0
318+
* @see #SCOPE_PROTOTYPE
319+
*/
320+
boolean isPrototype();
321+
322+
/**
323+
* Return whether this bean is "abstract", that is, not meant to be instantiated.
324+
*/
325+
boolean isAbstract();
326+
284327
/**
285328
* Return a description of the resource that this bean definition
286329
* came from (for the purpose of showing context in case of errors).

spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinition.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,13 +878,15 @@ public boolean hasMethodOverrides() {
878878
* Set the name of the initializer method.
879879
* <p>The default is {@code null} in which case there is no initializer method.
880880
*/
881+
@Override
881882
public void setInitMethodName(@Nullable String initMethodName) {
882883
this.initMethodName = initMethodName;
883884
}
884885

885886
/**
886887
* Return the name of the initializer method.
887888
*/
889+
@Override
888890
@Nullable
889891
public String getInitMethodName() {
890892
return this.initMethodName;
@@ -911,13 +913,15 @@ public boolean isEnforceInitMethod() {
911913
* Set the name of the destroy method.
912914
* <p>The default is {@code null} in which case there is no destroy method.
913915
*/
916+
@Override
914917
public void setDestroyMethodName(@Nullable String destroyMethodName) {
915918
this.destroyMethodName = destroyMethodName;
916919
}
917920

918921
/**
919922
* Return the name of the destroy method.
920923
*/
924+
@Override
921925
@Nullable
922926
public String getDestroyMethodName() {
923927
return this.destroyMethodName;
@@ -960,6 +964,7 @@ public boolean isSynthetic() {
960964
/**
961965
* Set the role hint for this {@code BeanDefinition}.
962966
*/
967+
@Override
963968
public void setRole(int role) {
964969
this.role = role;
965970
}
@@ -975,6 +980,7 @@ public int getRole() {
975980
/**
976981
* Set a human-readable description of this bean definition.
977982
*/
983+
@Override
978984
public void setDescription(@Nullable String description) {
979985
this.description = description;
980986
}

spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigUtils.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
2626
import org.springframework.beans.factory.config.BeanDefinition;
2727
import org.springframework.beans.factory.config.BeanDefinitionHolder;
28-
import org.springframework.beans.factory.support.AbstractBeanDefinition;
2928
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
3029
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
3130
import org.springframework.beans.factory.support.RootBeanDefinition;
@@ -255,16 +254,13 @@ else if (abd.getMetadata() != metadata) {
255254
abd.setDependsOn(dependsOn.getStringArray("value"));
256255
}
257256

258-
if (abd instanceof AbstractBeanDefinition) {
259-
AbstractBeanDefinition absBd = (AbstractBeanDefinition) abd;
260-
AnnotationAttributes role = attributesFor(metadata, Role.class);
261-
if (role != null) {
262-
absBd.setRole(role.getNumber("value").intValue());
263-
}
264-
AnnotationAttributes description = attributesFor(metadata, Description.class);
265-
if (description != null) {
266-
absBd.setDescription(description.getString("value"));
267-
}
257+
AnnotationAttributes role = attributesFor(metadata, Role.class);
258+
if (role != null) {
259+
abd.setRole(role.getNumber("value").intValue());
260+
}
261+
AnnotationAttributes description = attributesFor(metadata, Description.class);
262+
if (description != null) {
263+
abd.setDescription(description.getString("value"));
268264
}
269265
}
270266

spring-context/src/main/java/org/springframework/scripting/support/ScriptFactoryPostProcessor.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import org.springframework.beans.factory.config.BeanDefinition;
4242
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
4343
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
44-
import org.springframework.beans.factory.support.AbstractBeanDefinition;
4544
import org.springframework.beans.factory.support.BeanDefinitionValidationException;
4645
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
4746
import org.springframework.beans.factory.support.GenericBeanDefinition;
@@ -516,16 +515,13 @@ protected Class<?> createConfigInterface(BeanDefinition bd, @Nullable Class<?>[]
516515
Signature signature = new Signature(setterName, Type.VOID_TYPE, new Type[] {Type.getType(propertyType)});
517516
maker.add(signature, new Type[0]);
518517
}
519-
if (bd instanceof AbstractBeanDefinition) {
520-
AbstractBeanDefinition abd = (AbstractBeanDefinition) bd;
521-
if (abd.getInitMethodName() != null) {
522-
Signature signature = new Signature(abd.getInitMethodName(), Type.VOID_TYPE, new Type[0]);
523-
maker.add(signature, new Type[0]);
524-
}
525-
if (StringUtils.hasText(abd.getDestroyMethodName())) {
526-
Signature signature = new Signature(abd.getDestroyMethodName(), Type.VOID_TYPE, new Type[0]);
527-
maker.add(signature, new Type[0]);
528-
}
518+
if (bd.getInitMethodName() != null) {
519+
Signature signature = new Signature(bd.getInitMethodName(), Type.VOID_TYPE, new Type[0]);
520+
maker.add(signature, new Type[0]);
521+
}
522+
if (StringUtils.hasText(bd.getDestroyMethodName())) {
523+
Signature signature = new Signature(bd.getDestroyMethodName(), Type.VOID_TYPE, new Type[0]);
524+
maker.add(signature, new Type[0]);
529525
}
530526
return maker.create();
531527
}

0 commit comments

Comments
 (0)