Top Description Inners Fields Constructors Methods
org.springframework.context.annotation

public Class CommonAnnotationBeanPostProcessor

extends InitDestroyAnnotationBeanPostProcessor
implements InstantiationAwareBeanPostProcessor, BeanFactoryAware, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.Aware, org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor, org.springframework.beans.factory.config.BeanPostProcessor
Annotations
@SuppressWarnings:serial
Imports
java.beans.PropertyDescriptor, java.io.Serializable, java.lang.annotation.Annotation, java.lang.reflect.AnnotatedElement, .Field, .Member, .Method, .Modifier, java.util.ArrayList, .Collection, .Collections, .HashSet, .LinkedHashSet, .List, .Map, .Set, java.util.concurrent.ConcurrentHashMap, org.springframework.aop.TargetSource, org.springframework.aop.framework.ProxyFactory, org.springframework.aot.generate.AccessControl, .GeneratedClass, .GeneratedMethod, .GenerationContext, org.springframework.aot.hint.ExecutableMode, .RuntimeHints, org.springframework.aot.hint.support.ClassHintUtils, org.springframework.beans.BeanUtils, .PropertyValues, org.springframework.beans.factory.BeanCreationException, .BeanFactory, .BeanFactoryAware, .NoSuchBeanDefinitionException, org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor, .InjectionMetadata, org.springframework.beans.factory.aot.BeanRegistrationAotContribution, .BeanRegistrationCode, org.springframework.beans.factory.config.AutowireCapableBeanFactory, .ConfigurableBeanFactory, .DependencyDescriptor, .EmbeddedValueResolver, .InstantiationAwareBeanPostProcessor, org.springframework.beans.factory.support.AutowireCandidateResolver, .DefaultListableBeanFactory, .RegisteredBean, .RootBeanDefinition, org.springframework.core.BridgeMethodResolver, .Ordered, org.springframework.core.annotation.AnnotationUtils, org.springframework.javapoet.ClassName, .CodeBlock, org.springframework.jndi.support.SimpleJndiBeanFactory, org.springframework.lang.Nullable, org.springframework.util.Assert, .ClassUtils, .ObjectUtils, .ReflectionUtils, .StringUtils, .StringValueResolver

org.springframework.beans.factory.config.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 jakarta.annotation.PostConstruct and jakarta.annotation.PreDestroy annotations - as init annotation and destroy annotation, respectively - through inheriting from InitDestroyAnnotationBeanPostProcessor with pre-configured annotation types.

The central element is the jakarta.annotation.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.

Additionally, the original javax.annotation variants of the annotations dating back to the JSR-250 specification (Java EE 5-8, also included in JDK 6-8) are still supported as well. Note that this is primarily for a smooth upgrade path, not for adoption in new applications.

This post-processor also supports the EJB jakarta.ejb.EJB annotation, analogous to jakarta.annotation.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.

Authors
Juergen Hoeller, Sam Brannen
Since
2.5
See Also
setAlwaysUseJndiLookup, setResourceFactory, org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor, org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private class
CommonAnnotationBeanPostProcessor.EjbRefElement

Class representing injection information about an annotated field or setter method, supporting the @EJB annotation.

private class
CommonAnnotationBeanPostProcessor.LegacyResourceElement

Class representing injection information about an annotated field or setter method, supporting the @Resource annotation.

protected abstract static class
CommonAnnotationBeanPostProcessor.LookupElement

Class representing generic injection information about an annotated field or setter method, supporting @Resource and related annotations.

private class
CommonAnnotationBeanPostProcessor.ResourceElement

Class representing injection information about an annotated field or setter method, supporting the @Resource annotation.

Field Summary

Modifier and TypeField and Description
private boolean
private transient BeanFactory
private static final Class<? extends Annotation>
private transient StringValueResolver
private boolean
private final Set<String>
private final transient Map<String, InjectionMetadata>
private static final Class<? extends Annotation>
private static final Class<? extends Annotation>
private transient BeanFactory
private static final boolean
private static final Set<Class<? extends Annotation>>
private transient BeanFactory
Inherited from org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor:
logger

Constructor Summary

AccessConstructor and Description
public
CommonAnnotationBeanPostProcessor()

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

Method Summary

Modifier and TypeMethod and Description
protected Object

Returns:

the resource object (never null)
autowireResource
(BeanFactory
the factory to autowire against
factory
,
CommonAnnotationBeanPostProcessor.LookupElement
the descriptor for the annotated field/method
element
,
String
the name of the requesting bean
requestingBeanName
)

Obtain a resource object for the given name and type through autowiring based on the given factory.

protected Object

Returns:

the resource object (never null)
buildLazyResourceProxy
(CommonAnnotationBeanPostProcessor.LookupElement
the descriptor for the annotated field/method
element
,
String
the name of the requesting bean
requestingBeanName
)

Obtain a lazily resolving resource proxy for the given name and type, delegating to getResource on demand once a method call comes in.

private InjectionMetadata
private InjectionMetadata
private AutowireCandidateResolver
private Collection<CommonAnnotationBeanPostProcessor.LookupElement>
protected Object

Returns:

the resource object (never null)
getResource
(CommonAnnotationBeanPostProcessor.LookupElement
the descriptor for the annotated field/method
element
,
String
the name of the requesting bean
requestingBeanName
)

Obtain the resource object for the given name and type.

public void
ignoreResourceType(String
the resource type to ignore
resourceType
)

Ignore the given resource type when resolving @Resource annotations.

private static Class<? extends Annotation>
public boolean
postProcessAfterInstantiation(Object
the bean instance created, with properties not having been set yet
bean
,
String
the name of the bean
beanName
)

Overrides default org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation.

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.
public Object
postProcessBeforeInstantiation(Class<?>
the class of the bean to be instantiated
beanClass
,
String
the name of the bean
beanName
)

Overrides default org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation.

Apply this BeanPostProcessor before the target bean gets instantiated.
public void
postProcessMergedBeanDefinition(RootBeanDefinition
the merged bean definition for the bean
beanDefinition
,
Class<?>
the actual type of the managed bean instance
beanType
,
String
the name of the bean
beanName
)

Overrides org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessMergedBeanDefinition.

Implements org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor.postProcessMergedBeanDefinition.

Post-process the given merged bean definition for the specified bean.
public PropertyValues
postProcessProperties(PropertyValues
the property values that the factory is about to apply (never null)
pvs
,
Object
the bean instance created, but whose properties have not yet been set
bean
,
String
the name of the bean
beanName
)

Overrides default org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor.postProcessProperties.

Post-process the given property values before the factory applies them to the given bean.
public BeanRegistrationAotContribution
public void
processInjection(Object
the target instance to process
bean
)

Native processing method for direct calls with an arbitrary target instance, resolving all of its fields and methods which are annotated with one of the supported 'resource' annotation types.

public void
resetBeanDefinition(String
the name of the bean
beanName
)

Overrides default org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor.resetBeanDefinition.

A notification that the bean definition for the specified name has been reset, and that this post-processor should clear any metadata for the affected bean.
public void
setAlwaysUseJndiLookup(boolean alwaysUseJndiLookup)

Set whether to always use JNDI lookups equivalent to standard Jakarta EE resource injection, even for name attributes and default names.

public void
setBeanFactory(BeanFactory
owning BeanFactory (never null). The bean can immediately call methods on the factory.
beanFactory
)

Implements org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

Callback that supplies the owning factory to a bean instance.
public void
setFallbackToDefaultTypeMatch(boolean fallbackToDefaultTypeMatch)

Set whether to allow a fallback to a type match if no explicit name has been specified.

public void
setJndiFactory(BeanFactory jndiFactory)

Specify the factory for objects to be injected into @Resource / @EJB annotated fields and setter methods, for mappedName attributes that point directly into JNDI.

public void
setResourceFactory(BeanFactory resourceFactory)

Specify the factory for objects to be injected into @Resource / @EJB annotated fields and setter methods, for name attributes and default names.

Inherited from org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor:
addDestroyAnnotationTypeaddInitAnnotationTypegetOrderpostProcessAfterInitializationpostProcessBeforeDestructionpostProcessBeforeInitializationrequiresDestructionsetDestroyAnnotationTypesetInitAnnotationTypesetOrder