类 CommonAnnotationBeanPostProcessor
- 所有已实现的接口:
Aware,BeanFactoryAware,BeanPostProcessor,DestructionAwareBeanPostProcessor,DependenciesBeanPostProcessor,InitializationBeanPostProcessor,MergedBeanDefinitionPostProcessor,cn.taketoday.core.Ordered,cn.taketoday.core.PriorityOrdered,Serializable
BeanPostProcessor implementation
that supports common Java annotations out of the box, in particular the common
annotations in the jakarta.annotation package. These common Java
annotations are supported in many Jakarta EE technologies (e.g. JSF and JAX-RS).
This post-processor includes support for the PostConstruct
and PreDestroy annotations - as init annotation
and destroy annotation, respectively - through inheriting from
InitDestroyAnnotationBeanPostProcessor with pre-configured annotation types.
The central element is the Resource annotation
for annotation-driven injection of named beans, by default from the containing
Framework BeanFactory, with only mappedName references resolved in JNDI.
The "alwaysUseJndiLookup" flag enforces JNDI lookups
equivalent to standard Jakarta EE 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.
This post-processor also supports the EJB 3 EJB annotation,
analogous to Resource, 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.
For default usage, resolving resource names as Framework bean names, simply define the following in your application context:
<bean class="cn.taketoday.context.annotation.CommonAnnotationBeanPostProcessor"/>For direct JNDI access, resolving resource names as JNDI resource references within the Jakarta EE application's "java:comp/env/" namespace, use the following:
<bean class="cn.taketoday.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.
- 从以下版本开始:
- 4.0 2022/3/5 12:09
- 作者:
- Juergen Hoeller, Sam Brannen, Harry Yang
- 另请参阅:
-
嵌套类概要
嵌套类修饰符和类型类说明protected static classClass representing generic injection information about an annotated field or setter method, supporting @Resource and related annotations. -
字段概要
从类继承的字段 cn.taketoday.core.OrderedSupport
order从接口继承的字段 cn.taketoday.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE -
构造器概要
构造器构造器说明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(cn.taketoday.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.voidpostProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Object bean, String beanName) processDependencies(PropertyValues propertyValues, Object bean, String beanName) voidresetBeanDefinition(String beanName) voidsetAlwaysUseJndiLookup(boolean alwaysUseJndiLookup) Set whether to always use JNDI lookups equivalent to standard Jakarta EE resource injection, even fornameattributes and default names.voidsetBeanFactory(BeanFactory beanFactory) 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/@EJBannotated fields and setter methods, formappedNameattributes that point directly into JNDI.voidsetResourceFactory(BeanFactory resourceFactory) Specify the factory for objects to be injected into@Resource/@EJBannotated fields and setter methods, fornameattributes and default names.从类继承的方法 cn.taketoday.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor
postProcessAfterInitialization, postProcessBeforeDestruction, postProcessBeforeInitialization, requiresDestruction, setDependencyInjector, setDestroyAnnotationType, setInitAnnotationType从类继承的方法 cn.taketoday.core.OrderedSupport
getOrder, setOrder从类继承的方法 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait从接口继承的方法 cn.taketoday.core.Ordered
getOrder
-
构造器详细资料
-
CommonAnnotationBeanPostProcessor
public CommonAnnotationBeanPostProcessor()Create a new CommonAnnotationBeanPostProcessor, with the init and destroy annotation types set toPostConstructandPreDestroy, respectively.
-
-
方法详细资料
-
ignoreResourceType
Ignore the given resource type when resolving@Resourceannotations.- 参数:
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 Jakarta EE resource injection, even fornameattributes and default names.Default is "false": Resource names are used for Framework bean lookups in the containing BeanFactory; only
mappedNameattributes point directly into JNDI. Switch this flag to "true" for enforcing Jakarta EE style JNDI lookups in any case, even fornameattributes and default names. -
setJndiFactory
Specify the factory for objects to be injected into@Resource/@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 Jakarta EE resource injection. -
setResourceFactory
Specify the factory for objects to be injected into@Resource/@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 Framework bean names. Specify the resource factory explicitly for programmatic usage of this post-processor.
Specifying Framework's
SimpleJndiBeanFactoryleads to JNDI lookup behavior equivalent to standard Jakarta EE resource injection, even fornameattributes and default names. This is the same behavior that the "alwaysUseJndiLookup" flag enables. -
setBeanFactory
- 指定者:
setBeanFactory在接口中BeanFactoryAware- 覆盖:
setBeanFactory在类中InitDestroyAnnotationBeanPostProcessor
-
postProcessMergedBeanDefinition
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Object bean, String beanName) -
resetBeanDefinition
-
processDependencies
public PropertyValues processDependencies(PropertyValues propertyValues, Object bean, String beanName) -
buildLazyResourceProxy
protected Object buildLazyResourceProxy(CommonAnnotationBeanPostProcessor.LookupElement element, @Nullable String requestingBeanName) Obtain a lazily resolving resource proxy for the given name and type, delegating togetResource(cn.taketoday.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) - 另请参阅:
-
getResource
protected Object getResource(CommonAnnotationBeanPostProcessor.LookupElement element, @Nullable String requestingBeanName) throws NoSuchBeanDefinitionException 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) - 抛出:
NoSuchBeanDefinitionException- if no corresponding target resource found
-
autowireResource
protected Object autowireResource(BeanFactory factory, CommonAnnotationBeanPostProcessor.LookupElement element, @Nullable String requestingBeanName) throws NoSuchBeanDefinitionException 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) - 抛出:
NoSuchBeanDefinitionException- if no corresponding target resource found
-