- Building Microservices with Spring
- Dinesh Rajput Rajesh R V
- 137字
- 2021-07-02 14:54:11
Implementing the Abstract Factory Pattern in Spring (FactoryBean interface)
The Spring Framework provides the FactoryBean interface as an implementation of the Abstract Factory Pattern. A FactoryBean is a pattern to encapsulate interesting object construction logic in a class. The FactoryBean interface provides a way to customize the Spring IoC container's instantiation logic. You can implement this interface for objects that are themselves factories. Beans implementing FactoryBean are auto-detected.
The definition of this interface is as follows:
public interface FactoryBean<T> { T getObject() throws Exception; Class<T> getObjectType(); boolean isSingleton(); }
As per the preceding definition of this interface, the dependency injection using the FactoryBean and it causes getObject() to be invoked transparently. The isSingleton() method returns true for singleton, else it returns false. The getObjectType() method returns the object type of the object returned by the getObject() method.