Scrivio Interview library
ConceptFoundations5 min read · updated 12 November 2026

What is a Spring FactoryBean, how is it different from a @Bean factory method, and what does the & prefix in getBean("&myFactory") return?

A FactoryBean<T> is a bean whose job is to produce another object: the container calls its getObject() method and exposes the product under the FactoryBean's bean name. getBean("&myFactory") returns the FactoryBean itself instead of its product. Today @Bean methods cover most cases more simply, but frameworks still use FactoryBean to hide complex construction, for example LocalContainerEntityManagerFactoryBean and the Spring Data repository factories.

Why this question is asked

Every Spring Boot application with JPA contains FactoryBeans, even if its authors have never written one. When we inject an EntityManagerFactory or a Spring Data repository interface, a FactoryBean built that object. Interviewers ask this question to see whether we understand the slightly unusual rule that a bean name can refer to a product rather than to the bean itself, and why the & prefix exists. In this answer, we will recap what a factory is in Spring, write a small FactoryBean, see what getBean returns with and without the &, compare it with a @Bean method, and look at the two methods that control caching and type matching.

1A Quick Recap: Beans That Make Other Beans

Normally a bean definition describes the object we want: Spring instantiates the class and the instance is the bean. Sometimes, though, creating an object takes many steps, or the type we want to expose is an interface whose implementation is decided at runtime. For those cases Spring lets a bean act as a factory, an object whose job is to produce the object other beans really need.

FactoryBean<T> is the interface for that. It has getObject(), which returns the product, getObjectType(), which says what type the product will be, and isSingleton(), which says whether the container may cache the product. When the container sees that a bean implements FactoryBean, it treats the bean name specially: asking for that name gives the product, not the factory.

2Writing a Small FactoryBean

Here is a FactoryBean that builds a configured HTTP client. The construction is hidden inside getObject().

Java
public class ApiClientFactoryBean implements FactoryBean<ApiClient> {
private String baseUrl;
public void setBaseUrl(String baseUrl) { this.baseUrl = baseUrl; }
@Override public ApiClient getObject() {
return ApiClient.builder().baseUrl(baseUrl).timeout(Duration.ofSeconds(2)).build();
}
@Override public Class<?> getObjectType() { return ApiClient.class; }
@Override public boolean isSingleton() { return true; } // also the default
}
@Configuration
class ClientConfig {
@Bean ApiClientFactoryBean ordersClient() {
ApiClientFactoryBean f = new ApiClientFactoryBean();
f.setBaseUrl("https://orders.internal");
return f;
}
}

Notice that the @Bean method returns the factory, yet the bean named ordersClient that other beans receive is an ApiClient. A class that asks for ApiClient in its constructor gets the product without knowing a factory was involved.

3What getBean Returns, With and Without &

The & prefix is how we reach the factory itself when we need it, for example to inspect its settings or in tests.

Java
Object a = context.getBean("ordersClient");
System.out.println(a.getClass().getSimpleName()); // ApiClient: the product
Object b = context.getBean("&ordersClient");
System.out.println(b.getClass().getSimpleName()); // ApiClientFactoryBean: the factory
System.out.println(context.getBean("ordersClient") == context.getBean("ordersClient")); // true, isSingleton() is true
Step throughResolving a FactoryBean name
CallerContainerApiClientFactoryBean
getBean("ordersClient")
bean is a FactoryBean, name has no &
getObject() (once, if isSingleton is true)
ApiClient
the ApiClient product
getBean("&ordersClient")
the ApiClientFactoryBean itself

7 messages in order. Play them, or step through them one at a time.

So the plain name means the product, and &name means the factory. The reason for the prefix is that both objects share one name in the container's registry, and Spring needed a way to tell which one we mean. The last line shows the effect of isSingleton(): the container calls getObject() once and caches the product. If isSingleton() returned false, every getBean call would call getObject() again and return a new ApiClient.

4How a @Bean Method Compares

A @Bean method is also a factory, just a simpler one: Spring calls the method and the return value is the bean. For our client, the whole FactoryBean shrinks to this.

Java
@Bean
ApiClient ordersClient() {
return ApiClient.builder().baseUrl("https://orders.internal").timeout(Duration.ofSeconds(2)).build();
}

There is no extra interface, the return type tells the container the bean type, and scope and laziness come from annotations such as @Scope and @Lazy. For application code, this is what we should write today. FactoryBean dates from the XML era, when there was no way to run arbitrary Java code to build a bean, so a factory class with setters was the way to wrap complex construction.

5Where FactoryBean Is Still Used

Frameworks still use FactoryBean because it lets a library ship a reusable, configurable factory that hides many steps behind one bean definition. LocalContainerEntityManagerFactoryBean scans entities, configures the JPA provider and exposes an EntityManagerFactory. Spring Data registers a repository factory bean for each repository interface, such as JpaRepositoryFactoryBean, which generates the implementation at startup; that is how an interface with no implementation becomes injectable.

The two other methods matter more in these framework cases. getObjectType() lets the container answer the question "is there a bean of type OrderRepository?" without building the product, which would be expensive or even premature during startup. isSingleton() decides whether the product is cached; it defaults to true in the interface, which is what we want almost always.

6Follow-up questions

What is the difference between FactoryBean and BeanFactory?

BeanFactory is the container itself, the root interface for getting beans out of Spring, and ApplicationContext extends it. FactoryBean is a single bean that produces another object. The names are similar, but one is the whole registry and the other is one entry in it.

Can we inject the FactoryBean itself into another bean?

Yes. Declare the dependency with the FactoryBean's own type, for example ApiClientFactoryBean, and Spring injects the factory. Declaring the product type, ApiClient, injects the product. With a qualifier by name we can also use @Qualifier("&ordersClient") to ask for the factory.

Does a FactoryBean's product get post-processed?

The container applies BeanPostProcessor's after-initialisation step to the product, so for example an auto proxy creator can still wrap it. The product does not go through dependency injection or init callbacks of its own, because the container did not create it; the factory is responsible for building it completely.

7Conclusion

In this answer, we saw that a FactoryBean is a bean whose getObject() result is exposed under the bean's name, while getBean("&name") returns the factory itself. getObjectType() lets the container match types without creating the product, and isSingleton() decides whether the product is cached. For our own code, a @Bean method is the simpler factory and covers most cases, but frameworks still use FactoryBean, as LocalContainerEntityManagerFactoryBean and the Spring Data repository factory beans show.

What to say in the interviewAbout 80 seconds aloud

Open with

A FactoryBean is a bean that produces another object. It implements FactoryBean of T with three methods: getObject returns the product, getObjectType tells the container what type the product is, and isSingleton says whether the product can be cached, which defaults to true.

Then, if they want more

The unusual part is the naming rule. If I register a FactoryBean as ordersClient, then getBean of ordersClient, or injecting by the product type, gives me the product, not the factory. If I want the factory itself, I ask for getBean of ampersand ordersClient; the prefix exists because both share one name in the registry.

Compared with a @Bean method, FactoryBean is the older and heavier mechanism. A @Bean method is also a factory: Spring calls the method and the return value is the bean, its return type gives the bean type, and scope comes from annotations. So for application code I write @Bean methods.

FactoryBean still matters for frameworks that want a reusable, configurable factory that hides complex construction. LocalContainerEntityManagerFactoryBean builds the EntityManagerFactory, and Spring Data registers a repository factory bean per repository interface to generate the implementation. getObjectType matters there because the container can match types during startup without building the product early.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is a Spring FactoryBean, how is it different from a @Bean factory method, and what...": https://scrivio-interview-library.netlify.app/spring-boot/ioc-container/46bb620772/