spring-framework / org.springframework.beans.factory.annotation / InitDestroyAnnotationBeanPostProcessor

InitDestroyAnnotationBeanPostProcessor

open class InitDestroyAnnotationBeanPostProcessor : DestructionAwareBeanPostProcessor, MergedBeanDefinitionPostProcessor, PriorityOrdered, Serializable

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 JSR-250 javax.annotation.PostConstruct and javax.annotation.PreDestroy annotations out of the box, as init annotation and destroy annotation, respectively. Furthermore, it also supports the javax.annotation.Resource annotation for annotation-driven injection of named beans.

Author
Juergen Hoeller

Since
2.5

See Also
#setInitAnnotationType#setDestroyAnnotationType

Constructors

<init>

InitDestroyAnnotationBeanPostProcessor()

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 JSR-250 javax.annotation.PostConstruct and javax.annotation.PreDestroy annotations out of the box, as init annotation and destroy annotation, respectively. Furthermore, it also supports the javax.annotation.Resource annotation for annotation-driven injection of named beans.

Functions

getOrder

open fun getOrder(): Int

postProcessAfterInitialization

open fun postProcessAfterInitialization(bean: Any, beanName: String): Any

postProcessBeforeDestruction

open fun postProcessBeforeDestruction(bean: Any, beanName: String): Unit

postProcessBeforeInitialization

open fun postProcessBeforeInitialization(bean: Any, beanName: String): Any

postProcessMergedBeanDefinition

open fun postProcessMergedBeanDefinition(beanDefinition: RootBeanDefinition, beanType: Class<*>, beanName: String): Unit

requiresDestruction

open fun requiresDestruction(bean: Any): Boolean

setDestroyAnnotationType

open fun setDestroyAnnotationType(destroyAnnotationType: Class<out Annotation>): Unit

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 JSR-250 javax.annotation.PreDestroy annotation.

setInitAnnotationType

open fun setInitAnnotationType(initAnnotationType: Class<out Annotation>): Unit

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 JSR-250 javax.annotation.PostConstruct annotation.

setOrder

open fun setOrder(order: Int): Unit

Inheritors

CommonAnnotationBeanPostProcessor

open class CommonAnnotationBeanPostProcessor : InitDestroyAnnotationBeanPostProcessor, InstantiationAwareBeanPostProcessor, BeanFactoryAware, Serializable

org.springframework.beans.factory.config.BeanPostProcessor implementation that supports common Java annotations out of the box, in particular the JSR-250 annotations in the javax.annotation package. These common Java annotations are supported in many Java EE 5 technologies (e.g. JSF 1.2), as well as in Java 6's JAX-WS.

This post-processor includes support for the javax.annotation.PostConstruct and javax.annotation.PreDestroy annotations - as init annotation and destroy annotation, respectively - through inheriting from InitDestroyAnnotationBeanPostProcessor with pre-configured annotation types.

The central element is the javax.annotation.Resource annotation for annotation-driven injection of named beans, by default from the containing Spring BeanFactory, with only mappedName references resolved in JNDI. The "alwaysUseJndiLookup" flag enforces JNDI lookups equivalent to standard Java EE 5 resource injection for name references and default names as well. The target beans can be simple POJOs, with no special requirements other than the type having to match.

The JAX-WS javax.xml.ws.WebServiceRef annotation is supported too, analogous to javax.annotation.Resource but with the capability of creating specific JAX-WS service endpoints. This may either point to an explicitly defined resource by name or operate on a locally specified JAX-WS service class. Finally, this post-processor also supports the EJB 3 javax.ejb.EJB annotation, analogous to javax.annotation.Resource as well, with the capability to specify both a local bean name and a global JNDI name for fallback retrieval. The target beans can be plain POJOs as well as EJB 3 Session Beans in this case.

The common annotations supported by this post-processor are available in Java 6 (JDK 1.6) as well as in Java EE 5/6 (which provides a standalone jar for its common annotations as well, allowing for use in any Java 5 based application).

For default usage, resolving resource names as Spring bean names, simply define the following in your application context:

 <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
For direct JNDI access, resolving resource names as JNDI resource references within the Java EE application's "java:comp/env/" namespace, use the following:
 <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"> <property name="alwaysUseJndiLookup" value="true"/> </bean>
mappedName references will always be resolved in JNDI, allowing for global JNDI names (including "java:" prefix) as well. The "alwaysUseJndiLookup" flag just affects name references and default names (inferred from the field name / property name).

NOTE: A default CommonAnnotationBeanPostProcessor will be registered by the "context:annotation-config" and "context:component-scan" XML tags. Remove or turn off the default annotation configuration there if you intend to specify a custom CommonAnnotationBeanPostProcessor bean definition!

NOTE: Annotation injection will be performed before XML injection; thus the latter configuration will override the former for properties wired through both approaches.