站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > Spring Framework 2.1.0 API 文档英文版

CommonAnnotationBeanPostProcessor (Spring Framework API 2.1) - Spring Framework 2.1.0 API 文档英文版


The Spring Framework

org.springframework.beans.factory.annotation
Class CommonAnnotationBeanPostProcessor

java.lang.Object
  extended by org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor
      extended by org.springframework.beans.factory.annotation.CommonAnnotationBeanPostProcessor
All Implemented Interfaces:
Serializable, BeanFactoryAware, BeanPostProcessor, DestructionAwareBeanPostProcessor, InstantiationAwareBeanPostProcessor

public class CommonAnnotationBeanPostProcessor
extends InitDestroyAnnotationBeanPostProcessor
implements InstantiationAwareBeanPostProcessor, BeanFactoryAware, Serializable

BeanPostProcessor implementation that supports common JSR-250 annotations out of the box.

This includes support for the PostConstruct and PreDestroy annotations (as init annotation and destroy annotation, respectively). Also supports the Resource annotation for annotation-driven injection of named beans, either from the containing BeanFactory or from a specified resource factory (e.g. Spring's SimpleJndiBeanFactory for JNDI lookup behavior equivalent to standard Java EE 5 resource injection}.

The common JSR-250 annotations supported by this post-processor are available in Java 6 (JDK 1.6) as well as in Java EE 5 (which provides a standalone jar for its common annotations as well, allowing for use in any Java 5 based application). Hence, this post-processor works out of the box on JDK 1.6, and requires the JSR-250 API jar to be added to the classpath on JDK 1.5.

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

 <bean class="org.springframework.beans.factory.annotation.CommonAnnotationBeanPostProcessor"/>
For direct JNDI access, resolving resource names as JNDI resource references within the J2EE application's "java:comp/env/" namespace, use the following:
 <bean class="org.springframework.beans.factory.annotation.CommonAnnotationBeanPostProcessor">
   <property name="resourceFactory">
     <bean class="org.springframework.jndi.support.SimpleJndiBeanFactory"/>
   </property>
 </bean>

Since:
2.1
Author:
Juergen Hoeller
See Also:
setResourceFactory(org.springframework.beans.factory.BeanFactory), InitDestroyAnnotationBeanPostProcessor, AutowiredAnnotationBeanPostProcessor, Serialized Form

Constructor Summary
CommonAnnotationBeanPostProcessor()
          Create a new CommonAnnotationBeanPostProcessor, with the init and destroy annotation types set to PostConstruct and PreDestroy, respectively.
 
Method Summary
protected  Object getResource(String name, Class type, boolean shareable)
          Obtain the resource object for the given name and type.
 boolean postProcessAfterInstantiation(Object bean, String beanName)
          Perform operations after the bean has been instantiated, via a constructor or factory method, but before Spring property population (from explicit properties or autowiring) occurs.
 Object postProcessBeforeInstantiation(Class beanClass, String beanName)
          Apply this BeanPostProcessor before the target bean gets instantiated.
 PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName)
          Post-process the given property values before the factory applies them to the given bean.
 void setBeanFactory(BeanFactory beanFactory)
          Callback that supplies the owning factory to a bean instance.
 void setResourceFactory(BeanFactory resourceFactory)
          Specify the factory for objects to be injected into @Resource annotated fields and setter methods.
 
Methods inherited from class org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor
postProcessAfterInitialization, postProcessBeforeDestruction, postProcessBeforeInitialization, setDestroyAnnotationType, setInitAnnotationType
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.springframework.beans.factory.config.BeanPostProcessor
postProcessAfterInitialization, postProcessBeforeInitialization
 

Constructor Detail

CommonAnnotationBeanPostProcessor

public CommonAnnotationBeanPostProcessor()
Create a new CommonAnnotationBeanPostProcessor, with the init and destroy annotation types set to PostConstruct and PreDestroy, respectively.

Method Detail

setResourceFactory

public void setResourceFactory(BeanFactory resourceFactory)
Specify the factory for objects to be injected into @Resource annotated fields and setter methods.

The default is the BeanFactory that this post-processor is defined in, if any, looking up resource names as Spring bean names. Specify the resource factory explicitly for programmatic usage of this post-processor.

Specify Spring's SimpleJndiBeanFactory for JNDI lookup behavior equivalent to standard Java EE 5 resource injection, as available to Servlet 2.5 Servlets and EJB 3.0 Session Beans.

Parameters:
resourceFactory - the factory in the form of a Spring BeanFactory

setBeanFactory

public void setBeanFactory(BeanFactory beanFactory)
                    throws BeansException
Description copied from interface: BeanFactoryAware
Callback that supplies the owning factory to a bean instance.

Invoked after the population of normal bean properties but before an initialization callback such as InitializingBean.afterPropertiesSet() or a custom init-method.

Specified by:
setBeanFactory in interface BeanFactoryAware
Parameters:
beanFactory - owning BeanFactory (never null). The bean can immediately call methods on the factory.
Throws:
BeansException - in case of initialization errors
See Also:
BeanInitializationException

postProcessBeforeInstantiation

public Object postProcessBeforeInstantiation(Class beanClass,
                                             String beanName)
                                      throws BeansException
Description copied from interface: InstantiationAwareBeanPostProcessor
Apply this BeanPostProcessor before the target bean gets instantiated. The returned bean object may be a proxy to use instead of the target bean, effectively suppressing default instantiation of the target bean.

If a non-null object is returned by this method, the bean creation process will be short-circuited. The only further processing applied is the BeanPostProcessor.postProcessAfterInitialization(java.lang.Object, java.lang.String) callback from the configured BeanPostProcessors.

This callback will only be applied to bean definitions with a bean class. In particular, it will not be applied to beans with a "factory-method".

Post-processors may implement the extended SmartInstantiationAwareBeanPostProcessor interface in order to predict the type of the bean object that they are going to return here.

Specified by:
postProcessBeforeInstantiation in interface InstantiationAwareBeanPostProcessor
Parameters:
beanClass - the class of the bean to be instantiated
beanName - the name of the bean
Returns:
the bean object to expose instead of a default instance of the target bean, or null to proceed with default instantiation
Throws:
BeansException - in case of errors
See Also:
AbstractBeanDefinition.hasBeanClass(), AbstractBeanDefinition.getFactoryMethodName()

postProcessAfterInstantiation

public boolean postProcessAfterInstantiation(Object bean,
                                             String beanName)
                                      throws BeansException
Description copied from interface: InstantiationAwareBeanPostProcessor
Perform operations after the bean has been instantiated, via a constructor or factory method, but before Spring property population (from explicit properties or autowiring) occurs.

Specified by:
postProcessAfterInstantiation in interface InstantiationAwareBeanPostProcessor
Parameters:
bean - the bean instance created, but whose properties have not yet been set
beanName - the name of the bean
Returns:
true if properties should be set on the bean; false if property population should be skipped. Normal implementations should return true. Returning false will also prevent any subsequent InstantiationAwareBeanPostProcessor instances being invoked on this bean instance.
Throws:
BeansException - in case of errors

postProcessPropertyValues

public PropertyValues postProcessPropertyValues(PropertyValues pvs,
                                                PropertyDescriptor[] pds,
                                                Object bean,
                                                String beanName)
                                         throws BeansException
Description copied from interface: InstantiationAwareBeanPostProcessor
Post-process the given property values before the factory applies them to the given bean. Allows for checking whether all dependencies have been satisfied, for example based on a "Required" annotation on bean property setters.

Also allows for replacing the property values to apply, typically through creating a new MutablePropertyValues instance based on the original PropertyValues, adding or removing specific values.

Specified by:
postProcessPropertyValues in interface InstantiationAwareBeanPostProcessor
Parameters:
pvs - the property values that the factory is about to apply (never null)
pds - the relevant property descriptors for the target bean (with ignored dependency types - which the factory handles specifically - already filtered out)
bean - the bean instance created, but whose properties have not yet been set
beanName - the name of the bean
Returns:
the actual property values to apply to to the given bean (can be the passed-in PropertyValues instance), or null to skip property population
Throws:
BeansException - in case of errors
See Also:
MutablePropertyValues

getResource

protected Object getResource(String name,
                             Class type,
                             boolean shareable)
                      throws BeansException
Obtain the resource object for the given name and type.

Parameters:
name - the name of the target resource
type - the type of the target resource
shareable - whether the target resource is shareable (as defined in the @Resource annotation)
Returns:
the resource object (never null)
Throws:
BeansException - if we failed to obtain the target resource

The Spring Framework

Copyright © 2002-2007 The Spring Framework.