org.springframework.beans.factory.config
public class: PropertyPathFactoryBean [javadoc |
source]
java.lang.Object
org.springframework.beans.factory.config.PropertyPathFactoryBean
All Implemented Interfaces:
BeanFactoryAware, BeanNameAware, FactoryBean
FactoryBean that evaluates a property path on a given target object.
The target object can be specified directly or via a bean name.
Usage examples:
<!-- target bean to be referenced by name -->
<bean id="tb" class="org.springframework.beans.TestBean" singleton="false">
<property name="age" value="10"/>
<property name="spouse">
<bean class="org.springframework.beans.TestBean">
<property name="age" value="11"/>
</bean>
</property>
</bean>
<!-- will result in 12, which is the value of property 'age' of the inner bean -->
<bean id="propertyPath1" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<property name="targetObject">
<bean class="org.springframework.beans.TestBean">
<property name="age" value="12"/>
</bean>
</property>
<property name="propertyPath" value="age"/>
</bean>
<!-- will result in 11, which is the value of property 'spouse.age' of bean 'tb' -->
<bean id="propertyPath2" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<property name="targetBeanName" value="tb"/>
<property name="propertyPath" value="spouse.age"/>
</bean>
<!-- will result in 10, which is the value of property 'age' of bean 'tb' -->
<bean id="tb.age" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>
If you are using Spring 2.0 and XML Schema support in your configuration file(s),
you can also use the following style of configuration for property path access.
(See also the appendix entitled 'XML Schema-based configuration' in the Spring
reference manual for more examples.)
<!-- will result in 10, which is the value of property 'age' of bean 'tb' -->
<util:property-path id="name" path="testBean.age"/>
Thanks to Matthias Ernst for the suggestion and initial prototype!
| Method from org.springframework.beans.factory.config.PropertyPathFactoryBean Detail: |
public Object getObject() throws BeansException {
BeanWrapper target = this.targetBeanWrapper;
if (target == null) {
// Fetch prototype target bean...
Object bean = this.beanFactory.getBean(this.targetBeanName);
target = PropertyAccessorFactory.forBeanPropertyAccess(bean);
}
return target.getPropertyValue(this.propertyPath);
}
|
public Class getObjectType() {
return this.resultType;
}
|
public boolean isSingleton() {
return false;
}
While this FactoryBean will often be used for singleton targets,
the invoked getters for the property path might return a new object
for each call, so we have to assume that we're not returning the
same object for each #getObject() call. |
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
if (this.targetBeanWrapper != null && this.targetBeanName != null) {
throw new IllegalArgumentException("Specify either 'targetObject' or 'targetBeanName', not both");
}
if (this.targetBeanWrapper == null && this.targetBeanName == null) {
if (this.propertyPath != null) {
throw new IllegalArgumentException(
"Specify 'targetObject' or 'targetBeanName' in combination with 'propertyPath'");
}
// No other properties specified: check bean name.
int dotIndex = this.beanName.indexOf('.");
if (dotIndex == -1) {
throw new IllegalArgumentException(
"Neither 'targetObject' nor 'targetBeanName' specified, and PropertyPathFactoryBean " +
"bean name '" + this.beanName + "' does not follow 'beanName.property' syntax");
}
this.targetBeanName = this.beanName.substring(0, dotIndex);
this.propertyPath = this.beanName.substring(dotIndex + 1);
}
else if (this.propertyPath == null) {
// either targetObject or targetBeanName specified
throw new IllegalArgumentException("'propertyPath' is required");
}
if (this.targetBeanWrapper == null && this.beanFactory.isSingleton(this.targetBeanName)) {
// Eagerly fetch singleton target bean, and determine result type.
Object bean = this.beanFactory.getBean(this.targetBeanName);
this.targetBeanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(bean);
this.resultType = this.targetBeanWrapper.getPropertyType(this.propertyPath);
}
}
|
public void setBeanName(String beanName) {
this.beanName = StringUtils.trimAllWhitespace(BeanFactoryUtils.originalBeanName(beanName));
}
The bean name of this PropertyPathFactoryBean will be interpreted
as "beanName.property" pattern, if neither "targetObject" nor
"targetBeanName" nor "propertyPath" have been specified.
This allows for concise bean definitions with just an id/name. |
public void setPropertyPath(String propertyPath) {
this.propertyPath = StringUtils.trimAllWhitespace(propertyPath);
}
Specify the property path to apply to the target. |
public void setResultType(Class resultType) {
this.resultType = resultType;
}
Specify the type of the result from evaluating the property path.
Note: This is not necessary for directly specified target objects
or singleton target beans, where the type can be determined through
introspection. Just specify this in case of a prototype target,
provided that you need matching by type (for example, for autowiring). |
public void setTargetBeanName(String targetBeanName) {
this.targetBeanName = StringUtils.trimAllWhitespace(targetBeanName);
}
Specify the name of a target bean to apply the property path to.
Alternatively, specify a target object directly. |
public void setTargetObject(Object targetObject) {
this.targetBeanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(targetObject);
}
Specify a target object to apply the property path to.
Alternatively, specify a target bean name. |