Class CommonAnnotationBeanPostProcessor
- All Implemented Interfaces:
Serializable, BeanRegistrationAotProcessor, Aware, BeanFactoryAware, BeanPostProcessor, DestructionAwareBeanPostProcessor, InstantiationAwareBeanPostProcessor, MergedBeanDefinitionPostProcessor, Ordered, PriorityOrdered
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 (for example, 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
Spring 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 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 Session Beans in this case.
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 Jakarta EE application's "java:comp/env/" namespace, use the following:
<bean class="org.springframework.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.
- Since:
- 2.5
- Author:
- Juergen Hoeller, Sam Brannen
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classClass representing generic injection information about an annotated field or setter method, supporting @Resource and related annotations. -
Field Summary
Fields inherited from class InitDestroyAnnotationBeanPostProcessor
loggerFields inherited from interface BeanRegistrationAotProcessor
IGNORE_REGISTRATION_ATTRIBUTEFields inherited from interface Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE -
Constructor Summary
ConstructorsConstructorDescriptionCreate a new CommonAnnotationBeanPostProcessor, with the init and destroy annotation types set toPostConstructandPreDestroy, respectively. -
Method Summary
Modifier and TypeMethodDescriptionprotected ObjectautowireResource(BeanFactory factory, CommonAnnotationBeanPostProcessor.LookupElement element, @Nullable String requestingBeanName) Obtain a resource object for the given name and type through autowiring based on the given factory.protected Object