类 CommonAnnotationBeanPostProcessor
- java.lang.Object
- org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor
- org.springframework.context.annotation.CommonAnnotationBeanPostProcessor
- 所有已实现的接口:
Serializable,Aware,BeanFactoryAware,BeanPostProcessor,DestructionAwareBeanPostProcessor,InstantiationAwareBeanPostProcessor,MergedBeanDefinitionPostProcessor,Ordered,PriorityOrdered
public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBeanPostProcessor implements InstantiationAwareBeanPostProcessor, BeanFactoryAware, Serializable
BeanPostProcessorimplementation that supports common Java annotations out of the box, in particular the JSR-250 annotations in thejavax.annotationpackage. 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
PostConstructandPreDestroyannotations - as init annotation and destroy annotation, respectively - through inheriting fromInitDestroyAnnotationBeanPostProcessorwith pre-configured annotation types.The central element is the
Resourceannotation for annotation-driven injection of named beans, by default from the containing Spring BeanFactory, with onlymappedNamereferences resolved in JNDI. The"alwaysUseJndiLookup" flagenforces JNDI lookups equivalent to standard Java EE 5 resource injection fornamereferences 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
WebServiceRefannotation is supported too, analogous toResourcebut 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 3EJBannotation, analogous toResourceas 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>
mappedNamereferences will always be resolved in JNDI, allowing for global JNDI names (including "java:" prefix) as well. The "alwaysUseJndiLookup" flag just affectsnamereferences 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.
嵌套类概要
嵌套类 修饰符和类型 类 说明 protected classCommonAnnotationBeanPostProcessor.LookupElementClass representing generic injection information about an annotated field or setter method, supporting @Resource and related annotations.
字段概要
从类继承的字段 org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor
logger
从接口继承的字段 org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
构造器概要
构造器 构造器 说明 CommonAnnotationBeanPostProcessor()Create a new CommonAnnotationBeanPostProcessor, with the init and destroy annotation types set toPostConstructandPreDestroy, respectively.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 protected ObjectautowireResource(BeanFactory factory, CommonAnnotationBeanPostProcessor.LookupElement element, String requestingBeanName)Obtain a resource object for the given name and type through autowiring based on the given factory.protected ObjectbuildLazyResourceProxy(CommonAnnotationBeanPostProcessor.LookupElement element, String requestingBeanName)Obtain a lazily resolving resource proxy for the given name and type, delegating togetResource(org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.LookupElement, java.lang.String)on demand once a method call comes in.protected ObjectgetResource(CommonAnnotationBeanPostProcessor.LookupElement element, String requestingBeanName)Obtain the resource object for the given name and type.voidignoreResourceType(String resourceType)Ignore the given resource type when resolving@Resourceannotations.booleanpostProcessAfterInstantiation(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.ObjectpostProcessBeforeInstantiation(Class<?> beanClass, String beanName)Apply this BeanPostProcessor before the target bean gets instantiated.voidpostProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName)Post-process the given merged bean definition for the specified bean.PropertyValuespostProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName)Post-process the given property values before the factory applies them to the given bean.voidsetAlwaysUseJndiLookup(boolean alwaysUseJndiLookup)Set whether to always use JNDI lookups equivalent to standard Java EE 5 resource injection, even fornameattributes and default names.voidsetBeanFactory(BeanFactory beanFactory)Callback that supplies the owning factory to a bean instance.voidsetFallbackToDefaultTypeMatch(boolean fallbackToDefaultTypeMatch)Set whether to allow a fallback to a type match if no explicit name has been specified.voidsetJndiFactory(BeanFactory jndiFactory)Specify the factory for objects to be injected into@Resource/@WebServiceRef/@EJBannotated fields and setter methods, formappedNameattributes that point directly into JNDI.voidsetResourceFactory(BeanFactory resourceFactory)Specify the factory for objects to be injected into@Resource/@WebServiceRef/@EJBannotated fields and setter methods, fornameattributes and default names.从类继承的方法 org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor
getOrder, postProcessAfterInitialization, postProcessBeforeDestruction, postProcessBeforeInitialization, requiresDestruction, setDestroyAnnotationType, setInitAnnotationType, setOrder
从类继承的方法 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
从接口继承的方法 org.springframework.beans.factory.config.BeanPostProcessor
postProcessAfterInitialization, postProcessBeforeInitialization
构造器详细资料
CommonAnnotationBeanPostProcessor
public CommonAnnotationBeanPostProcessor()
Create a new CommonAnnotationBeanPostProcessor, with the init and destroy annotation types set toPostConstructandPreDestroy, respectively.
方法详细资料
ignoreResourceType
public void ignoreResourceType(String resourceType)
Ignore the given resource type when resolving@Resourceannotations.By default, the
javax.xml.ws.WebServiceContextinterface will be ignored, since it will be resolved by the JAX-WS runtime.- 参数:
resourceType- the resource type to ignore
setFallbackToDefaultTypeMatch
public void setFallbackToDefaultTypeMatch(boolean fallbackToDefaultTypeMatch)
Set whether to allow a fallback to a type match if no explicit name has been specified. The default name (i.e. the field name or bean property name) will still be checked first; if a bean of that name exists, it will be taken. However, if no bean of that name exists, a by-type resolution of the dependency will be attempted if this flag is "true".Default is "true". Switch this flag to "false" in order to enforce a by-name lookup in all cases, throwing an exception in case of no name match.
setAlwaysUseJndiLookup
public void setAlwaysUseJndiLookup(boolean alwaysUseJndiLookup)
Set whether to always use JNDI lookups equivalent to standard Java EE 5 resource injection, even fornameattributes and default names.Default is "false": Resource names are used for Spring bean lookups in the containing BeanFactory; only
mappedNameattributes point directly into JNDI. Switch this flag to "true" for enforcing Java EE style JNDI lookups in any case, even fornameattributes and default names.
setJndiFactory
public void setJndiFactory(BeanFactory jndiFactory)
Specify the factory for objects to be injected into@Resource/@WebServiceRef/@EJBannotated fields and setter methods, formappedNameattributes that point directly into JNDI. This factory will also be used if "alwaysUseJndiLookup" is set to "true" in order to enforce JNDI lookups even fornameattributes and default names.The default is a
SimpleJndiBeanFactoryfor JNDI lookup behavior equivalent to standard Java EE 5 resource injection.
setResourceFactory
public void setResourceFactory(BeanFactory resourceFactory)
Specify the factory for objects to be injected into@Resource/@WebServiceRef/@EJBannotated fields and setter methods, fornameattributes and default names.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.
Specifying Spring's
SimpleJndiBeanFactoryleads to JNDI lookup behavior equivalent to standard Java EE 5 resource injection, even fornameattributes and default names. This is the same behavior that the "alwaysUseJndiLookup" flag enables.
setBeanFactory
public void setBeanFactory(BeanFactory beanFactory)
从接口复制的说明:BeanFactoryAwareCallback 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.- 指定者:
setBeanFactory在接口中BeanFactoryAware- 参数:
beanFactory- owning BeanFactory (nevernull). The bean can immediately call methods on the factory.- 另请参阅:
BeanInitializationException
postProcessMergedBeanDefinition
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName)
从接口复制的说明:MergedBeanDefinitionPostProcessorPost-process the given merged bean definition for the specified bean.- 指定者:
postProcessMergedBeanDefinition在接口中MergedBeanDefinitionPostProcessor- 覆盖:
postProcessMergedBeanDefinition在类中InitDestroyAnnotationBeanPostProcessor- 参数:
beanDefinition- the merged bean definition for the beanbeanType- the actual type of the managed bean instancebeanName- the name of the bean
postProcessBeforeInstantiation
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException
从接口复制的说明:InstantiationAwareBeanPostProcessorApply 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 configuredBeanPostProcessors.This callback will be applied to bean definitions with their bean class, as well as to factory-method definitions in which case the returned bean type will be passed in here.
Post-processors may implement the extended
SmartInstantiationAwareBeanPostProcessorinterface in order to predict the type of the bean object that they are going to return here.- 指定者:
postProcessBeforeInstantiation在接口中InstantiationAwareBeanPostProcessor- 参数:
beanClass- the class of the bean to be instantiatedbeanName- the name of the bean- 返回:
- the bean object to expose instead of a default instance of the target bean, or
nullto proceed with default instantiation - 抛出:
BeansException- in case of errors- 另请参阅:
InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation(java.lang.Object, java.lang.String),AbstractBeanDefinition.getBeanClass(),AbstractBeanDefinition.getFactoryMethodName()
postProcessAfterInstantiation
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException
从接口复制的说明:InstantiationAwareBeanPostProcessorPerform operations after the bean has been instantiated, via a constructor or factory method, but before Spring property population (from explicit properties or autowiring) occurs.This is the ideal callback for performing custom field injection on the given bean instance, right before Spring's autowiring kicks in.
- 指定者:
postProcessAfterInstantiation在接口中InstantiationAwareBeanPostProcessor- 参数:
bean- the bean instance created, with properties not having been set yetbeanName- the name of the bean- 返回:
trueif properties should be set on the bean;falseif property population should be skipped. Normal implementations should returntrue. Returningfalsewill also prevent any subsequent InstantiationAwareBeanPostProcessor instances being invoked on this bean instance.- 抛出:
BeansException- in case of errors- 另请参阅:
InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation(java.lang.Class<?>, java.lang.String)
postProcessPropertyValues
public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException
从接口复制的说明:InstantiationAwareBeanPostProcessorPost-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.
- 指定者:
postProcessPropertyValues在接口中InstantiationAwareBeanPostProcessor- 参数:
pvs- the property values that the factory is about to apply (nevernull)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- 返回:
- the actual property values to apply to the given bean (can be the passed-in PropertyValues instance), or
nullto skip property population - 抛出:
BeansException- in case of errors- 另请参阅:
MutablePropertyValues
buildLazyResourceProxy
protected Object buildLazyResourceProxy(CommonAnnotationBeanPostProcessor.LookupElement element, String requestingBeanName)
Obtain a lazily resolving resource proxy for the given name and type, delegating togetResource(org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.LookupElement, java.lang.String)on demand once a method call comes in.- 参数:
element- the descriptor for the annotated field/methodrequestingBeanName- the name of the requesting bean- 返回:
- the resource object (never
null) - 从以下版本开始:
- 4.2
- 另请参阅:
getResource(org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.LookupElement, java.lang.String),Lazy
getResource
protected Object getResource(CommonAnnotationBeanPostProcessor.LookupElement element, String requestingBeanName) throws BeansException
Obtain the resource object for the given name and type.- 参数:
element- the descriptor for the annotated field/methodrequestingBeanName- the name of the requesting bean- 返回:
- the resource object (never
null) - 抛出:
BeansException- if we failed to obtain the target resource
autowireResource
protected Object autowireResource(BeanFactory factory, CommonAnnotationBeanPostProcessor.LookupElement element, String requestingBeanName) throws BeansException
Obtain a resource object for the given name and type through autowiring based on the given factory.- 参数:
factory- the factory to autowire againstelement- the descriptor for the annotated field/methodrequestingBeanName- the name of the requesting bean- 返回:
- the resource object (never
null) - 抛出:
BeansException- if we failed to obtain the target resource