Scrivio Interview library
ConceptFoundations5 min read · updated 12 November 2026

What is a BeanDefinition in Spring, and what information does it hold?

A BeanDefinition is the container's recipe for a bean: everything Spring needs to know to create it, without the bean itself. It holds the bean class or the factory method, the scope, the lazy flag, constructor arguments and property values, init and destroy methods, depends-on names, and the primary and autowire-candidate flags. Definitions come from component scanning, @Bean methods, XML or programmatic registration, and BeanFactoryPostProcessors can change them before any bean is created.

Why this question is asked

We rarely write a BeanDefinition by hand, yet it is the most central idea in the container. It explains why Spring can know about a bean long before creating it, how @Lazy or @Primary actually take effect, and how features such as property placeholders and Spring AOT work. Interviewers ask it to see whether we understand the container's two phases. In this answer, we will recap the idea of definitions versus instances, look at what a definition holds with a small program that prints one, see where definitions come from, and see how a post-processor can change them.

1A Quick Recap: Recipes Before Objects

The Spring container works in two phases. First it collects bean definitions, descriptions of the beans the application should have. Only later, during the refresh of the ApplicationContext, does it create the actual objects from those descriptions.

A good comparison is a recipe and a cake. The BeanDefinition is the recipe: which class to instantiate, what to pass to its constructor, how long it should live. The bean is the cake. Spring can read, list and even change recipes before baking anything, and that is where much of its flexibility comes from. The interface is org.springframework.beans.factory.config.BeanDefinition, and the usual implementations are RootBeanDefinition, GenericBeanDefinition and AnnotatedGenericBeanDefinition, which all extend AbstractBeanDefinition.

2What a Definition Holds

The easiest way to see it is to print one. Here a configuration class declares a bean with a few options, and we read its definition back from the bean factory.

Java
@Configuration
class AppConfig {
@Bean(initMethod = "open", destroyMethod = "close")
@Lazy @Primary @DependsOn("flyway")
@Scope("singleton")
ReportStore reportStore(DataSource ds) { return new ReportStore(ds); }
}
var ctx = new AnnotationConfigApplicationContext(AppConfig.class);
BeanDefinition bd = ctx.getBeanFactory().getBeanDefinition("reportStore");
System.out.println(bd.getFactoryBeanName()); // appConfig
System.out.println(bd.getFactoryMethodName()); // reportStore
System.out.println(bd.getScope()); // singleton
System.out.println(bd.isLazyInit()); // true
System.out.println(bd.isPrimary()); // true
System.out.println(Arrays.toString(bd.getDependsOn())); // [flyway]
System.out.println(bd.getInitMethodName()); // open
System.out.println(bd.isAutowireCandidate()); // true
Main fields of a BeanDefinition
FieldMeaningSet by
beanClassNameclass to instantiatescanning, XML class=, registerBean
factoryBeanName / factoryMethodNamecreate by calling a method@Bean methods, XML factory-method
scopesingleton, prototype, request, ...@Scope
lazyInitcreate on first use, not at refresh@Lazy
constructorArgumentValuesvalues for the constructorXML constructor-arg, programmatic
propertyValuesvalues for settersXML property, programmatic
initMethodName / destroyMethodNamelifecycle callbacks@Bean(initMethod=...)
dependsOnbeans to create first@DependsOn
primary / autowireCandidatechoice when several beans match@Primary, autowireCandidate=false

Notice that the bean class is not set directly here: a @Bean method is described by a factory bean name, the configuration class, and a factory method name. For a scanned @Component the definition holds the bean class name instead. The definition also holds constructor argument values and property values, used heavily by XML configuration and programmatic registration, the role, which marks infrastructure beans, and a description.

3Where Definitions Come From

Every way of declaring a bean ends up as a BeanDefinition in the bean factory's registry. Component scanning, run by ConfigurationClassPostProcessor, finds classes annotated with @Component or its stereotypes and registers a ScannedGenericBeanDefinition for each. @Bean methods in configuration classes become definitions with a factory method. XML files are read by XmlBeanDefinitionReader into GenericBeanDefinitions. And code can register definitions directly, through GenericApplicationContext.registerBean, a BeanDefinitionRegistryPostProcessor, or an ImportBeanDefinitionRegistrar.

Because they all produce the same kind of object, the rest of the container does not care where a bean was declared. Creation, injection, post-processing and proxies work identically for a bean from XML and a bean from a @Bean method.

Step throughFrom declaration to bean
  1. Declarations@Component, @Bean, XML, registerBean
  2. BeanDefinitions in the registryrecipes only
  3. BeanFactoryPostProcessorsmay read or change the recipes
  4. Refresh creates the beansusing each definition
  5. Beans in the containerready to inject

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

4Changing Definitions Before Beans Exist

Since definitions exist before any bean, a BeanFactoryPostProcessor can inspect and change them. That is exactly what PropertySourcesPlaceholderConfigurer does: it walks the definitions and replaces ${...} placeholders in their values. We can do the same for our own needs.

Java
@Component
class LazyReportsPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory bf) {
for (String name : bf.getBeanDefinitionNames()) {
BeanDefinition bd = bf.getBeanDefinition(name);
if (name.startsWith("report")) {
bd.setLazyInit(true); // reportStore, reportMailer: created on first use
}
}
}
}

Notice that the processor changes recipes, not objects. That is the line to respect: a BeanFactoryPostProcessor must not create ordinary beans, for example by calling getBean, because then those beans are built before the other post-processors run and miss injection or proxies. Changing objects after they are created is the job of a BeanPostProcessor instead.

This recipe-first design is also what makes Spring AOT possible. The AOT step reads the final definitions at build time and generates code that registers them, so the application does not have to rediscover them by scanning at every start.

5Follow-up questions

What is the difference between a BeanDefinition and a bean?

The definition is metadata, a description of how to create a bean; the bean is the object created from it. There is one definition per bean name, but a prototype-scoped definition produces a new bean every time it is requested, and a lazy definition may never produce a bean if nothing uses it.

What is a merged bean definition?

Definitions can inherit from a parent definition, a feature mostly used in XML with the parent attribute. Before creating a bean, Spring merges the child with its parents into a RootBeanDefinition that holds the final values. Annotation-based code rarely uses inheritance, but the merged form is what the creation code works with.

Can I see all bean definitions of a running application?

Yes. ConfigurableListableBeanFactory.getBeanDefinitionNames() lists them, and getBeanDefinition(name) returns each one. In Spring Boot, the Actuator beans endpoint shows the beans with their scope, type and dependencies, which is often enough for debugging.

6Conclusion

In this answer, we saw that a BeanDefinition is the container's metadata recipe for creating a bean. It holds the bean class or factory method, scope, lazy flag, constructor arguments and property values, init and destroy methods, depends-on, and the primary and autowire-candidate flags. Definitions come from component scanning, @Bean methods, XML or programmatic registration, and all end up in the same registry. BeanFactoryPostProcessors can change them before any bean is created, which is how placeholders, and our own customisations, work.

What to say in the interviewAbout 80 seconds aloud

Open with

A BeanDefinition is the recipe Spring keeps for each bean, separate from the bean object itself. The container first collects definitions and only later, during refresh, creates beans from them.

Then, if they want more

A definition holds either the bean class, for a scanned component, or a factory bean name and factory method name, for a @Bean method.

It also holds the scope, singleton or prototype or a web scope, the lazy-init flag, constructor argument values and property values, the init and destroy method names, the depends-on list, and the primary and autowire-candidate flags that decide which bean is chosen when several match.

Definitions come from component scanning, @Bean methods, XML through XmlBeanDefinitionReader, or programmatic registration with registerBean or a registry post-processor, and once registered, the container treats them all the same. The important consequence is that a BeanFactoryPostProcessor can read and change definitions before any bean exists.

PropertySourcesPlaceholderConfigurer does exactly that to resolve placeholders, and we can, for example, mark some beans lazy. The rule is that such a processor changes recipes and must not create ordinary beans. This separation is also what lets Spring AOT generate the registration code at build time.

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 BeanDefinition in Spring, and what information does it hold?": https://scrivio-interview-library.netlify.app/spring-boot/ioc-container/2077dd1b93/