|
The Spring Framework | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor
org.springframework.beans.factory.annotation.CommonAnnotationBeanPostProcessor
public class CommonAnnotationBeanPostProcessor
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>
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 |
|---|
public CommonAnnotationBeanPostProcessor()
PostConstruct and PreDestroy,
respectively.
| Method Detail |
|---|
public void setResourceFactory(BeanFactory resourceFactory)
@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.
resourceFactory - the factory in the form of a Spring BeanFactorypublic void setBeanFactory(BeanFactory beanFactory) throws BeansException
BeanFactoryAwareInvoked after the population of normal bean properties
but before an initialization callback such as
InitializingBean.afterPropertiesSet() or a custom init-method.
setBeanFactory in interface BeanFactoryAwarebeanFactory - owning BeanFactory (never null).
The bean can immediately call methods on the factory.
BeansException - in case of initialization errorsBeanInitializationExceptionpublic Object postProcessBeforeInstantiation(Class beanClass, String beanName) throws BeansException
InstantiationAwareBeanPostProcessorIf 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.
postProcessBeforeInstantiation in interface InstantiationAwareBeanPostProcessorbeanClass - the class of the bean to be instantiatedbeanName - the name of the bean
null to proceed with default instantiation
BeansException - in case of errorsAbstractBeanDefinition.hasBeanClass(),
AbstractBeanDefinition.getFactoryMethodName()public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException
InstantiationAwareBeanPostProcessor
postProcessAfterInstantiation in interface InstantiationAwareBeanPostProcessorbean - the bean instance created, but whose properties have not yet been setbeanName - the name of the bean
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.
BeansException - in case of errorspublic PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException
InstantiationAwareBeanPostProcessorAlso 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.
postProcessPropertyValues in interface InstantiationAwareBeanPostProcessorpvs - 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 setbeanName - the name of the bean
null
to skip property population
BeansException - in case of errorsMutablePropertyValuesprotected Object getResource(String name, Class type, boolean shareable) throws BeansException
name - the name of the target resourcetype - the type of the target resourceshareable - whether the target resource is shareable
(as defined in the @Resource annotation)
null)
BeansException - if we failed to obtain the target resource
|
The Spring Framework | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||