spring-framework / org.springframework.beans.factory.config / PropertyPathFactoryBean

PropertyPathFactoryBean

open class PropertyPathFactoryBean : FactoryBean<Any>, BeanNameAware, BeanFactoryAware

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!

Author
Juergen Hoeller

Since
1.1.2

See Also
#setTargetObject#setTargetBeanName#setPropertyPath

Constructors

<init>

PropertyPathFactoryBean()

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!

Functions

getObject

open fun getObject(): Any

getObjectType

open fun getObjectType(): Class<*>

isSingleton

open fun isSingleton(): Boolean

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.

setBeanFactory

open fun setBeanFactory(beanFactory: BeanFactory): Unit

setBeanName

open fun setBeanName(beanName: String): Unit

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.

setPropertyPath

open fun setPropertyPath(propertyPath: String): Unit

Specify the property path to apply to the target.

setResultType

open fun setResultType(resultType: Class<*>): Unit

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).

setTargetBeanName

open fun setTargetBeanName(targetBeanName: String): Unit

Specify the name of a target bean to apply the property path to. Alternatively, specify a target object directly.

setTargetObject

open fun setTargetObject(targetObject: Any): Unit

Specify a target object to apply the property path to. Alternatively, specify a target bean name.