Package net.shibboleth.shared.spring.factory

# Class AbstractFactoryBean<T>

java.lang.Object
net.shibboleth.shared.spring.factory.AbstractFactoryBean<T>

Type Parameters:
  `T` - the bean type

All Implemented Interfaces:
  `Aware`, `BeanClassLoaderAware`, `BeanFactoryAware`, `DisposableBean`, `FactoryBean<T>`, `InitializingBean`

Direct Known Subclasses:
  `AbstractComponentAwareFactoryBean`, `ViewResolversFactoryBean`

public abstract class AbstractFactoryBean<T> extends Object implements FactoryBean<T>, BeanClassLoaderAware, BeanFactoryAware, InitializingBean, DisposableBean
Cloned from Spring's base class to fix bug they won't acknowledge regarding the ability for `createInstance()` to return null.

Simple template superclass for `FactoryBean` implementations that creates a singleton or a prototype object, depending on a flag.

If the "singleton" flag is `true` (the default), this class will create the object that it creates exactly once on initialization and subsequently return said singleton instance on all calls to the `getObject()` method.

Else, this class will create a new instance every time the `getObject()` method is invoked. Subclasses are responsible for implementing the abstract `createInstance()` template method to actually create the object(s) to expose.

Since:
  9.1.0
See Also:
  - `setSingleton(boolean)`
  - `createInstance()`

## Nested Class Summary

Nested Classes
Modifier and Type
Class
Description
`private class`
`AbstractFactoryBean.EarlySingletonInvocationHandler`
Reflective InvocationHandler for lazy access to the actual singleton object.

## Field Summary

Fields
Modifier and Type
Field
Description
`private ClassLoader`
`beanClassLoader`
`private BeanFactory`
`beanFactory`
`private T`
`earlySingletonInstance`
`private boolean`
`initialized`
`private boolean`
`singleton`
`private T`
`singletonInstance`

### Fields inherited from interface org.springframework.beans.factory.FactoryBean

`OBJECT_TYPE_ATTRIBUTE`

## Constructor Summary

Constructors
Constructor
Description
`AbstractFactoryBean()`

## Method Summary

Modifier and Type
Method
Description
`void`
`afterPropertiesSet()`
Eagerly create the singleton instance, if necessary.
`protected abstract T`
`createInstance()`
Template method that subclasses must override to construct the object returned by this factory.
`void`
`destroy()`
Destroy the singleton instance, if any.
`protected void`
`destroyInstance(T instance)`
Callback for destroying a singleton instance.
`protected BeanFactory`
`getBeanFactory()`
Return the BeanFactory that this bean runs in.
`protected TypeConverter`
`getBeanTypeConverter()`
Obtain a bean type converter from the BeanFactory that this bean runs in.
`private T`
`getEarlySingletonInstance()`
Determine an 'early singleton' instance, exposed in case of a circular reference.
`protected Class<?>[]`
`getEarlySingletonInterfaces()`
Return an array of interfaces that a singleton object exposed by this FactoryBean is supposed to implement, for use with an 'early singleton proxy' that will be exposed in case of a circular reference.
`final T`
`getObject()`
Expose the singleton instance or create a new prototype instance.
`abstract Class<?>`
`getObjectType()`
This abstract method declaration mirrors the method in the FactoryBean interface, for a consistent offering of abstract template methods.
`private T`
`getSingletonInstance()`
Expose the singleton instance (for access through the 'early singleton' proxy).
`boolean`
`isSingleton()`
`void`
`setBeanClassLoader(ClassLoader classLoader)`
`void`
`setBeanFactory(BeanFactory beanFactory)`
`void`
`setSingleton(boolean singleton)`
Set if a singleton should be created, or a new object on each request otherwise.

### Methods inherited from class java.lang.Object

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

## Field Details

### singleton

private boolean singleton

### beanClassLoader

@Nullable private ClassLoader beanClassLoader

### beanFactory

@Nullable private BeanFactory beanFactory

### initialized

private boolean initialized

### singletonInstance

@Nullable private T singletonInstance

### earlySingletonInstance

@Nullable private T earlySingletonInstance

## Constructor Details

### AbstractFactoryBean

public AbstractFactoryBean()

## Method Details

### setSingleton

public void setSingleton(boolean singleton)
Set if a singleton should be created, or a new object on each request otherwise. Default is `true` (a singleton).

Parameters:
  `singleton` - flag to set

### isSingleton

public boolean isSingleton()

Specified by:
  `isSingleton` in interface `FactoryBean<T>`

### setBeanClassLoader

public void setBeanClassLoader(@Nonnull ClassLoader classLoader)

Specified by:
  `setBeanClassLoader` in interface `BeanClassLoaderAware`

### setBeanFactory

public void setBeanFactory(@Nullable BeanFactory beanFactory)

Specified by:
  `setBeanFactory` in interface `BeanFactoryAware`

### getBeanFactory

@Nullable protected BeanFactory getBeanFactory()
Return the BeanFactory that this bean runs in.

Returns:
  bean factory

### getBeanTypeConverter

@Nonnull protected TypeConverter getBeanTypeConverter()
Obtain a bean type converter from the BeanFactory that this bean runs in. This is typically a fresh instance for each call, since TypeConverters are usually not thread-safe.

Falls back to a SimpleTypeConverter when not running in a BeanFactory.

Returns:
  type converter
See Also:
  - `ConfigurableBeanFactory.getTypeConverter()`
  - `SimpleTypeConverter`

### afterPropertiesSet

public void afterPropertiesSet() throws Exception
Eagerly create the singleton instance, if necessary.

Specified by:
  `afterPropertiesSet` in interface `InitializingBean`
Throws:
  `Exception`

### getObject

@Nullable public final T getObject() throws Exception
Expose the singleton instance or create a new prototype instance.

Specified by:
  `getObject` in interface `FactoryBean<T>`
Throws:
  `Exception`
See Also:
  - `createInstance()`
  - `getEarlySingletonInterfaces()`

### getEarlySingletonInstance

@Nullable private T getEarlySingletonInstance() throws Exception
Determine an 'early singleton' instance, exposed in case of a circular reference. Not called in a non-circular scenario.

Returns:
  early singleton
Throws:
  `Exception` - on error

### getSingletonInstance

@Nullable private T getSingletonInstance() throws IllegalStateException
Expose the singleton instance (for access through the 'early singleton' proxy).

Returns:
  the singleton instance that this FactoryBean holds
Throws:
  `IllegalStateException` - if the singleton instance is not initialized

### destroy

public void destroy() throws Exception
Destroy the singleton instance, if any.

Specified by:
  `destroy` in interface `DisposableBean`
Throws:
  `Exception`
See Also:
  - `destroyInstance(Object)`

### getObjectType

@Nullable public abstract Class<?> getObjectType()
This abstract method declaration mirrors the method in the FactoryBean interface, for a consistent offering of abstract template methods.

Specified by:
  `getObjectType` in interface `FactoryBean<T>`
See Also:
  - `FactoryBean.getObjectType()`

### createInstance

@Nullable protected abstract T createInstance() throws Exception
Template method that subclasses must override to construct the object returned by this factory.

Invoked on initialization of this FactoryBean in case of a singleton; else, on each `getObject()` call.

Returns:
  the object returned by this factory
Throws:
  `Exception` - if an exception occurred during object creation
See Also:
  - `getObject()`

### getEarlySingletonInterfaces

@Nullable protected Class<?>[] getEarlySingletonInterfaces()
Return an array of interfaces that a singleton object exposed by this FactoryBean is supposed to implement, for use with an 'early singleton proxy' that will be exposed in case of a circular reference.

The default implementation returns this FactoryBean's object type, provided that it is an interface, or `null` otherwise. The latter indicates that early singleton access is not supported by this FactoryBean. This will lead to a FactoryBeanNotInitializedException getting thrown.

Returns:
  the interfaces to use for 'early singletons', or `null` to indicate a FactoryBeanNotInitializedException
See Also:
  - `FactoryBeanNotInitializedException`

### destroyInstance

protected void destroyInstance(@Nullable T instance) throws Exception
Callback for destroying a singleton instance. Subclasses may override this to destroy the previously created instance.

The default implementation is empty.

Parameters:
  `instance` - the singleton instance, as returned by `createInstance()`
Throws:
  `Exception` - in case of shutdown errors
See Also:
  - `createInstance()`