org.springframework.beans.factory.annotation
public class: InitDestroyAnnotationBeanPostProcessor [javadoc |
source]
java.lang.Object
org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor
All Implemented Interfaces:
PriorityOrdered, DestructionAwareBeanPostProcessor, Serializable, MergedBeanDefinitionPostProcessor
Direct Known Subclasses:
CommonAnnotationBeanPostProcessor
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.
| Field Summary |
|---|
| protected final Log | logger | Logger available to subclasses |
| Method from org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor Detail: |
public int getOrder() {
return this.order;
}
|
public Object postProcessAfterInitialization(Object bean,
String beanName) throws BeansException {
return bean;
}
|
public void postProcessBeforeDestruction(Object bean,
String beanName) throws BeansException {
LifecycleMetadata metadata = findLifecycleMetadata(bean.getClass());
try {
metadata.invokeDestroyMethods(bean, beanName);
}
catch (InvocationTargetException ex) {
String msg = "Invocation of destroy method failed on bean with name '" + beanName + "'";
if (logger.isDebugEnabled()) {
logger.warn(msg, ex.getTargetException());
}
else {
logger.warn(msg + ": " + ex.getTargetException());
}
}
catch (Throwable ex) {
logger.error("Couldn't invoke destroy method on bean with name '" + beanName + "'", ex);
}
}
|
public Object postProcessBeforeInitialization(Object bean,
String beanName) throws BeansException {
LifecycleMetadata metadata = findLifecycleMetadata(bean.getClass());
try {
metadata.invokeInitMethods(bean, beanName);
}
catch (InvocationTargetException ex) {
throw new BeanCreationException(beanName, "Invocation of init method failed", ex.getTargetException());
}
catch (Throwable ex) {
throw new BeanCreationException(beanName, "Couldn't invoke init method", ex);
}
return bean;
}
|
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition,
Class beanType,
String beanName) {
if (beanType != null) {
LifecycleMetadata metadata = findLifecycleMetadata(beanType);
for (Iterator< LifecycleElement > it = metadata.getInitMethods().iterator(); it.hasNext();) {
String methodName = it.next().getMethod().getName();
if (!beanDefinition.isExternallyManagedInitMethod(methodName)) {
beanDefinition.registerExternallyManagedInitMethod(methodName);
}
else {
it.remove();
}
}
for (Iterator< LifecycleElement > it = metadata.getDestroyMethods().iterator(); it.hasNext();) {
String methodName = it.next().getMethod().getName();
if (!beanDefinition.isExternallyManagedDestroyMethod(methodName)) {
beanDefinition.registerExternallyManagedDestroyMethod(methodName);
}
else {
it.remove();
}
}
}
}
|
public void setDestroyAnnotationType(Class destroyAnnotationType) {
this.destroyAnnotationType = 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 JSR-250 javax.annotation.PreDestroy annotation. |
public void setInitAnnotationType(Class initAnnotationType) {
this.initAnnotationType = 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 JSR-250 javax.annotation.PostConstruct annotation. |
public void setOrder(int order) {
this.order = order;
}
|