Bean Definition in Application Context

A bean definition describes how the Spring container should create, configure, and manage a bean. The `ApplicationContext` uses bean definitions as metadata when building and managing the application's object graph.

A bean definition is configuration metadata held by the Spring container. It can specify the bean's class, scope, dependencies, initialization and destruction behavior, and other configuration details.

The `ApplicationContext` reads or receives bean definitions and uses them to create bean instances when needed. The definition is not the bean object itself; it is the container's recipe and management information for that object.

Bean definitions can come from several configuration mechanisms, including component scanning, Java configuration, and XML configuration. For example, `@Component` can cause a class to be registered as a bean, while `@Bean` declares that a configuration method produces a bean.

During dependency injection, the container consults bean definitions to determine which objects to create and how their dependencies should be supplied. This lets application code depend on managed objects without manually constructing the entire object graph.

A useful distinction is that a bean definition describes management metadata, while a bean is the runtime object created according to that metadata.

Key points

Interview questions

How does a bean definition participate in dependency injection?

The container uses bean definitions to understand which managed objects exist and what dependencies they require. When creating a bean, it resolves those dependencies from other managed beans and supplies them through the configured injection mechanism. This allows Spring to construct an object graph rather than requiring application code to manually instantiate dependencies.

What is a bean definition in Spring?

A bean definition is metadata that tells the Spring container how a bean should be created and managed. It can describe the bean's class, dependencies, scope, and lifecycle configuration. The definition is separate from the object instance itself. The container uses this metadata when constructing and managing beans.

Where do bean definitions come from in a Spring application?

Bean definitions can be registered through component scanning, Java configuration, XML configuration, and programmatic registration mechanisms. With component scanning, classes such as `@Component` can be discovered and registered. With Java configuration, methods annotated with `@Bean` can provide managed objects. These mechanisms ultimately provide metadata that the container uses to manage beans.

Does a bean definition represent an actual object in memory?

No. A bean definition is configuration metadata, whereas the bean is the runtime object created and managed according to that metadata. For a singleton bean, the container normally maintains one shared instance within the relevant container. Other scopes can change how and when instances are created.

Why is understanding bean definitions useful when debugging Spring applications?

Many Spring startup and injection problems ultimately involve incorrect or missing bean metadata. A bean may not exist because scanning did not discover its class, a configuration method was not registered, or conditional configuration prevented registration. Ambiguity can also arise when multiple definitions match the same dependency. Thinking in terms of definitions helps distinguish registration problems from problems involving the runtime bean instance.