Interview Question: What’s the Relationship Between BeanFactory and FactoryBean?
This article explains the distinction between Spring's BeanFactory and FactoryBean, covering their roles, core APIs, typical implementations, key differences in registration and retrieval, classic use cases, common pitfalls, and frequent interview follow‑up questions, with concrete code examples and comparison tables.
BeanFactory – core IoC container interface
BeanFactory is the root interface of Spring’s IoC container. It defines core APIs such as getBean(String name), getBean(Class<T>), containsBean, isSingleton. In practice developers usually work with its sub‑interface ApplicationContext, which adds event publishing, internationalisation, resource loading and eager singleton pre‑instantiation.
Relevant sub‑interfaces: BeanFactory – only “get bean”. ListableBeanFactory – adds batch operations like getBeansOfType(). HierarchicalBeanFactory – supports parent‑child containers. ApplicationContext – full‑featured implementation. DefaultListableBeanFactory – most complete concrete class.
FactoryBean – special bean that acts as a factory
When a bean’s creation logic is too complex for a simple @Bean method, Spring encourages implementing FactoryBean. The interface declares:
public interface FactoryBean<T> {
T getObject() throws Exception;
Class<?> getObjectType();
default boolean isSingleton() { return true; }
}Example implementation that creates a JDBC Connection:
@Component
public class MyFactoryBean implements FactoryBean<Connection> {
@Override
public Connection getObject() throws Exception {
String url = "jdbc:mysql://localhost:3306/mydb";
return DriverManager.getConnection(url, "root", "123456");
}
@Override
public Class<?> getObjectType() { return Connection.class; }
}Injecting the produced object:
@Service
public class UserService {
@Autowired
private Connection connection; // receives the object returned by getObject()
}Key distinction – what is registered vs what you obtain
Ordinary bean: getBean("myService") returns the MyService instance.
FactoryBean: registering MyFactoryBean makes getBean("myFactoryBean") return the object produced by getObject() (e.g., Connection), not the factory itself.
To obtain the factory instance, prepend the & prefix: getBean("&myFactoryBean") returns the MyFactoryBean object. The prefix is defined by the constant FACTORY_BEAN_PREFIX = "&" in BeanFactory.
If FactoryBean.isSingleton() returns false, each getBean() call invokes getObject() and creates a new instance.
Classic use cases in the Spring ecosystem
MyBatis‑Spring – SqlSessionFactoryBean creates a SqlSessionFactory.
Spring‑Cloud‑OpenFeign – FeignClientFactoryBean creates a Feign proxy.
Spring Transaction – TransactionProxyFactoryBean creates a transaction proxy.
Spring AOP – ProxyFactoryBean creates an AOP proxy.
These frameworks hide complex creation logic behind a simple bean name.
Common pitfalls
The FactoryBean itself is a regular Spring bean and is fully managed (dependency injection, lifecycle callbacks). When other beans request it via getBean(), Spring returns the product of getObject(), not the factory.
Developers sometimes forget the & prefix needed to retrieve the factory instance.
Typical interview follow‑up questions
Difference between BeanFactory and ApplicationContext: the former is the minimal container with lazy bean creation; the latter is a superset that adds event publishing, internationalisation, AOP auto‑proxying and eager singleton pre‑instantiation.
Effect of FactoryBean.isSingleton() returning false: each getBean() call creates a new object because Spring does not cache the result.
Why use FactoryBean instead of @Bean: it isolates complex creation logic, can be processed by BeanPostProcessor, supports generic type inference and is suitable for framework‑level abstractions such as MyBatis or Feign.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Architect Handbook
Focused on Java interview questions and practical article sharing, covering algorithms, databases, Spring Boot, microservices, high concurrency, JVM, Docker containers, and ELK-related knowledge. Looking forward to progressing together with you.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
