站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > Spring Framework 2.1.0 API 文档英文版

RequiredAnnotationBeanPostProcessor (Spring Framework API 2.1) - Spring Framework 2.1.0 API 文档英文版


The Spring Framework

org.springframework.beans.factory.annotation
Class RequiredAnnotationBeanPostProcessor

java.lang.Object
  extended by org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter
      extended by org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor
All Implemented Interfaces:
BeanPostProcessor, InstantiationAwareBeanPostProcessor, SmartInstantiationAwareBeanPostProcessor

public class RequiredAnnotationBeanPostProcessor
extends InstantiationAwareBeanPostProcessorAdapter

BeanPostProcessor implementation that enforces required JavaBean properties to have been configured. Required bean properties are detected through a Java 5 annotation: by default, Spring's Required 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 implemented (and may still be desirable), because all that this class does is enforce 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.

Since:
2.0
Author:
Rob Harrop, Juergen Hoeller
See Also:
setRequiredAnnotationType(java.lang.Class), Required

Constructor Summary
RequiredAnnotationBeanPostProcessor()
           
 
Method Summary
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)?
 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 setRequiredAnnotationType(Class<? extends Annotation> requiredAnnotationType)
          Set the 'required' annotation type, to be used on bean property setter methods.
 
Methods inherited from class org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter
determineConstructor, postProcessAfterInitialization, postProcessAfterInstantiation, postProcessBeforeInitialization, postProcessBeforeInstantiation, predictBeanType
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RequiredAnnotationBeanPostProcessor

public RequiredAnnotationBeanPostProcessor()
Method Detail

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.


postProcessPropertyValues

public PropertyValues postProcessPropertyValues(PropertyValues pvs,
                                                PropertyDescriptor[] pds,
                                                Object bean,
                                                String beanName)
                                         throws BeansException
Description copied from interface: 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.

Specified by:
postProcessPropertyValues in interface InstantiationAwareBeanPostProcessor
Overrides:
postProcessPropertyValues in class InstantiationAwareBeanPostProcessorAdapter
Parameters:
pvs - the property values that the factory is about to apply (never null)
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 set
beanName - the name of the bean
Returns:
the actual property values to apply to to the given bean (can be the passed-in PropertyValues instance), or null to skip property population
Throws:
BeansException - in case of errors
See Also:
MutablePropertyValues

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 supplied property.

Parameters:
propertyDescriptor - the target PropertyDescriptor (never null)
Returns:
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

The Spring Framework

Copyright © 2002-2007 The Spring Framework.