Quick revision
The short answer
I would not keep the old FactoryBeans as they are, because FactoryBeans whose product type is only known at runtime are hard for AOT, which must know every bean type at build time. For a fixed set of beans, plain @Bean methods are simplest and AOT friendly; for one client per configured service, I would register bean definitions programmatically, with a BeanDefinitionRegistryPostProcessor or the BeanRegistrar API in Spring Framework 7, setting the target type explicitly. I would keep the starter's public contract stable, test AOT processing in CI, and release the change behind a flag or in a major version.
What to remember
In this answer, we saw why decade-old FactoryBeans with dynamic types complicate AOT: the build cannot create them, so it cannot learn their product types. For a fixed set of beans, @Bean methods are simplest; for clients driven by configuration, programmatic registration with a BeanDefinitionRegistryPostProcessor or Spring Framework 7's BeanRegistrar, with explicit target types, works on the JVM and under AOT. We also saw that AOT freezes configuration-driven beans at build time, and that a safe migration keeps the public contract stable, tests AOT in CI, and ships behind a flag before becoming the default in a major version.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would not keep the old FactoryBeans as they are, because FactoryBeans whose product type is only known at runtime are hard for AOT, which must know every bean type at build time. For a fixed set of beans, plain @Bean methods are simplest and AOT friendly; for one client per configured service, I would register bean definitions programmatically, with a BeanDefinitionRegistryPostProcessor or the BeanRegistrar API in Spring Framework 7, setting the target type explicitly. I would keep the starter's public contract stable, test AOT processing in CI, and release the change behind a flag or in a major version.
Why this question is asked
Internal starters outlive the people who wrote them. A starter that creates one HTTP client per configured service with a decade-old FactoryBean may have worked flawlessly on the JVM, but AOT and native images ask a new question of every bean: what exactly is your type, and can we know it during the build? Interviewers ask this design question to see whether we can weigh a framework change against the teams who depend on the library. In this answer, we will recap the current design and what AOT needs, compare the three options, choose one for configuration-driven clients, and plan a safe migration.
1A Quick Recap of the Current Starter
The starter reads configuration such as clients.orders.url and clients.billing.url, and for each entry registers a ClientFactoryBean. Each factory builds a client proxy for an interface, with retries, timeouts and metrics. Application teams simply inject OrdersClient or BillingClient.
AOT processing is a build-time step: Spring starts the application context in a special mode, evaluates conditions and bean definitions without creating beans, and generates Java code that registers exactly those definitions at runtime. For a native image, GraalVM then needs to know every class reached by reflection or proxies, and AOT generates those hints. Both steps need the bean's real type at build time.
2Why the Old FactoryBeans Are a Problem
A typical old factory is written as ClientFactoryBean implements FactoryBean<Object>, with getObjectType() returning a field set from configuration. During the AOT build Spring does not create the factory, so it sees only Object. It cannot generate correct type information, cannot register the proxy interfaces for the native image, and may resolve injection points differently than on the JVM. Some failures show up only when the native image runs, as missing proxy configuration or missing beans, long after the build passed.
There is a second, subtler issue. AOT freezes the bean definitions that exist at build time. If the set of clients depends on configuration, then the clients present at build time are the ones that will exist in the native image. That is a real change in behaviour for a starter whose whole point is to react to configuration, and it affects whichever option we choose.
3The Three Options Side by Side
Each option fits a different shape of problem, so it helps to put them next to each other.
| Option | Fits | AOT |
|---|---|---|
Keep FactoryBean<Object> | nothing new; legacy only | poor: type unknown at build time |
@Bean methods | a fixed, known set of clients | good: return type is the bean type |
| Programmatic registration | one client per configured entry | good if target type is set explicitly |
FactoryBean can be made AOT friendly too, by declaring precise types, but then it is just a heavier form of option three.4A Fixed Set: @Bean Methods
If the starter really provides a handful of well-known clients, @Bean methods are the simplest answer. The return type tells both the JVM container and AOT exactly what the bean is, conditions such as @ConditionalOnProperty decide whether each one exists, and the code reads like any other configuration.
5Configuration-Driven Clients: Programmatic Registration
Our starter creates a client per configured service, so the set is open-ended. Here we register one bean definition per entry. A BeanDefinitionRegistryPostProcessor, a hook that may add definitions before beans are created, works in all current versions. Spring Framework 7 adds the BeanRegistrar interface, imported with @Import, which offers a more compact API for exactly this job (check your version for the exact method signatures). Either way, the important line is the explicit target type.
Notice that every definition says its type before anything is created, and that construction goes through a named static factory method rather than a lambda. AOT generates Java code for each definition, and it can write a call to ClientFactory.create("orders"), but it cannot turn an arbitrary lambda instance supplier into source code (check your version for what it supports). Notice also that the registrar binds configuration at registration time; under AOT that happens during the build, so we must document that the client list is fixed into a native image. Teams that need new clients without rebuilding should stay on the JVM, or we provide a runtime registry bean that creates extra clients on demand.
6Migrating Without Breaking Teams
The starter's public contract is what teams use: property names, bean names, the injected interfaces and the customiser hooks. I would keep all of those identical, so changing the internals is invisible to them. A compatibility test suite captures this contract by starting a context with the old configuration and asserting the same beans, names and types exist.
In CI I would add a job that runs the AOT processing goal, process-aot in Maven or processAot in Gradle, on a sample application, then runs its tests in AOT mode, and ideally builds a native image nightly. I would ship the new registration behind a property such as clients.registration=v2 in a minor release so early adopters can try it, then make it the default in the next major version with release notes explaining the build-time freezing and how to switch back during the transition.
- Write contract tests against the old starterbean names, types, properties
- Implement registration with explicit target typesbehind
clients.registration=v2 - Add AOT processing and native tests to CIsample app, nightly native build
- Minor release: opt-in flag, gather feedback
- Major release: new default, old path deprecatedrelease notes explain build-time freezing
5 steps in order. Play them, or step through them one at a time.
7Follow-up questions
Could we just fix getObjectType() and keep the FactoryBeans?
Yes, as a first step. Declaring precise generics or setting FactoryBean.OBJECT_TYPE_ATTRIBUTE on each definition gives AOT the type it needs. It is a small, low-risk change that may be enough for the JVM and AOT on the JVM. It still leaves the older, more complex construction path in place, so I would treat it as the bridge, not the destination.
What does AOT do with @ConditionalOnProperty?
AOT evaluates conditions during the build, using the configuration available then, and generates code for the beans that passed. At runtime in AOT mode the conditions are not evaluated again. So a property that turns a client on or off must be set at build time for native images, which is the same freezing effect we saw for registration.
How do you test that teams can still override a client?
With ApplicationContextRunner we can start the auto-configuration with a user-defined OrdersClient bean and assert that the starter's bean backs off, then start it without one and assert the starter's bean exists. Running the same tests in the AOT job shows whether overriding behaves the same in both modes.
8Conclusion
In this answer, we saw why decade-old FactoryBeans with dynamic types complicate AOT: the build cannot create them, so it cannot learn their product types. For a fixed set of beans, @Bean methods are simplest; for clients driven by configuration, programmatic registration with a BeanDefinitionRegistryPostProcessor or Spring Framework 7's BeanRegistrar, with explicit target types, works on the JVM and under AOT. We also saw that AOT freezes configuration-driven beans at build time, and that a safe migration keeps the public contract stable, tests AOT in CI, and ships behind a flag before becoming the default in a major version.
Open with
I would move away from the old FactoryBeans, but the choice depends on the shape of the beans. The core problem is that those factories are typically FactoryBean of Object with a type read from configuration, and AOT never creates beans at build time, so it cannot learn the product type and cannot generate correct registrations or native image hints.
Then, if they want more
If the starter provides a fixed set of clients, I would simply write @Bean methods with conditions: the return type is the bean type and AOT handles it well.
Our starter creates one client per configured service, so I would register definitions programmatically, using a BeanDefinitionRegistryPostProcessor today or the BeanRegistrar API in Spring Framework 7, and always set the target type explicitly on each definition.
I would also be honest that AOT freezes configuration-driven beans at build time, so a native image contains the clients configured when it was built, and document that. To migrate safely, I would keep the public contract identical, the properties, bean names and interfaces, and lock it with contract tests using ApplicationContextRunner.
CI would run the process-aot step and the tests in AOT mode, with a nightly native build. I would ship the new path behind a property in a minor release, then make it the default in the next major version with clear release notes.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Framework 7
- 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.
- Official documentation
- Spring Framework reference, Core technologies
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 "You maintain an internal starter that creates a client per configured service using...": https://scrivio-interview-library.netlify.app/spring-boot/ioc-container/4ea99aa35f/