Top Description Fields Constructors Methods
org.springframework.beans.factory.config

public Class PropertyPathFactoryBean

extends Object
implements FactoryBean<Object>, BeanNameAware, BeanFactoryAware
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.FactoryBean
Imports
org.apache.commons.logging.Log, .LogFactory, org.springframework.beans.BeanWrapper, .BeansException, .PropertyAccessorFactory, org.springframework.beans.factory.BeanFactory, .BeanFactoryAware, .BeanFactoryUtils, .BeanNameAware, .FactoryBean, org.springframework.lang.Nullable, org.springframework.util.Assert, .StringUtils

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

Field Summary

Modifier and TypeField and Description
private BeanFactory
private String
private static final Log
private String
private Class<?>
private String
private BeanWrapper

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Object
getObject()

Implements org.springframework.beans.factory.FactoryBean.getObject.

Return an instance (possibly shared or independent) of the object managed by this factory.
public Class<?>
getObjectType()

Implements org.springframework.beans.factory.FactoryBean.getObjectType.

Return the type of object that this FactoryBean creates, or null if not known in advance.
public boolean
isSingleton()

Overrides default org.springframework.beans.factory.FactoryBean.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.
public void
setBeanFactory(BeanFactory
owning BeanFactory (never null). The bean can immediately call methods on the factory.
beanFactory
)

Implements org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

Callback that supplies the owning factory to a bean instance.
public void
setBeanName(String
the name of the bean in the factory. Note that this name is the actual bean name used in the factory, which may differ from the originally specified name: in particular for inner bean names, the actual bean name might have been made unique through appending "#..." suffixes. Use the BeanFactoryUtils#originalBeanName(String) method to extract the original bean name (without suffix), if desired.
beanName
)

Implements org.springframework.beans.factory.BeanNameAware.setBeanName.

The bean name of this PropertyPathFactoryBean will be interpreted as "beanName.property" pattern, if neither "targetObject" nor "targetBeanName" nor "propertyPath" have been specified.
public void
setPropertyPath(String
the property path, potentially nested (e.g. "age" or "spouse.age")
propertyPath
)

Specify the property path to apply to the target.

public void
setResultType(Class<?>
the result type, for example "java.lang.Integer"
resultType
)

Specify the type of the result from evaluating the property path.

public void
setTargetBeanName(String
the bean name to be looked up in the containing bean factory (e.g. "testBean")
targetBeanName
)

Specify the name of a target bean to apply the property path to.

public void
setTargetObject(Object
a target object, for example a bean reference or an inner bean
targetObject
)

Specify a target object to apply the property path to.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

beanFactoryback to summary
private BeanFactory beanFactory
Annotations
@Nullable
beanNameback to summary
private String beanName
Annotations
@Nullable
loggerback to summary
private static final Log logger
propertyPathback to summary
private String propertyPath
Annotations
@Nullable
resultTypeback to summary
private Class<?> resultType
Annotations
@Nullable
targetBeanNameback to summary
private String targetBeanName
Annotations
@Nullable
targetBeanWrapperback to summary
private BeanWrapper targetBeanWrapper
Annotations
@Nullable

Constructor Detail

PropertyPathFactoryBeanback to summary
public PropertyPathFactoryBean()

Method Detail

getObjectback to summary
public Object getObject() throws BeansException

Implements org.springframework.beans.factory.FactoryBean.getObject.

Doc from org.springframework.beans.factory.FactoryBean.getObject.

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 FactoryBean is not fully initialized yet at the time of the call (for example because it is involved in a circular reference), throw a corresponding FactoryBeanNotInitializedException.

As of Spring 2.0, FactoryBeans are allowed to return null objects. The factory will consider this as normal value to be used; it will not throw a FactoryBeanNotInitializedException in this case anymore. FactoryBean implementations are encouraged to throw FactoryBeanNotInitializedException themselves now, as appropriate.

Returns:Object

an instance of the bean (can be null)

Annotations
@Override
@Nullable
getObjectTypeback to summary
public Class<?> getObjectType()

Implements org.springframework.beans.factory.FactoryBean.getObjectType.

Doc from org.springframework.beans.factory.FactoryBean.getObjectType.

Return the type of object that this FactoryBean creates, or null if not known in advance.

This allows one to check for specific types of beans without instantiating objects, for example on autowiring.

In the case of implementations that are creating a singleton object, this method 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.

Returns:Class<?>

the type of object that this FactoryBean creates, or null if not known at the time of the call

Annotations
@Override
isSingletonback to summary
public boolean isSingleton()

Overrides default org.springframework.beans.factory.FactoryBean.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.

Returns:boolean

Doc from org.springframework.beans.factory.FactoryBean.isSingleton.

whether the exposed object is a singleton

Annotations
@Override
setBeanFactoryback to summary
public void setBeanFactory(BeanFactory beanFactory)

Implements org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

Doc from org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

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.

Parameters
beanFactory:BeanFactory

owning BeanFactory (never null). The bean can immediately call methods on the factory.

Annotations
@Override
setBeanNameback to summary
public void setBeanName(String beanName)

Implements org.springframework.beans.factory.BeanNameAware.setBeanName.

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.

Parameters
beanName:String

Doc from org.springframework.beans.factory.BeanNameAware.setBeanName.

the name of the bean in the factory. Note that this name is the actual bean name used in the factory, which may differ from the originally specified name: in particular for inner bean names, the actual bean name might have been made unique through appending "#..." suffixes. Use the BeanFactoryUtils#originalBeanName(String) method to extract the original bean name (without suffix), if desired.

Annotations
@Override
setPropertyPathback to summary
public void setPropertyPath(String propertyPath)

Specify the property path to apply to the target.

Parameters
propertyPath:String

the property path, potentially nested (e.g. "age" or "spouse.age")

setResultTypeback to summary
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:Class<?>

the result type, for example "java.lang.Integer"

setTargetBeanNameback to summary
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:String

the bean name to be looked up in the containing bean factory (e.g. "testBean")

See Also
setTargetObject
setTargetObjectback to summary
public void setTargetObject(Object targetObject)

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

Parameters
targetObject:Object

a target object, for example a bean reference or an inner bean

See Also
setTargetBeanName