Class DelegatingFactoryBean

java.lang.Object
io.microsphere.spring.beans.factory.DelegatingFactoryBean
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.DisposableBean, org.springframework.beans.factory.FactoryBean<Object>, org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationContextAware

public class DelegatingFactoryBean extends Object implements org.springframework.beans.factory.FactoryBean<Object>, org.springframework.beans.factory.InitializingBean, org.springframework.beans.factory.DisposableBean, org.springframework.context.ApplicationContextAware, org.springframework.beans.factory.BeanNameAware
A FactoryBean implementation that delegates to an existing object instance, providing lifecycle management and integration with Spring's ApplicationContext.

This class is useful when you want to expose an already instantiated object as a Spring bean, while still benefiting from Spring's lifecycle callbacks (e.g., InitializingBean, DisposableBean, etc.).

Key Features:

  • Delegates bean creation via the getObject() method.
  • Supports initialization through InitializingBean.afterPropertiesSet().
  • Supports destruction callback if the delegate implements DisposableBean.
  • Implements Spring's aware interfaces such as ApplicationContextAware and BeanNameAware, delegating calls to the target object if applicable.

Usage Example:


 MyService myService = new MyServiceImpl();
 DelegatingFactoryBean factoryBean = new DelegatingFactoryBean(myService);

 // When used in a Spring configuration:
 @Bean
 public FactoryBean<MyService> myServiceFactoryBean() {
     return new DelegatingFactoryBean<>(myServiceInstance);
 }
 

In this example, Spring will treat the returned FactoryBean as a bean definition, and the actual object returned by getObject() will be registered in the context. If the delegate implements lifecycle or aware interfaces, they will be invoked at the appropriate time during bean creation and destruction.

Since:
1.0.0
Author:
Mercy
See Also:
  • FactoryBean
  • InitializingBean
  • DisposableBean
  • ApplicationContextAware
  • BeanNameAware
  • Constructor Details

    • DelegatingFactoryBean

      public DelegatingFactoryBean(Object delegate)
    • DelegatingFactoryBean

      public DelegatingFactoryBean(Object delegate, boolean singleton)
  • Method Details

    • getObject

      public Object getObject() throws Exception
      Specified by:
      getObject in interface org.springframework.beans.factory.FactoryBean<Object>
      Throws:
      Exception
    • getObjectType

      public Class<?> getObjectType()
      Specified by:
      getObjectType in interface org.springframework.beans.factory.FactoryBean<Object>
    • afterPropertiesSet

      public void afterPropertiesSet() throws Exception
      Specified by:
      afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
      Throws:
      Exception
    • setApplicationContext

      public void setApplicationContext(org.springframework.context.ApplicationContext context) throws org.springframework.beans.BeansException
      Specified by:
      setApplicationContext in interface org.springframework.context.ApplicationContextAware
      Throws:
      org.springframework.beans.BeansException
    • setBeanName

      public void setBeanName(String name)
      Specified by:
      setBeanName in interface org.springframework.beans.factory.BeanNameAware
    • isSingleton

      public boolean isSingleton()
      Specified by:
      isSingleton in interface org.springframework.beans.factory.FactoryBean<Object>
    • destroy

      public void destroy() throws Exception
      Specified by:
      destroy in interface org.springframework.beans.factory.DisposableBean
      Throws:
      Exception