Factory pattern in Spring — ApplicationContext as a factory
Fast recall
Quick revision
Core answer
BeanFactory and ApplicationContext locate and create bean definitions, resolve dependencies, and manage lifecycle. Most application code receives beans through injection rather than calling the container as a service locator. FactoryBean is Spring's explicit extension point for custom object creation; an @Bean method is another container-managed creation recipe.Interview-ready guide
Interview answer
Spring's IoC container has factory responsibilities. BeanFactory and ApplicationContext read bean definitions, create the selected implementation, resolve dependencies, and manage scope and lifecycle. In normal application code I use constructor injection rather than calling getBean, because direct container lookup hides dependencies and behaves like a service locator. When construction itself is special, Spring provides FactoryBean, whose getObject() supplies the product, and @Bean methods provide container-managed creation recipes. These ideas are factory-like, though I would not label every Spring mechanism as an exact textbook Factory Method implementation.
Factory pattern in Spring — ApplicationContext as a factory
Direct Answer
Spring's container performs factory responsibilities: BeanFactory and ApplicationContext locate and create bean definitions, resolve dependencies, and manage lifecycle. Most application code receives beans through injection rather than calling the container as a service locator. FactoryBean<T> is Spring's explicit extension point for custom object creation; an @Bean method is another container-managed creation recipe.
Deep Dive & Architecture
Spring's IoC container separates application code from bean construction. BeanFactory is the core bean-access contract, while ApplicationContext adds broader application services. Bean definitions and configuration tell the container which type to create, how to resolve its dependencies, and which lifecycle and scope rules apply.
Application classes normally use constructor injection. Calling applicationContext.getBean(...) throughout business code turns the container into a service locator and hides dependencies.
For special construction, a FactoryBean<T> is itself a managed bean whose getObject() supplies another object. An @Bean method also acts as a configuration recipe invoked by the container. These mechanisms have factory-like roles, but not every Spring creation feature is a literal textbook GoF Factory Method implementation.
Code Example
@Configuration
class AppConfig {
@Bean // a factory method: Spring calls it to create the bean
PaymentProcessor paymentProcessor() { return new StripePaymentProcessor(); }
@Bean
NotificationService notificationService() {
var s = new EmailNotificationService();
s.setRetryCount(3); // factory handles complex init
return s;
}
}
@Service
class OrderService {
private final PaymentProcessor processor;
// The container (a factory) creates PaymentProcessor and injects it.
OrderService(PaymentProcessor processor) { this.processor = processor; }
void placeOrder(Order order) { processor.process(order.getTotal()); }
}
// The container resolves the whole graph for you:
var context = new AnnotationConfigApplicationContext(AppConfig.class);
var orderService = context.getBean(OrderService.class); // Factory in action
Common Mistakes & Pitfalls
- Not knowing
BeanFactoryexists.ApplicationContextextends it;BeanFactoryis the actual factory interface. - Thinking
@Beanis magic. It's a factory method — Spring calls it, and you control the creation logic inside. - Calling
newinside beans.new UserRepository()bypasses the container, so Spring can't inject or manage it. - Ignoring scope. Singleton vs prototype is the factory deciding whether to cache and reuse or create fresh.
Check your understanding
Answer these without looking back at the article.
- What is the Factory design pattern?
- How do you implement a Factory Method pattern in Java?
- What are the drawbacks of overusing the Factory pattern?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.