org.springframework.beans.factory.config
Class PropertyPathFactoryBean

java.lang.Object
  extended by org.springframework.beans.factory.config.PropertyPathFactoryBean
All Implemented Interfaces:
BeanFactoryAware, BeanNameAware, FactoryBean

public class PropertyPathFactoryBean
extends Object
implements FactoryBean, 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</value></property>
   <property name="spouse">
     <bean class="org.springframework.beans.TestBean">
       <property name="age"><value>11</value></property>
     </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</value></property>
     </bean>
   </property>
   <property name="propertyPath"><value>age</value></property>
 </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</value></property>
   <property name="propertyPath"><value>spouse.age</value></property>
 </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"/>
Thanks to Matthias Ernst for the suggestion and initial prototype!

Since:
1.1.2
Author:
Juergen Hoeller
See Also:
setTargetObject(java.lang.Object), setTargetBeanName(java.lang.String), setPropertyPath(java.lang.String)

Constructor Summary
PropertyPathFactoryBean()
           
 
Method Summary
 Object getObject()
          Return an instance (possibly shared or independent) of the object managed by this factory.
 Class getObjectType()
          Return the type of object that this FactoryBean creates, or null if not known in advance.
 boolean isSingleton()
          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.
 void setBeanFactory(BeanFactory beanFactory)
          Callback that supplies the owning factory to a bean instance.
 void setBeanName(String beanName)
          The bean name of this PropertyPathFactoryBean will be interpreted as "beanName.property" pattern, if neither "targetObject" nor "targetBeanName" nor "propertyPath" have been specified.
 void setPropertyPath(String propertyPath)
          Specify the property path to apply to the target.
 void setResultType(Class resultType)
          Specify the type of the result from evaluating the property path.
 void setTargetBeanName(String targetBeanName)
          Specify the name of a target bean to apply the property path to.
 void setTargetObject(Object targetObject)
          Specify a target object to apply the property path to.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PropertyPathFactoryBean

public PropertyPathFactoryBean()
Method Detail

setTargetObject

public void setTargetObject(Object targetObject)
Specify a target object to apply the property path to. Alternatively, specify a target bean name.

Parameters:
targetObject - a target object, for example a bean reference or an inner bean
See Also:
setTargetBeanName(java.lang.String)

setTargetBeanName

public void setTargetBeanName(String targetBeanName)
Specify the name of a target bean to apply the property path to. Alternatively, specify a target object directly.

Parameters:
targetBeanName - the bean name to be looked up in the containing bean factory (e.g. "testBean")
See Also:
setTargetObject(java.lang.Object)

setPropertyPath

public void setPropertyPath(String propertyPath)
Specify the property path to apply to the target.

Parameters:
propertyPath - the property path, potentially nested (e.g. "age" or "spouse.age")

setResultType

public void setResultType(Class 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).

Parameters:
resultType - the result type, for example "java.lang.Integer"

setBeanName

public void setBeanName(String 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.

Specified by:
setBeanName in interface BeanNameAware
Parameters:
beanName - the name of the bean in the factory

setBeanFactory

public void setBeanFactory(BeanFactory beanFactory)
Description copied from interface: 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.

Specified by:
setBeanFactory in interface BeanFactoryAware
Parameters:
beanFactory - owning BeanFactory (never null). The bean can immediately call methods on the factory.
See Also:
BeanInitializationException

getObject

public Object getObject()
                 throws BeansException
Description copied from interface: FactoryBean
Return an instance (possibly shared or independent) of the object managed by this factory. As with a BeanFactory, this allows support for both the Singleton and Prototype design pattern.

If this method returns null, the factory will consider the FactoryBean as not fully initialized and throw a corresponding FactoryBeanNotInitializedException.

Specified by:
getObject in interface FactoryBean
Returns:
an instance of the bean (should not be null; a null value will be considered as an indication of incomplete initialization)
Throws:
BeansException
See Also:
FactoryBeanNotInitializedException

getObjectType

public Class getObjectType()
Description copied from interface: FactoryBean
Return the type of object that this FactoryBean creates, or null if not known in advance. This allows to check for specific types of beans without instantiating objects, for example on autowiring.

For a singleton, this should try to avoid singleton creation as far as possible; it should rather estimate the type in advance. For prototypes, returning a meaningful type here is advisable too.

This method can be called before this FactoryBean has been fully initialized. It must not rely on state created during initialization; of course, it can still use such state if available.

NOTE: Autowiring will simply ignore FactoryBeans that return null here. Therefore it is highly recommended to implement this method properly, using the current state of the FactoryBean.

Specified by:
getObjectType in interface FactoryBean
Returns:
the type of object that this FactoryBean creates, or null if not known at the time of the call
See Also:
ListableBeanFactory.getBeansOfType(java.lang.Class)

isSingleton

public boolean isSingleton()
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.

Specified by:
isSingleton in interface FactoryBean
Returns:
if this bean is a singleton
See Also:
FactoryBean.getObject()


Copyright (c) 2002-2007 The Spring Framework Project.