Scrivio Interview library
DebuggingIntermediate7 min read · updated 12 November 2026

A custom FactoryBean returns null from getObjectType(), and autowiring by type of its product fails or creates the bean too early during startup. Explain why, and how static and instance factory methods in bean definitions compare.

The container uses getObjectType() to decide whether a FactoryBean's product matches a requested type without building the product. When it returns null, Spring either has to instantiate the FactoryBean early to find out, which drags its dependencies into creation too soon, or it skips the bean, and the by-type injection finds no match. The fix is to return a concrete type or, for generic factories, set FactoryBean.OBJECT_TYPE_ATTRIBUTE on the bean definition.

Why this question is asked

This bug usually appears in home-grown infrastructure code: a generic FactoryBean that creates clients or proxies for any interface, written so that the type is only known after construction. Startup then fails with a missing bean, or log lines show beans being created in a strange order. Interviewers use it to test whether we know how the container matches types before beans exist, which is the same knowledge AOT and native images rely on. In this answer, we will recap how type matching works, reproduce both failures, fix them, and then compare static and instance factory methods, including why BeanFactoryPostProcessor beans need static @Bean methods.

1A Quick Recap of Type Matching Before Creation

When a bean asks for a dependency of type InventoryClient, Spring has to find every bean definition that could produce an InventoryClient. Many of those beans do not exist yet; startup is still in progress. For an ordinary class, the definition already says the class, so the answer is easy. For a @Bean method, Spring reads the declared return type.

A FactoryBean is harder. The definition says ApiClientFactoryBean, but the bean that others receive is whatever getObject() returns. To answer the question without building that product, Spring asks the FactoryBean's getObjectType(). If the FactoryBean itself has not been created yet, Spring first looks for the type in the bean definition, in its generic signature, or in a special attribute; only if all of those fail does it consider creating the FactoryBean just to call getObjectType().

2Reproducing the Problem

Here is a generic factory that creates a client for any interface, with the interface chosen by configuration.

Java
public class RemoteClientFactoryBean implements FactoryBean<Object> {
private Class<?> clientInterface; // set from configuration
private final HttpTransport transport; // a dependency
public RemoteClientFactoryBean(HttpTransport transport) { this.transport = transport; }
public void setClientInterface(Class<?> c) { this.clientInterface = c; }
@Override public Object getObject() { return RemoteProxies.create(clientInterface, transport); }
@Override public Class<?> getObjectType() { return clientInterface; } // null until the setter runs
}
@Service
class StockService {
StockService(InventoryClient inventory) { } // injected by type
}
// Either:
// NoSuchBeanDefinitionException: No qualifying bean of type 'InventoryClient' available
// or log lines showing HttpTransport and RemoteClientFactoryBean created
// long before the rest of the infrastructure, during type matching

Notice that the class is declared as FactoryBean<Object>, so the generic signature tells Spring nothing. And getObjectType() reads a field that is only set after the FactoryBean is created and configured, so on an unconfigured instance it returns null.

3Why It Fails or Creates Beans Too Early

Spring has two ways out when it cannot learn the type from the definition. If the lookup allows eager initialisation, Spring creates the FactoryBean right there to ask it. Creating it means resolving its constructor arguments, so HttpTransport and everything HttpTransport needs are created too, possibly before the BeanPostProcessors that should proxy them are registered. That is the source of log messages such as "Bean 'httpTransport' is not eligible for getting processed by all BeanPostProcessors". If the lookup does not allow eager initialisation, for example during some internal phases, Spring simply skips the bean, and if no other candidate exists, injection fails with No qualifying bean.

Even when Spring creates the FactoryBean, the result can still be null if the property that decides the type is not yet applied, and null means "I do not know", which never matches InventoryClient. So the same code can fail in one application and work in another, depending on the order in which other beans happen to ask.

Step throughHow Spring finds a FactoryBean's product type
  1. Type in the bean definition attribute?
    YesUse it, nothing createdNoCheck the generic signature
  2. Generic type resolvable, e.g. FactoryBean<InventoryClient>?
    YesUse itNoAsk the FactoryBean itself
  3. Eager init allowed for this lookup?
    YesCreate the FactoryBean now and call getObjectType()NoSkip this bean: no match
  4. getObjectType() returned a type?
    YesMatch against itNoTreated as unknown: no match

4 steps in order. Play them, or step through them one at a time.

Every step after the first two either creates beans early or loses the match.

4The Fix: Make the Type Known Up Front

For a factory with one product type, the fix is to declare it: implement FactoryBean<InventoryClient> and return InventoryClient.class. For a generic factory used for many interfaces, we tell Spring the type on the bean definition, with the attribute that Spring checks first.

Java
// Registering one client per interface, for example in a registrar
RootBeanDefinition def = new RootBeanDefinition(RemoteClientFactoryBean.class);
def.getPropertyValues().add("clientInterface", InventoryClient.class);
def.setAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE, InventoryClient.class); // a Class or ResolvableType
registry.registerBeanDefinition("inventoryClient", def);
// and getObjectType() never returns null for a configured instance:
@Override public Class<?> getObjectType() { return clientInterface; }
// StockService now gets its InventoryClient; no early creation in the log

Now Spring answers the type question from the definition alone, so nothing is created early and the match never depends on ordering. Spring Data uses this same attribute for its repository factory beans.

5Static and Instance Factory Methods Compared

FactoryBean is one way to delegate construction; factory methods are the other. A static factory method is a static method on a class: in XML we wrote class="Clients" factory-method="inventory", and Spring calls Clients.inventory() with no instance of Clients. An instance factory method is a method on another bean: factory-bean="clientFactory" factory-method="inventory", so Spring must create clientFactory first and then call the method on it. A @Bean method in a @Configuration class is an instance factory method, and the configuration class is the factory bean.

That difference matters for BeanFactoryPostProcessor beans, which run very early to modify bean definitions before any normal bean is created. If such a post-processor is declared with a non-static @Bean method, Spring must create the whole configuration class to call it, too early for that class's @Autowired fields and @Value annotations to be processed. Declaring the method static avoids creating the configuration class at all.

Java
@Configuration
class PlaceholderConfig {
@Value("${app.region}") String region; // would be null if the class were created early
@Bean
static PropertySourcesPlaceholderConfigurer placeholders() { // static: no PlaceholderConfig instance needed
return new PropertySourcesPlaceholderConfigurer();
}
}

Spring logs a warning when it sees a non-static @Bean method returning a BeanFactoryPostProcessor, which is a useful clue when debugging. The same advice applies to BeanPostProcessor beans, for a similar reason.

6Why This Matters More With AOT

AOT processing, Spring's build-time step for faster startup and GraalVM native images, inspects bean definitions during the build and generates code that registers them. It needs to know each bean's type at that point, because it cannot create beans at build time just to ask them. A FactoryBean whose type is only known after creation gives AOT nothing to work with, and in a native image there is no reflection fallback at runtime for types it did not see. So a precise getObjectType(), a precise generic signature or the OBJECT_TYPE_ATTRIBUTE is not only good manners on the JVM; it is required for native images.

7Follow-up questions

Why does Spring not just create every FactoryBean early to be safe?

Because creating a FactoryBean creates its dependencies, and those may be created before the BeanPostProcessors that should wrap them, so they lose proxies for transactions, security or metrics. It also slows startup and can fail when dependencies are not ready. Spring therefore tries the cheap, side-effect-free ways first and creates the FactoryBean only as a last resort.

What does a NullBean or a null getObject() mean?

If getObject() returns null, Spring registers a special NullBean placeholder for the product, and injecting it into a required dependency fails. This is different from getObjectType() returning null: one means the product is empty, the other means the type is unknown. Both are signs that the factory was not configured when it was asked.

Is the OBJECT_TYPE_ATTRIBUTE public API?

Yes, it is a constant on the FactoryBean interface, and since Spring Framework 6 it accepts a Class or a ResolvableType, which lets us describe generic products such as Repository<Order, Long>. Check your version for the accepted value types if you target an older release.

8Conclusion

In this answer, we saw that Spring matches a FactoryBean's product type before creating the product, first from the bean definition attribute, then from the generic signature, and only then by creating the FactoryBean and calling getObjectType(). A null result either forces early creation, with dependencies built before their post-processors, or produces no match at all. Returning a concrete type or setting FactoryBean.OBJECT_TYPE_ATTRIBUTE fixes both. We also compared static factory methods, which need no instance, with instance factory methods such as @Bean methods, saw why BeanFactoryPostProcessor @Bean methods must be static, and why AOT makes precise types essential.

What to say in the interviewAbout 90 seconds aloud

Open with

The container has to decide which bean definitions can produce an InventoryClient while most beans do not exist yet. For a FactoryBean, the product type comes from three places in order: the FactoryBean.OBJECT_TYPE_ATTRIBUTE on the bean definition, the generic signature such as FactoryBean of InventoryClient, and finally getObjectType on an actual instance.

Then, if they want more

Our factory is declared as FactoryBean of Object and getObjectType reads a field that is set later, so it returns null.

Spring then either creates the FactoryBean early to ask it, which creates its dependencies before the BeanPostProcessors are ready and causes the not eligible for all BeanPostProcessors warnings, or, when eager initialisation is not allowed, skips it, and autowiring by type fails.

The fix is to return a concrete type, or for a generic factory to set OBJECT_TYPE_ATTRIBUTE when registering each definition, which is what Spring Data does. On factory methods: a static factory method is called on a class without an instance, while an instance factory method, including any @Bean method, needs its factory bean, the configuration class, created first.

That is why @Bean methods returning BeanFactoryPostProcessors must be static, otherwise the configuration class is created too early and loses its @Value injection. With AOT and native images this all matters more, because types must be known at build time.

About this answer

Last updated
12 November 2026
Versions it names
Spring Framework 6
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 "A custom FactoryBean returns null from getObjectType(), and autowiring by type of its...": https://scrivio-interview-library.netlify.app/spring-boot/ioc-container/948da0f9c4/