org.springframework.beans.factory.config

## 类 MethodInvokingFactoryBean

- java.lang.Object
- - org.springframework.util.MethodInvoker
  - - org.springframework.beans.support.ArgumentConvertingMethodInvoker
    - - org.springframework.beans.factory.config.MethodInvokingBean
      - - org.springframework.beans.factory.config.MethodInvokingFactoryBean

- 所有已实现的接口:
    Aware, BeanClassLoaderAware, BeanFactoryAware, FactoryBean<Object>, InitializingBean

      public class MethodInvokingFactoryBean
      extends MethodInvokingBean
      implements FactoryBean<Object>

  `FactoryBean` which returns a value which is the result of a static or instance method invocation. For most use cases it is better to just use the container's built-in factory method support for the same purpose, since that is smarter at converting arguments. This factory bean is still useful though when you need to call a method which doesn't return any value (for example, a static class method to force some sort of initialization to happen). This use case is not supported by factory methods, since a return value is needed to obtain the bean instance.

  Note that as it is expected to be used mostly for accessing factory methods, this factory by default operates in a singleton fashion. The first request to `getObject()` by the owning bean factory will cause a method invocation, whose return value will be cached for subsequent requests. An internal `singleton` property may be set to "false", to cause this factory to invoke the target method each time it is asked for an object.

  NOTE: If your target method does not produce a result to expose, consider `MethodInvokingBean` instead, which avoids the type determination and lifecycle limitations that this `MethodInvokingFactoryBean` comes with.

  This invoker supports any kind of target method. A static method may be specified by setting the `targetMethod` property to a String representing the static method name, with `targetClass` specifying the Class that the static method is defined on. Alternatively, a target instance method may be specified, by setting the `targetObject` property as the target object, and the `targetMethod` property as the name of the method to call on that target object. Arguments for the method invocation may be specified by setting the `arguments` property.

  This class depends on `afterPropertiesSet()` being called once all properties have been set, as per the InitializingBean contract.

  An example (in an XML based bean factory definition) of a bean definition which uses this class to call a static factory method:

       <bean id="myObject" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
         <property name="staticMethod" value="com.whatever.MyClassFactory.getInstance"/>
       </bean>

  An example of calling a static method then an instance method to get at a Java system property. Somewhat verbose, but it works.

       <bean id="sysProps" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
         <property name="targetClass" value="java.lang.System"/>
         <property name="targetMethod" value="getProperties"/>
       </bean>

       <bean id="javaVersion" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
         <property name="targetObject" ref="sysProps"/>
         <property name="targetMethod" value="getProperty"/>
         <property name="arguments" value="java.version"/>
       </bean>

  从以下版本开始:
    21.11.2003
  作者:
    Colin Sampaleanu, Juergen Hoeller
  另请参阅:
    `MethodInvokingBean`, `MethodInvoker`

- - ### 字段概要

    - ### 从类继承的字段 org.springframework.util.MethodInvoker

      `targetClass`

  - ### 构造器概要

    构造器
    构造器和说明
    `MethodInvokingFactoryBean()`

  - ### 方法概要

    所有方法 实例方法 具体方法
    限定符和类型 | 方法和说明
    --- | ---
    `void` | `afterPropertiesSet()` Invoked by the containing `BeanFactory` after it has set all bean properties and satisfied `BeanFactoryAware`, `ApplicationContextAware` etc.
    `Object` | `getObject()` Returns the same value each time if the singleton property is set to "true", otherwise returns the value returned from invoking the specified method on the fly.
    `Class<?>` | `getObjectType()` Return the type of object that this FactoryBean creates, or `null` if not known in advance.
    `boolean` | `isSingleton()` Is the object managed by this factory a singleton?
    `void` | `setSingleton(boolean singleton)` Set if a singleton should be created, or a new object on each `getObject()` request otherwise.

    - ### 从类继承的方法 org.springframework.beans.factory.config.MethodInvokingBean

      `getDefaultTypeConverter, invokeWithTargetException, resolveClassName, setBeanClassLoader, setBeanFactory`

    - ### 从类继承的方法 org.springframework.beans.support.ArgumentConvertingMethodInvoker

      `doFindMatchingMethod, findMatchingMethod, getTypeConverter, registerCustomEditor, setTypeConverter`

    - ### 从类继承的方法 org.springframework.util.MethodInvoker

      `getArguments, getPreparedMethod, getTargetClass, getTargetMethod, getTargetObject, getTypeDifferenceWeight, invoke, isPrepared, prepare, setArguments, setStaticMethod, setTargetClass, setTargetMethod, setTargetObject`

    - ### 从类继承的方法 java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### 构造器详细资料

    - #### MethodInvokingFactoryBean

          public MethodInvokingFactoryBean()

  - ### 方法详细资料

    - #### setSingleton

          public void setSingleton(boolean singleton)

      Set if a singleton should be created, or a new object on each `getObject()` request otherwise. Default is "true".

    - #### afterPropertiesSet

          public void afterPropertiesSet()
                                  throws Exception

      从接口复制的说明: `InitializingBean`
      Invoked by the containing `BeanFactory` after it has set all bean properties and satisfied `BeanFactoryAware`, `ApplicationContextAware` etc.

      This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.

      指定者:
        `afterPropertiesSet` 在接口中 `InitializingBean`
      覆盖:
        `afterPropertiesSet` 在类中 `MethodInvokingBean`
      抛出:
        `Exception` - in the event of misconfiguration (such as failure to set an essential property) or if initialization fails for any other reason

    - #### getObject

          @Nullable
          public Object getObject()
                                      throws Exception

      Returns the same value each time if the singleton property is set to "true", otherwise returns the value returned from invoking the specified method on the fly.
      指定者:
        `getObject` 在接口中 `FactoryBean<Object>`
      返回:
        an instance of the bean (can be `null`)
      抛出:
        `Exception` - in case of creation errors
      另请参阅:
        `FactoryBeanNotInitializedException`

    - #### getObjectType

          public Class<?> getObjectType()

      Return the type of object that this FactoryBean creates, or `null` if not known in advance.
      指定者:
        `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>)`

    - #### isSingleton

          public boolean isSingleton()

      从接口复制的说明: `FactoryBean`
      Is the object managed by this factory a singleton? That is, will `FactoryBean.getObject()` always return the same object (a reference that can be cached)?

      NOTE: If a FactoryBean indicates to hold a singleton object, the object returned from `getObject()` might get cached by the owning BeanFactory. Hence, do not return `true` unless the FactoryBean always exposes the same reference.

      The singleton status of the FactoryBean itself will generally be provided by the owning BeanFactory; usually, it has to be defined as singleton there.

      NOTE: This method returning `false` does not necessarily indicate that returned objects are independent instances. An implementation of the extended `SmartFactoryBean` interface may explicitly indicate independent instances through its `SmartFactoryBean.isPrototype()` method. Plain `FactoryBean` implementations which do not implement this extended interface are simply assumed to always return independent instances if the `isSingleton()` implementation returns `false`.

      The default implementation returns `true`, since a `FactoryBean` typically manages a singleton instance.

      指定者:
        `isSingleton` 在接口中 `FactoryBean<Object>`
      返回:
        whether the exposed object is a singleton
      另请参阅:
        `FactoryBean.getObject()`, `SmartFactoryBean.isPrototype()`