Class LazyInitTargetSource

  • All Implemented Interfaces:, TargetClassAware, TargetSource, Aware, BeanFactoryAware

    public class LazyInitTargetSource
    extends AbstractBeanFactoryBasedTargetSource
    TargetSource that lazily accesses a singleton bean from a BeanFactory.

    Useful when a proxy reference is needed on initialization but the actual target object should not be initialized until first use. When the target bean is defined in an ApplicationContext (or a BeanFactory that is eagerly pre-instantiating singleton beans) it must be marked as "lazy-init" too, else it will be instantiated by said ApplicationContext (or BeanFactory) on startup.

    For example:

     <bean id="serviceTarget" class="example.MyService" lazy-init="true">
     <bean id="service" class="org.springframework.aop.framework.ProxyFactoryBean">
       <property name="targetSource">
         <bean class="">
           <property name="targetBeanName"><idref local="serviceTarget"/></property>
    The "serviceTarget" bean will not get initialized until a method on the "service" proxy gets invoked.

    Subclasses can extend this class and override the postProcessTargetObject(Object) to perform some additional processing with the target object when it is first loaded.

    Juergen Hoeller, Rob Harrop
    See Also:
    BeanFactory.getBean(java.lang.String), postProcessTargetObject(java.lang.Object), Serialized Form
    • Constructor Detail

      • LazyInitTargetSource

        public LazyInitTargetSource()
    • Method Detail

      • getTarget

        public java.lang.Object getTarget()
                                             throws BeansException
        Description copied from interface: TargetSource
        Return a target instance. Invoked immediately before the AOP framework calls the "target" of an AOP method invocation.
        the target object which contains the joinpoint, or null if there is no actual target instance
      • postProcessTargetObject

        protected void postProcessTargetObject(java.lang.Object targetObject)
        Subclasses may override this method to perform additional processing on the target object when it is first loaded.
        targetObject - the target object that has just been instantiated (and configured)