Top Description Inners Fields Constructors Methods
org.springframework.beans.factory.annotation

public Class InitDestroyAnnotationBeanPostProcessor

extends Object
implements DestructionAwareBeanPostProcessor, MergedBeanDefinitionPostProcessor, BeanRegistrationAotProcessor, PriorityOrdered, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, org.springframework.core.PriorityOrdered, org.springframework.core.Ordered, org.springframework.beans.factory.aot.BeanRegistrationAotProcessor, org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor, org.springframework.beans.factory.config.BeanPostProcessor, org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor
Annotations
@SuppressWarnings:serial
Imports
java.io.IOException, .ObjectInputStream, .Serializable, java.lang.annotation.Annotation, java.lang.reflect.InvocationTargetException, .Method, .Modifier, java.util.ArrayList, .Collection, .Collections, .LinkedHashSet, .List, .Map, .Set, java.util.concurrent.ConcurrentHashMap, java.util.stream.Stream, org.apache.commons.logging.Log, .LogFactory, org.springframework.beans.BeansException, org.springframework.beans.factory.BeanCreationException, org.springframework.beans.factory.aot.BeanRegistrationAotContribution, .BeanRegistrationAotProcessor, org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor, org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor, .RegisteredBean, .RootBeanDefinition, org.springframework.core.Ordered, .PriorityOrdered, org.springframework.core.annotation.AnnotationUtils, org.springframework.lang.Nullable, org.springframework.util.ClassUtils, .CollectionUtils, .ReflectionUtils

org.springframework.beans.factory.config.BeanPostProcessor implementation that invokes annotated init and destroy methods. Allows for an annotation alternative to Spring's org.springframework.beans.factory.InitializingBean and org.springframework.beans.factory.DisposableBean callback interfaces.

The actual annotation types that this post-processor checks for can be configured through the "initAnnotationType" and "destroyAnnotationType" properties. Any custom annotation can be used, since there are no required annotation attributes.

Init and destroy annotations may be applied to methods of any visibility: public, package-protected, protected, or private. Multiple such methods may be annotated, but it is recommended to only annotate one single init method and destroy method, respectively.

Spring's org.springframework.context.annotation.CommonAnnotationBeanPostProcessor supports the jakarta.annotation.PostConstruct and jakarta.annotation.PreDestroy annotations out of the box, as init annotation and destroy annotation, respectively. Furthermore, it also supports the jakarta.annotation.Resource annotation for annotation-driven injection of named beans.

Authors
Juergen Hoeller, Stephane Nicoll, Phillip Webb, Sam Brannen
Since
2.5
See Also
setInitAnnotationType, setDestroyAnnotationType

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
InitDestroyAnnotationBeanPostProcessor.LifecycleMetadata

Class representing information about annotated init and destroy methods.

private static class
InitDestroyAnnotationBeanPostProcessor.LifecycleMethod

Class representing an annotated init or destroy method.

Field Summary

Modifier and TypeField and Description
private final Set<Class<? extends Annotation>>
private final transient InitDestroyAnnotationBeanPostProcessor.LifecycleMetadata
private final Set<Class<? extends Annotation>>
private final transient Map<Class<?>, InitDestroyAnnotationBeanPostProcessor.LifecycleMetadata>
protected transient Log
private int

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
addDestroyAnnotationType(Class<? extends Annotation> destroyAnnotationType)

Add a destroy annotation to check for, indicating destruction methods to call when the context is shutting down.

public void
addInitAnnotationType(Class<? extends Annotation> initAnnotationType)

Add an init annotation to check for, indicating initialization methods to call after configuration of a bean.

private InitDestroyAnnotationBeanPostProcessor.LifecycleMetadata
buildLifecycleMetadata(final Class<?> beanClass)

private InitDestroyAnnotationBeanPostProcessor.LifecycleMetadata
findLifecycleMetadata(RootBeanDefinition beanDefinition, Class<?> beanClass)

private InitDestroyAnnotationBeanPostProcessor.LifecycleMetadata
public int
getOrder()

Implements org.springframework.core.Ordered.getOrder.

Get the order value of this object.
public Object
postProcessAfterInitialization(Object
the new bean instance
bean
,
String
the name of the bean
beanName
)

Overrides default org.springframework.beans.factory.config.BeanPostProcessor.postProcessAfterInitialization.

Apply this BeanPostProcessor to the given new bean instance after any bean initialization callbacks (like InitializingBean's afterPropertiesSet or a custom init-method).
public void
postProcessBeforeDestruction(Object
the bean instance to be destroyed
bean
,
String
the name of the bean
beanName
)

Implements org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor.postProcessBeforeDestruction.

Apply this BeanPostProcessor to the given bean instance before its destruction, e.g. invoking custom destruction callbacks.
public Object
postProcessBeforeInitialization(Object
the new bean instance
bean
,
String
the name of the bean
beanName
)

Overrides default org.springframework.beans.factory.config.BeanPostProcessor.postProcessBeforeInitialization.

Apply this BeanPostProcessor to the given new bean instance before any bean initialization callbacks (like InitializingBean's afterPropertiesSet or a custom init-method).
public void
postProcessMergedBeanDefinition(RootBeanDefinition
the merged bean definition for the bean
beanDefinition
,
Class<?>
the actual type of the managed bean instance
beanClass
,
String
the name of the bean
beanName
)

Implements org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor.postProcessMergedBeanDefinition.

Post-process the given merged bean definition for the specified bean.
public BeanRegistrationAotContribution
processAheadOfTime(RegisteredBean
the registered bean to process
registeredBean
)

Implements org.springframework.beans.factory.aot.BeanRegistrationAotProcessor.processAheadOfTime.

Process the given RegisteredBean instance ahead-of-time and return a contribution or null.
private void
public boolean
requiresDestruction(Object
the bean instance to check
bean
)

Overrides default org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor.requiresDestruction.

Determine whether the given bean instance requires destruction by this post-processor.
private static String[]
public void
setDestroyAnnotationType(Class<? extends Annotation> destroyAnnotationType)

Specify the destroy annotation to check for, indicating destruction methods to call when the context is shutting down.

public void
setInitAnnotationType(Class<? extends Annotation> initAnnotationType)

Specify the init annotation to check for, indicating initialization methods to call after configuration of a bean.

public void
setOrder(int order)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

destroyAnnotationTypesback to summary
private final Set<Class<? extends Annotation>> destroyAnnotationTypes
emptyLifecycleMetadataback to summary
private final transient InitDestroyAnnotationBeanPostProcessor.LifecycleMetadata emptyLifecycleMetadata
initAnnotationTypesback to summary
private final Set<Class<? extends Annotation>> initAnnotationTypes
lifecycleMetadataCacheback to summary
private final transient Map<Class<?>, InitDestroyAnnotationBeanPostProcessor.LifecycleMetadata> lifecycleMetadataCache
Annotations
@Nullable
loggerback to summary
protected transient Log logger
orderback to summary
private int order

Constructor Detail

InitDestroyAnnotationBeanPostProcessorback to summary
public InitDestroyAnnotationBeanPostProcessor()

Method Detail

addDestroyAnnotationTypeback to summary
public void addDestroyAnnotationType(Class<? extends Annotation> destroyAnnotationType)

Add a destroy annotation to check for, indicating destruction methods to call when the context is shutting down.

Since
6.0.11
See Also
setDestroyAnnotationType
addInitAnnotationTypeback to summary
public void addInitAnnotationType(Class<? extends Annotation> initAnnotationType)

Add an init annotation to check for, indicating initialization methods to call after configuration of a bean.

Since
6.0.11
See Also
setInitAnnotationType
buildLifecycleMetadataback to summary
private InitDestroyAnnotationBeanPostProcessor.LifecycleMetadata buildLifecycleMetadata(final Class<?> beanClass)
findLifecycleMetadataback to summary
private InitDestroyAnnotationBeanPostProcessor.LifecycleMetadata findLifecycleMetadata(RootBeanDefinition beanDefinition, Class<?> beanClass)
findLifecycleMetadataback to summary
private InitDestroyAnnotationBeanPostProcessor.LifecycleMetadata findLifecycleMetadata(Class<?> beanClass)
getOrderback to summary
public int getOrder()

Implements org.springframework.core.Ordered.getOrder.

Doc from org.springframework.core.Ordered.getOrder.

Get the order value of this object.

Higher values are interpreted as lower priority. As a consequence, the object with the lowest value has the highest priority (somewhat analogous to Servlet load-on-startup values).

Same order values will result in arbitrary sort positions for the affected objects.

Returns:int

the order value

Annotations
@Override
postProcessAfterInitializationback to summary
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException

Overrides default org.springframework.beans.factory.config.BeanPostProcessor.postProcessAfterInitialization.

Doc from org.springframework.beans.factory.config.BeanPostProcessor.postProcessAfterInitialization.

Apply this BeanPostProcessor to the given new bean instance after any bean initialization callbacks (like InitializingBean's afterPropertiesSet or a custom init-method). The bean will already be populated with property values. The returned bean instance may be a wrapper around the original.

In case of a FactoryBean, this callback will be invoked for both the FactoryBean instance and the objects created by the FactoryBean (as of Spring 2.0). The post-processor can decide whether to apply to either the FactoryBean or created objects or both through corresponding bean instanceof FactoryBean checks.

This callback will also be invoked after a short-circuiting triggered by a InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation method, in contrast to all other BeanPostProcessor callbacks.

The default implementation returns the given bean as-is.

Parameters
bean:Object

the new bean instance

beanName:String

the name of the bean

Returns:Object

the bean instance to use, either the original or a wrapped one; if null, no subsequent BeanPostProcessors will be invoked

Annotations
@Override
Exceptions
BeansException:
in case of errors
postProcessBeforeDestructionback to summary
public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException

Implements org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor.postProcessBeforeDestruction.

Doc from org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor.postProcessBeforeDestruction.

Apply this BeanPostProcessor to the given bean instance before its destruction, e.g. invoking custom destruction callbacks.

Like DisposableBean's destroy and a custom destroy method, this callback will only apply to beans which the container fully manages the lifecycle for. This is usually the case for singletons and scoped beans.

Parameters
bean:Object

the bean instance to be destroyed

beanName:String

the name of the bean

Annotations
@Override
Exceptions
BeansException:
in case of errors
postProcessBeforeInitializationback to summary
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException

Overrides default org.springframework.beans.factory.config.BeanPostProcessor.postProcessBeforeInitialization.

Doc from org.springframework.beans.factory.config.BeanPostProcessor.postProcessBeforeInitialization.

Apply this BeanPostProcessor to the given new bean instance before any bean initialization callbacks (like InitializingBean's afterPropertiesSet or a custom init-method). The bean will already be populated with property values. The returned bean instance may be a wrapper around the original.

The default implementation returns the given bean as-is.

Parameters
bean:Object

the new bean instance

beanName:String

the name of the bean

Returns:Object

the bean instance to use, either the original or a wrapped one; if null, no subsequent BeanPostProcessors will be invoked

Annotations
@Override
Exceptions
BeansException:
in case of errors
postProcessMergedBeanDefinitionback to summary
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanClass, String beanName)

Implements org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor.postProcessMergedBeanDefinition.

Doc from org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor.postProcessMergedBeanDefinition.

Post-process the given merged bean definition for the specified bean.

Parameters
beanDefinition:RootBeanDefinition

the merged bean definition for the bean

beanClass:Class<?>

the actual type of the managed bean instance

beanName:String

the name of the bean

Annotations
@Override
processAheadOfTimeback to summary
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean)

Implements org.springframework.beans.factory.aot.BeanRegistrationAotProcessor.processAheadOfTime.

Doc from org.springframework.beans.factory.aot.BeanRegistrationAotProcessor.processAheadOfTime.

Process the given RegisteredBean instance ahead-of-time and return a contribution or null.

Processors are free to use any techniques they like to analyze the given instance. Most typically use reflection to find fields or methods to use in the contribution. Contributions typically generate source code or resource files that can be used when the AOT optimized application runs.

If the given instance isn't relevant to the processor, it should return a null contribution.

Parameters
registeredBean:RegisteredBean

the registered bean to process

Returns:BeanRegistrationAotContribution

a BeanRegistrationAotContribution or null

Annotations
@Override
@Nullable
readObjectback to summary
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException
requiresDestructionback to summary
public boolean requiresDestruction(Object bean)

Overrides default org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor.requiresDestruction.

Doc from org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor.requiresDestruction.

Determine whether the given bean instance requires destruction by this post-processor.

The default implementation returns true. If a pre-5 implementation of DestructionAwareBeanPostProcessor does not provide a concrete implementation of this method, Spring silently assumes true as well.

Parameters
bean:Object

the bean instance to check

Returns:boolean

true if postProcessBeforeDestruction is supposed to be called for this bean instance eventually, or false if not needed

Annotations
@Override
safeMergeback to summary
private static String[] safeMerge(String[] existingNames, Collection<InitDestroyAnnotationBeanPostProcessor.LifecycleMethod> detectedMethods)
setDestroyAnnotationTypeback to summary
public void setDestroyAnnotationType(Class<? extends Annotation> destroyAnnotationType)

Specify the destroy annotation to check for, indicating destruction methods to call when the context is shutting down.

Any custom annotation can be used, since there are no required annotation attributes. There is no default, although a typical choice is the jakarta.annotation.PreDestroy annotation.

See Also
addDestroyAnnotationType
setInitAnnotationTypeback to summary
public void setInitAnnotationType(Class<? extends Annotation> initAnnotationType)

Specify the init annotation to check for, indicating initialization methods to call after configuration of a bean.

Any custom annotation can be used, since there are no required annotation attributes. There is no default, although a typical choice is the jakarta.annotation.PostConstruct annotation.

See Also
addInitAnnotationType
setOrderback to summary
public void setOrder(int order)
org.springframework.beans.factory.annotation back to summary

private Class InitDestroyAnnotationBeanPostProcessor.LifecycleMetadata

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.LifecycleMetadata

Class representing information about annotated init and destroy methods.

Field Summary

Modifier and TypeField and Description
private final Class<?>
private volatile Set<InitDestroyAnnotationBeanPostProcessor.LifecycleMethod>
private volatile Set<InitDestroyAnnotationBeanPostProcessor.LifecycleMethod>
private final Collection<InitDestroyAnnotationBeanPostProcessor.LifecycleMethod>
private final Collection<InitDestroyAnnotationBeanPostProcessor.LifecycleMethod>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
public boolean
public void
public void
invokeInitMethods(Object target, String beanName)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

beanClassback to summary
private final Class<?> beanClass
checkedDestroyMethodsback to summary
private volatile Set<InitDestroyAnnotationBeanPostProcessor.LifecycleMethod> checkedDestroyMethods
Annotations
@Nullable
checkedInitMethodsback to summary
private volatile Set<InitDestroyAnnotationBeanPostProcessor.LifecycleMethod> checkedInitMethods
Annotations
@Nullable
destroyMethodsback to summary
private final Collection<InitDestroyAnnotationBeanPostProcessor.LifecycleMethod> destroyMethods
initMethodsback to summary
private final Collection<InitDestroyAnnotationBeanPostProcessor.LifecycleMethod> initMethods

Constructor Detail

LifecycleMetadataback to summary
public LifecycleMetadata(Class<?> beanClass, Collection<InitDestroyAnnotationBeanPostProcessor.LifecycleMethod> initMethods, Collection<InitDestroyAnnotationBeanPostProcessor.LifecycleMethod> destroyMethods)

Method Detail

checkInitDestroyMethodsback to summary
public void checkInitDestroyMethods(RootBeanDefinition beanDefinition)
hasDestroyMethodsback to summary
public boolean hasDestroyMethods()
invokeDestroyMethodsback to summary
public void invokeDestroyMethods(Object target, String beanName) throws Throwable
invokeInitMethodsback to summary
public void invokeInitMethods(Object target, String beanName) throws Throwable
org.springframework.beans.factory.annotation back to summary

private Class InitDestroyAnnotationBeanPostProcessor.LifecycleMethod

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.LifecycleMethod

Class representing an annotated init or destroy method.

Field Summary

Modifier and TypeField and Description
private final String
private final Method

Constructor Summary

AccessConstructor and Description
public
LifecycleMethod(Method method, Class<?> beanClass)

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(Object
the reference object with which to compare.
other
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public String
public Method
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
public void
invoke(Object target)

private static boolean
isPrivateOrNotVisible(Method method, Class<?> beanClass)

Determine if the supplied lifecycle Method is private or not visible to the supplied bean Class.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

identifierback to summary
private final String identifier
methodback to summary
private final Method method

Constructor Detail

LifecycleMethodback to summary
public LifecycleMethod(Method method, Class<?> beanClass)

Method Detail

equalsback to summary
public boolean equals(Object other)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
other:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
getIdentifierback to summary
public String getIdentifier()
getMethodback to summary
public Method getMethod()
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object.

Annotations
@Override
invokeback to summary
public void invoke(Object target) throws Throwable
isPrivateOrNotVisibleback to summary
private static boolean isPrivateOrNotVisible(Method method, Class<?> beanClass)

Determine if the supplied lifecycle Method is private or not visible to the supplied bean Class.

Since
6.0.11