Scrivio Interview library
DesignAdvanced6 min read · updated 12 November 2026

An internal starter needs to adjust bean definitions contributed by other auto-configurations, for example marking certain beans lazy. Would you use a BeanFactoryPostProcessor? What are the risks?

A BeanFactoryPostProcessor can change bean definitions, so marking other auto-configurations' beans lazy is technically possible. But it ties our starter to other libraries' bean names and internal structure, which change between versions; it must be declared static and cannot use regular beans or bound @ConfigurationProperties; and it complicates AOT processing and debugging. I would only use it as a last resort and prefer documented extension points: properties, customiser beans, @ConditionalOnMissingBean overrides and Spring Boot's own lazy-initialisation support.

Why this question is asked

Platform teams that own an internal starter often want to adjust what other auto-configurations contribute: make some beans lazy to speed up startup, change a scope, or switch off a bean they consider wasteful. A BeanFactoryPostProcessor gives full access to every definition, so it can do all of that. Interviewers ask this design question to see whether we can separate what is possible from what is maintainable. In this answer, we will see how such a post-processor would look, go through its risks one by one, look at how it interacts with AOT, and then at the documented alternatives and how to choose between them.

1What the Post-Processor Would Look Like

A BeanFactoryPostProcessor runs once, after all bean definitions are loaded and before any regular bean is created. A bean definition is the recipe for a bean, and setting its lazy flag means Spring will not create it at startup. So the starter could do this:

Java
public class CompanyLazyBeans implements BeanFactoryPostProcessor {
private static final Set<String> LAZY = Set.of("jacksonObjectMapper", "meterRegistryPostProcessor");
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory factory) {
for (String name : factory.getBeanDefinitionNames()) {
if (LAZY.contains(name)) {
factory.getBeanDefinition(name).setLazyInit(true); // other libraries' beans, by name
}
}
}
}
@AutoConfiguration
class CompanyStartupAutoConfiguration {
@Bean
static CompanyLazyBeans companyLazyBeans() { return new CompanyLazyBeans(); } // must be static
}

It works in the sense that the flag is set. Notice what it depends on: exact bean names chosen by other projects, and the assumption that making those particular beans lazy is safe. The second name in the list is a good example of the danger, because some infrastructure beans are post-processors themselves, and post-processors are always created eagerly regardless of the lazy flag, so the change silently does nothing.

2Risk 1: Depending on Other Libraries' Internals

Bean names, definition classes and the structure of auto-configurations are implementation details of each library. They are not part of a documented contract, and they change: a bean is renamed, split in two, moved into a different auto-configuration or replaced by a factory. Our post-processor then either does nothing, which is a silent regression, or changes the wrong bean, which may break something far away.

Laziness itself also has semantics that other libraries may rely on. A bean that registers a listener, starts a scheduler or validates configuration in its constructor does that work only if it is created. Making it lazy can mean the listener is never registered. The library author never promised that their bean works when lazy, so we are stepping outside the tested configurations.

3Risk 2: It Lives Before Everything Else

A BeanFactoryPostProcessor runs before the bean post-processors that handle @Autowired, @Value and @PostConstruct are registered. So it must be declared with a static @Bean method, or Spring has to create the whole configuration class early and logs a warning that annotations in it will not be processed. And it cannot rely on regular beans: injecting one would create it before any bean post-processor exists, so it would miss injection and proxies.

This includes @ConfigurationProperties. Those beans are bound by a bean post-processor, so at this stage they do not exist or are not bound. If the list of lazy beans should be configurable, we have to read it from the Environment ourselves, for example by implementing EnvironmentAware and using Spring Boot's Binder.

Java
public class CompanyLazyBeans implements BeanFactoryPostProcessor, EnvironmentAware {
private Set<String> lazy = Set.of();
@Override public void setEnvironment(Environment env) {
lazy = Binder.get(env).bind("company.startup.lazy-beans", Bindable.setOf(String.class))
.orElse(Set.of()); // company.startup.lazy-beans=foo,bar
}
// postProcessBeanFactory as before, using 'lazy'
}

4Risk 3: AOT and Debugging

With Spring AOT, used for GraalVM native images and for faster JVM startup, the bean factory is prepared at build time: factory post-processors run during the build and the resulting definitions are written out as generated code. A post-processor that reads environment properties then makes its decision with the build's environment, not the runtime one, and the decision is frozen into the generated code. Depending on the version, factory post-processors may not run again at runtime in AOT mode (check your version), so a property change in production would have no effect.

Debugging also suffers. A developer who sees a bean behaving lazily searches their own code and the library's documentation and finds nothing, because the change happens in a starter they did not write, by name, through an API almost nobody reads. The condition evaluation report and actuator endpoints show the beans but not that a post-processor changed their definitions.

5The Documented Alternatives

Most goals have a supported way. For laziness specifically, Spring Boot already has spring.main.lazy-initialization, and it offers LazyInitializationExcludeFilter beans to keep chosen beans eager, so a starter can ship a sensible exclusion set instead of editing definitions. For changing how a library's bean is configured, the library's properties and customiser beans (Jackson2ObjectMapperBuilderCustomizer, RestClientCustomizer and many others) are the contract the authors maintain. For replacing a bean, auto-configurations use @ConditionalOnMissingBean, so defining our own bean of that type makes theirs back off. For removing a whole feature, spring.autoconfigure.exclude switches off an auto-configuration class by name, which is a public, documented name. (Spring Boot 4 with Jackson 3 uses different Jackson builder and customiser types; check your version.)

Java
// Keep message listeners eager when a service turns on lazy initialisation
@Bean
static LazyInitializationExcludeFilter eagerListeners() {
return LazyInitializationExcludeFilter.forBeanTypes(MessageListenerContainer.class);
}
// Replace a default instead of editing its definition (Spring Boot 3, Jackson 2)
@Bean
ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) { // auto-configured one backs off
return builder.build().registerModule(new CompanyModule());
}
Step throughChoosing how a starter adjusts other auto-configurations
  1. Is there a property for it?
    YesSet the property default in the starterNoLook for a customiser
  2. Is there a customiser or exclude filter bean?
    YesProvide that beanNoConsider replacing the bean
  3. Does the auto-configuration back off on a missing bean?
    YesDefine our own bean of that typeNoAsk upstream for a hook
  4. Last resort: a BeanFactoryPostProcessorstatic, tested per version, documented, switchable

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

Notice that every one of these is a hook the library authors know about and test. When they change their internals, they keep the hook working, which is precisely the guarantee our post-processor lacks.

6Follow-up questions

If you had to ship the post-processor anyway, how would you limit the risk?

Make it opt-in or switchable with a property, match on bean types rather than names where possible, log at startup every definition it changed, and add a test per supported Spring Boot version that fails if an expected bean is missing or renamed. And keep a ticket to replace it when a supported hook appears.

Why does setting lazy on a BeanPostProcessor definition not work?

Because Spring creates all bean post-processors eagerly during refresh, before ordinary beans, regardless of their lazy flag: it needs them in place to process everything else. So the change has no effect, which is a good example of the internal knowledge such a post-processor needs.

Is BeanDefinitionRegistryPostProcessor any safer?

No, it is more powerful and so more risky. It can add and remove definitions, which makes it suitable for frameworks that register beans from their own metadata, but for adjusting other libraries' beans it has the same dependence on their internals.

7Conclusion

In this answer, we saw that a BeanFactoryPostProcessor can mark other auto-configurations' beans lazy, but at a cost. It depends on bean names and internals that change between versions, it must be static and cannot use regular beans or bound @ConfigurationProperties, and it freezes decisions into AOT-generated code while being hard for others to debug. Documented extension points, properties, customisers, LazyInitializationExcludeFilter, @ConditionalOnMissingBean overrides and auto-configuration exclusions, achieve the same goals with a contract the library authors maintain.

What to say in the interviewAbout 80 seconds aloud

Open with

Technically a BeanFactoryPostProcessor can do it, since it runs after all definitions are loaded and before any regular bean exists, so it can call setLazyInit on any definition.

Then, if they want more

But I would treat it as a last resort. The first risk is that it depends on other libraries' bean names and internal structure, which are not a contract and change between versions, so it either silently stops working or changes the wrong bean, and lazy beans that register listeners or validate configuration may stop doing that.

Some beans, like post-processors, are always eager anyway. Second, it runs before injection works, so it must be declared with a static @Bean method and cannot use regular beans or @ConfigurationProperties; I would have to read configuration through EnvironmentAware and the Binder.

Third, with AOT the decision is made at build time and frozen into generated code, and for developers it is very hard to discover why a bean behaves differently. So I would prefer the documented hooks: spring.main.lazy-initialization with LazyInitializationExcludeFilter beans, library properties, customiser beans, defining our own bean so @ConditionalOnMissingBean backs off, or spring.autoconfigure.exclude.

If I still had to ship it, I would make it switchable, log every change and test it against each supported Boot version.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 3, Spring Boot 4
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 "An internal starter needs to adjust bean definitions contributed by other...": https://scrivio-interview-library.netlify.app/spring-boot/bean-lifecycle/4c4d7bb772/