Class AnnotatedInjectionBeanPostProcessor
- java.lang.Object
-
- io.microsphere.spring.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter
-
- io.microsphere.spring.beans.factory.annotation.AnnotatedInjectionBeanPostProcessor
-
- All Implemented Interfaces:
org.springframework.beans.factory.Aware
,org.springframework.beans.factory.BeanClassLoaderAware
,org.springframework.beans.factory.BeanFactoryAware
,org.springframework.beans.factory.config.BeanPostProcessor
,org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor
,org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor
,org.springframework.beans.factory.DisposableBean
,org.springframework.beans.factory.InitializingBean
,org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor
,org.springframework.context.EnvironmentAware
,org.springframework.core.Ordered
,org.springframework.core.PriorityOrdered
public class AnnotatedInjectionBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter implements org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor, org.springframework.core.PriorityOrdered, org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.BeanClassLoaderAware, org.springframework.context.EnvironmentAware, org.springframework.beans.factory.InitializingBean, org.springframework.beans.factory.DisposableBean
The genericBeanPostProcessor
implementation to support the dependency injection for the customized annotations.As a substitution, besides the core features of
AutowiredAnnotationBeanPostProcessor
,AnnotatedInjectionBeanPostProcessor
also supports:The dependency injection with multiple types of annotations
-
Annotation Features Enhancement:
setClassValuesAsString(boolean)
: whether to turn Class references into Strings (for compatibility with or to preserve them as Class referencessetNestedAnnotationsAsMap(boolean)
: whether to turn nested Annotation instances into AnnotationAttributes maps (for compatibility withAnnotationMetadata
or to preserve them as Annotation instancessetIgnoreDefaultValue(boolean)
: whether ignore default value or not: whether try merged annotation or not
- {@link #setCacheSize(int) The size of the metadata cache}
- Since:
- 1.0.0
- Author:
- Mercy
- See Also:
AutowiredAnnotationBeanPostProcessor
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
AnnotatedInjectionBeanPostProcessor.AnnotationInjectedElement<M extends java.lang.reflect.Member>
AnnotationInjectionMetadata.InjectedElement
-
Constructor Summary
Constructors Constructor Description AnnotatedInjectionBeanPostProcessor(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType, java.lang.Class<? extends java.lang.annotation.Annotation>... otherAnnotationTypes)
AnnotatedInjectionBeanPostProcessor(java.util.Collection<java.lang.Class<? extends java.lang.annotation.Annotation>> annotationTypes)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
afterPropertiesSet()
void
destroy()
java.lang.reflect.Constructor<?>[]
determineCandidateConstructors(java.lang.Class<?> beanClass, java.lang.String beanName)
Determine the candidate constructors to use for the given bean.protected boolean
determineRequiredStatus(org.springframework.core.annotation.AnnotationAttributes attributes)
Determine if the annotated field or method requires its dependency.protected org.springframework.core.annotation.AnnotationAttributes
doGetAnnotationAttributes(java.lang.reflect.AnnotatedElement annotatedElement, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
GetAnnotationAttributes
protected org.springframework.core.annotation.AnnotationAttributes
findInjectionAnnotationAttributes(java.lang.reflect.AnnotatedElement annotatedElement)
java.util.Collection<java.lang.Class<? extends java.lang.annotation.Annotation>>
getAnnotationTypes()
org.springframework.beans.factory.config.ConfigurableListableBeanFactory
getBeanFactory()
java.lang.ClassLoader
getClassLoader()
org.springframework.core.env.Environment
getEnvironment()
int
getOrder()
void
postProcessMergedBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition beanDefinition, java.lang.Class<?> beanType, java.lang.String beanName)
org.springframework.beans.PropertyValues
postProcessProperties(org.springframework.beans.PropertyValues pvs, java.lang.Object bean, java.lang.String beanName)
Post-process the given property values before the factory applies them to the given bean.org.springframework.beans.PropertyValues
postProcessPropertyValues(org.springframework.beans.PropertyValues pvs, java.beans.PropertyDescriptor[] pds, java.lang.Object bean, java.lang.String beanName)
Post-process the given property values before the factory applies them to the given bean.protected java.lang.Object
resolveDependency(org.springframework.beans.factory.config.DependencyDescriptor desc, java.lang.String beanName, java.util.Set<java.lang.String> injectedBeanNames)
protected java.lang.Object
resolveInjectedFieldValue(java.lang.Object bean, java.lang.String beanName, org.springframework.beans.PropertyValues pvs, AnnotatedInjectionBeanPostProcessor.AnnotationInjectedElement<java.lang.reflect.Field> fieldElement)
Resolve the injected-object as the value of the annotatedField
protected java.lang.Object[]
resolveInjectedMethodArguments(java.lang.Object bean, java.lang.String beanName, org.springframework.beans.PropertyValues pvs, AnnotatedInjectionBeanPostProcessor.AnnotationInjectedElement<java.lang.reflect.Method> methodElement)
Resolve the injected-objects as the arguments of the annotatedmethod
void
setBeanClassLoader(java.lang.ClassLoader classLoader)
void
setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory)
void
setCacheSize(int cacheSize)
Set the size of cachevoid
setClassValuesAsString(boolean classValuesAsString)
void
setEnvironment(org.springframework.core.env.Environment environment)
void
setIgnoreDefaultValue(boolean ignoreDefaultValue)
void
setNestedAnnotationsAsMap(boolean nestedAnnotationsAsMap)
void
setOrder(int order)
void
setTryMergedAnnotation(boolean tryMergedAnnotation)
-
Methods inherited from class io.microsphere.spring.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter
determineBeanType, getEarlyBeanReference, postProcessAfterInstantiation, postProcessBeforeInstantiation, predictBeanType
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
-
-
Constructor Detail
-
AnnotatedInjectionBeanPostProcessor
public AnnotatedInjectionBeanPostProcessor(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType, java.lang.Class<? extends java.lang.annotation.Annotation>... otherAnnotationTypes)
- Parameters:
annotationType
- the single type ofannotation
-
AnnotatedInjectionBeanPostProcessor
public AnnotatedInjectionBeanPostProcessor(java.util.Collection<java.lang.Class<? extends java.lang.annotation.Annotation>> annotationTypes)
- Parameters:
annotationTypes
- the multiple types ofannotations
-
-
Method Detail
-
getAnnotationTypes
public final java.util.Collection<java.lang.Class<? extends java.lang.annotation.Annotation>> getAnnotationTypes()
-
setBeanFactory
public final void setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory) throws org.springframework.beans.BeansException
- Specified by:
setBeanFactory
in interfaceorg.springframework.beans.factory.BeanFactoryAware
- Throws:
org.springframework.beans.BeansException
-
determineCandidateConstructors
public final java.lang.reflect.Constructor<?>[] determineCandidateConstructors(java.lang.Class<?> beanClass, java.lang.String beanName) throws org.springframework.beans.BeansException
Description copied from class:InstantiationAwareBeanPostProcessorAdapter
Determine the candidate constructors to use for the given bean.The default implementation returns
null
.- Specified by:
determineCandidateConstructors
in interfaceorg.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor
- Overrides:
determineCandidateConstructors
in classInstantiationAwareBeanPostProcessorAdapter
- Parameters:
beanClass
- the raw class of the bean (nevernull
)beanName
- the name of the bean- Returns:
- the candidate constructors, or
null
if none specified - Throws:
org.springframework.beans.BeansException
- in case of errors
-
postProcessPropertyValues
public final org.springframework.beans.PropertyValues postProcessPropertyValues(org.springframework.beans.PropertyValues pvs, java.beans.PropertyDescriptor[] pds, java.lang.Object bean, java.lang.String beanName) throws org.springframework.beans.factory.BeanCreationException
Description copied from class:InstantiationAwareBeanPostProcessorAdapter
Post-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.
The default implementation returns the given
pvs
as-is.- Specified by:
postProcessPropertyValues
in interfaceorg.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor
- Overrides:
postProcessPropertyValues
in classInstantiationAwareBeanPostProcessorAdapter
- Parameters:
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- Returns:
- the actual property values to apply to the given bean (can be the passed-in
PropertyValues instance), or
null
to skip property population - Throws:
org.springframework.beans.factory.BeanCreationException
- See Also:
InstantiationAwareBeanPostProcessorAdapter.postProcessProperties(org.springframework.beans.PropertyValues, java.lang.Object, java.lang.String)
,MutablePropertyValues
-
postProcessProperties
public final org.springframework.beans.PropertyValues postProcessProperties(org.springframework.beans.PropertyValues pvs, java.lang.Object bean, java.lang.String beanName) throws org.springframework.beans.BeansException
Description copied from class:InstantiationAwareBeanPostProcessorAdapter
Post-process the given property values before the factory applies them to the given bean.The default implementation returns the given
pvs
as-is.- Specified by:
postProcessProperties
in interfaceorg.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor
- Overrides:
postProcessProperties
in classInstantiationAwareBeanPostProcessorAdapter
- Parameters:
pvs
- the property values that the factory is about to apply (nevernull
)bean
- the bean instance created, but whose properties have not yet been setbeanName
- the name of the bean- Returns:
- the actual property values to apply to the given bean (can be the passed-in
PropertyValues instance), or
null
to skip property population - Throws:
org.springframework.beans.BeansException
- in case of errors
-
determineRequiredStatus
protected boolean determineRequiredStatus(org.springframework.core.annotation.AnnotationAttributes attributes)
Determine if the annotated field or method requires its dependency.- Parameters:
attributes
- the injected annotation attributes- Returns:
- whether the annotation indicates that a dependency is required
-
findInjectionAnnotationAttributes
protected final org.springframework.core.annotation.AnnotationAttributes findInjectionAnnotationAttributes(java.lang.reflect.AnnotatedElement annotatedElement)
-
doGetAnnotationAttributes
protected final org.springframework.core.annotation.AnnotationAttributes doGetAnnotationAttributes(java.lang.reflect.AnnotatedElement annotatedElement, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
GetAnnotationAttributes
- Parameters:
annotatedElement
-the annotated element
annotationType
- thetyoe
pfannotation
- Returns:
- if
annotatedElement
can't be found inannotatedElement
, returnnull
-
postProcessMergedBeanDefinition
public final void postProcessMergedBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition beanDefinition, java.lang.Class<?> beanType, java.lang.String beanName)
- Specified by:
postProcessMergedBeanDefinition
in interfaceorg.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor
-
setClassValuesAsString
public final void setClassValuesAsString(boolean classValuesAsString)
- Parameters:
classValuesAsString
- whether to turn Class references into Strings (for compatibility withAnnotationMetadata
or to preserve them as Class references
-
setNestedAnnotationsAsMap
public final void setNestedAnnotationsAsMap(boolean nestedAnnotationsAsMap)
- Parameters:
nestedAnnotationsAsMap
- whether to turn nested Annotation instances intoAnnotationAttributes
maps (for compatibility withAnnotationMetadata
or to preserve them as Annotation instances
-
setIgnoreDefaultValue
public final void setIgnoreDefaultValue(boolean ignoreDefaultValue)
- Parameters:
ignoreDefaultValue
- whether ignore default value or not
-
setTryMergedAnnotation
public final void setTryMergedAnnotation(boolean tryMergedAnnotation)
- Parameters:
tryMergedAnnotation
- whether try merged annotation or not
-
setCacheSize
public final void setCacheSize(int cacheSize)
Set the size of cache- Parameters:
cacheSize
- the size of cache
-
setOrder
public final void setOrder(int order)
-
setBeanClassLoader
public final void setBeanClassLoader(java.lang.ClassLoader classLoader)
- Specified by:
setBeanClassLoader
in interfaceorg.springframework.beans.factory.BeanClassLoaderAware
-
setEnvironment
public final void setEnvironment(org.springframework.core.env.Environment environment)
- Specified by:
setEnvironment
in interfaceorg.springframework.context.EnvironmentAware
-
afterPropertiesSet
public void afterPropertiesSet() throws java.lang.Exception
- Specified by:
afterPropertiesSet
in interfaceorg.springframework.beans.factory.InitializingBean
- Throws:
java.lang.Exception
-
destroy
public void destroy() throws java.lang.Exception
- Specified by:
destroy
in interfaceorg.springframework.beans.factory.DisposableBean
- Throws:
java.lang.Exception
-
resolveInjectedFieldValue
protected java.lang.Object resolveInjectedFieldValue(java.lang.Object bean, java.lang.String beanName, org.springframework.beans.PropertyValues pvs, AnnotatedInjectionBeanPostProcessor.AnnotationInjectedElement<java.lang.reflect.Field> fieldElement) throws java.lang.Throwable
Resolve the injected-object as the value of the annotatedField
- Parameters:
bean
- The bean that will be injectedbeanName
- The name of requesting bean that will be injectedpvs
-PropertyValues
fieldElement
-the field element
was annotated- Returns:
- An injected object
- Throws:
java.lang.Throwable
- If resolving is failed
-
resolveInjectedMethodArguments
protected java.lang.Object[] resolveInjectedMethodArguments(java.lang.Object bean, java.lang.String beanName, org.springframework.beans.PropertyValues pvs, AnnotatedInjectionBeanPostProcessor.AnnotationInjectedElement<java.lang.reflect.Method> methodElement) throws java.lang.Throwable
Resolve the injected-objects as the arguments of the annotatedmethod
- Parameters:
bean
- The bean that will be injectedbeanName
- The name of the bean that will be injectedpvs
-PropertyValues
methodElement
-the method element
was annotated- Returns:
- The array of the injected objects as the arguments of the annotated
method
- Throws:
java.lang.Throwable
- If resolving is failed
-
resolveDependency
protected final java.lang.Object resolveDependency(org.springframework.beans.factory.config.DependencyDescriptor desc, java.lang.String beanName, java.util.Set<java.lang.String> injectedBeanNames)
-
getOrder
public final int getOrder()
- Specified by:
getOrder
in interfaceorg.springframework.core.Ordered
-
getEnvironment
public final org.springframework.core.env.Environment getEnvironment()
-
getClassLoader
public final java.lang.ClassLoader getClassLoader()
-
getBeanFactory
public final org.springframework.beans.factory.config.ConfigurableListableBeanFactory getBeanFactory()
-
-