类 PropertyPathFactoryBean

  • 所有已实现的接口:
    Aware, BeanFactoryAware, BeanNameAware, FactoryBean<Object>

    public class PropertyPathFactoryBean
    extends Object
    implements FactoryBean<Object>, 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!
    从以下版本开始:
    1.1.2
    作者:
    Juergen Hoeller
    另请参阅:
    setTargetObject(java.lang.Object), setTargetBeanName(java.lang.String), setPropertyPath(java.lang.String)
    • 方法详细资料

      • setTargetObject

        public void setTargetObject​(Object targetObject)
        Specify a target object to apply the property path to. Alternatively, specify a target bean name.
        参数:
        targetObject - a target object, for example a bean reference or an inner bean
        另请参阅:
        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.
        参数:
        targetBeanName - the bean name to be looked up in the containing bean factory (e.g. "testBean")
        另请参阅:
        setTargetObject(java.lang.Object)
      • setPropertyPath

        public void setPropertyPath​(String propertyPath)
        Specify the property path to apply to the target.
        参数:
        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).

        参数:
        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.
        指定者:
        setBeanName 在接口中 BeanNameAware
        参数:
        beanName - 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.
      • getObject

        public Object getObject()
                         throws BeansException
        从接口复制的说明: 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 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.

        指定者:
        getObject 在接口中 FactoryBean<Object>
        返回:
        an instance of the bean (can be null)
        抛出:
        BeansException
        另请参阅:
        FactoryBeanNotInitializedException
      • getObjectType

        public Class<?> getObjectType()
        从接口复制的说明: FactoryBean
        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.

        指定者:
        getObjectType 在接口中 FactoryBean<Object>
        返回:
        the type of object that this FactoryBean creates, or null if not known at the time of the call
        另请参阅:
        ListableBeanFactory.getBeansOfType(java.lang.Class<T>)