类 RequiredAnnotationBeanPostProcessor
- java.lang.Object
- org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter
- org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor
- 所有已实现的接口:
Aware
,BeanFactoryAware
,BeanPostProcessor
,InstantiationAwareBeanPostProcessor
,SmartInstantiationAwareBeanPostProcessor
,MergedBeanDefinitionPostProcessor
,Ordered
,PriorityOrdered
public class RequiredAnnotationBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter implements MergedBeanDefinitionPostProcessor, PriorityOrdered, BeanFactoryAware
BeanPostProcessor
implementation that enforces required JavaBean properties to have been configured. Required bean properties are detected through a Java 5 annotation: by default, Spring'sRequired
annotation.The motivation for the existence of this BeanPostProcessor is to allow developers to annotate the setter properties of their own classes with an arbitrary JDK 1.5 annotation to indicate that the container must check for the configuration of a dependency injected value. This neatly pushes responsibility for such checking onto the container (where it arguably belongs), and obviates the need (in part) for a developer to code a method that simply checks that all required properties have actually been set.
Please note that an 'init' method may still need to be implemented (and may still be desirable), because all that this class does is enforcing that a 'required' property has actually been configured with a value. It does not check anything else... In particular, it does not check that a configured value is not
null
.Note: A default RequiredAnnotationBeanPostProcessor 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 RequiredAnnotationBeanPostProcessor bean definition.
- 从以下版本开始:
- 2.0
- 作者:
- Rob Harrop, Juergen Hoeller
- 另请参阅:
setRequiredAnnotationType(java.lang.Class<? extends java.lang.annotation.Annotation>)
,Required
字段概要
字段 修饰符和类型 字段 说明 static String
SKIP_REQUIRED_CHECK_ATTRIBUTE
Bean definition attribute that may indicate whether a given bean is supposed to be skipped when performing this post-processor's required property check.从接口继承的字段 org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
构造器概要
构造器 构造器 说明 RequiredAnnotationBeanPostProcessor()
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 int
getOrder()
Get the order value of this object.protected Class<? extends Annotation>
getRequiredAnnotationType()
Return the 'required' annotation type.protected boolean
isRequiredProperty(PropertyDescriptor propertyDescriptor)
Is the supplied property required to have a value (that is, to be dependency-injected)?void
postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName)
Post-process the given merged bean definition for the specified bean.PropertyValues
postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName)
Post-process the given property values before the factory applies them to the given bean.void
setBeanFactory(BeanFactory beanFactory)
Callback that supplies the owning factory to a bean instance.void
setOrder(int order)
void
setRequiredAnnotationType(Class<? extends Annotation> requiredAnnotationType)
Set the 'required' annotation type, to be used on bean property setter methods.protected boolean
shouldSkip(ConfigurableListableBeanFactory beanFactory, String beanName)
Check whether the given bean definition is not subject to the annotation-based required property check as performed by this post-processor.从类继承的方法 org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter
determineCandidateConstructors, getEarlyBeanReference, postProcessAfterInitialization, postProcessAfterInstantiation, postProcessBeforeInitialization, postProcessBeforeInstantiation, predictBeanType
从类继承的方法 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
从接口继承的方法 org.springframework.beans.factory.config.BeanPostProcessor
postProcessAfterInitialization, postProcessBeforeInitialization
字段详细资料
SKIP_REQUIRED_CHECK_ATTRIBUTE
public static final String SKIP_REQUIRED_CHECK_ATTRIBUTE
Bean definition attribute that may indicate whether a given bean is supposed to be skipped when performing this post-processor's required property check.
构造器详细资料
RequiredAnnotationBeanPostProcessor
public RequiredAnnotationBeanPostProcessor()
方法详细资料
setRequiredAnnotationType
public void setRequiredAnnotationType(Class<? extends Annotation> requiredAnnotationType)
Set the 'required' annotation type, to be used on bean property setter methods.The default required annotation type is the Spring-provided
Required
annotation.This setter property exists so that developers can provide their own (non-Spring-specific) annotation type to indicate that a property value is required.
getRequiredAnnotationType
protected Class<? extends Annotation> getRequiredAnnotationType()
Return the 'required' annotation type.
setBeanFactory
public void setBeanFactory(BeanFactory beanFactory)
从接口复制的说明:BeanFactoryAware
Callback 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
setOrder
public void setOrder(int order)
getOrder
public int getOrder()
从接口复制的说明:Ordered
Get the order value of this object.Higher values are interpreted as lower priority. As a consequence, the object with the lowest value has the highest priority (somewhat analogous to Servlet
load-on-startup
values).Same order values will result in arbitrary sort positions for the affected objects.
- 指定者:
getOrder
在接口中Ordered
- 返回:
- the order value
- 另请参阅:
Ordered.HIGHEST_PRECEDENCE
,Ordered.LOWEST_PRECEDENCE
postProcessMergedBeanDefinition
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName)
从接口复制的说明:MergedBeanDefinitionPostProcessor
Post-process the given merged bean definition for the specified bean.- 指定者:
postProcessMergedBeanDefinition
在接口中MergedBeanDefinitionPostProcessor
- 参数:
beanDefinition
- the merged bean definition for the beanbeanType
- the actual type of the managed bean instancebeanName
- the name of the bean
postProcessPropertyValues
public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException
从接口复制的说明:InstantiationAwareBeanPostProcessor
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.
- 指定者:
postProcessPropertyValues
在接口中InstantiationAwareBeanPostProcessor
- 覆盖:
postProcessPropertyValues
在类中InstantiationAwareBeanPostProcessorAdapter
- 参数:
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
null
to skip property population - 抛出:
BeansException
- in case of errors- 另请参阅:
MutablePropertyValues
shouldSkip
protected boolean shouldSkip(ConfigurableListableBeanFactory beanFactory, String beanName)
Check whether the given bean definition is not subject to the annotation-based required property check as performed by this post-processor.The default implementations check for the presence of the
SKIP_REQUIRED_CHECK_ATTRIBUTE
attribute in the bean definition, if any. It also suggests skipping in case of a bean definition with a "factory-bean" reference set, assuming that instance-based factories pre-populate the bean.- 参数:
beanFactory
- the BeanFactory to check againstbeanName
- the name of the bean to check against- 返回:
true
to skip the bean;false
to process it
isRequiredProperty
protected boolean isRequiredProperty(PropertyDescriptor propertyDescriptor)
Is the supplied property required to have a value (that is, to be dependency-injected)?This implementation looks for the existence of a
"required" annotation
on the suppliedproperty
.- 参数:
propertyDescriptor
- the target PropertyDescriptor (nevernull
)- 返回:
true
if the supplied property has been marked as being required;false
if not, or if the supplied property does not have a setter method