@Bean Method in Spring

A `@Bean` method explicitly tells the Spring container how to create and register an object as a bean. It is commonly used inside `@Configuration` classes when you want to control bean construction or register a type you do not create with a stereotype annotation.

A `@Bean` annotation marks a method whose return value should be managed by the Spring container. During application startup, Spring invokes the method and registers the resulting object in the application context, making it available for dependency injection.

The method's return type usually describes the bean exposed to other components, while the method body contains its construction logic. This is useful when creating third-party objects, configuring libraries, or when construction requires explicit setup that is not naturally expressed with `@Component`.

A common pattern is to place `@Bean` methods in a class annotated with `@Configuration`. Spring processes that configuration and makes the returned objects available for injection, for example through constructor injection.

By default, Spring beans are singleton-scoped, so a bean created by a `@Bean` method is normally reused from the application context rather than recreated for every injection. Other scopes can be configured when different lifecycle behavior is required.

Key points

Interview questions

Can a `@Bean` method have dependencies?

Yes. A `@Bean` method can declare parameters that Spring resolves from the application context. This allows one bean definition to depend on another managed bean. For example, a service client bean could receive a configured HTTP client as a method parameter.

What scope does a `@Bean` use by default?

The default scope of a Spring bean is singleton. That means the application context normally maintains one bean instance for that bean definition. If another lifecycle is required, a different scope can be specified. Scope determines how instances are created and managed by the container.

Why are `@Bean` methods useful for third-party libraries?

You generally cannot add Spring annotations to a class owned by an external library. A configuration class can instead expose an instance of that class through a `@Bean` method. The method can also perform required setup, configure constructor arguments, and return the fully initialized object. Spring then manages that object like other beans.

What does `@Bean` do in Spring?

`@Bean` tells Spring that the object returned by an annotated method should be registered in the application context. Spring can then manage that object and inject it into other Spring-managed components. The annotation is commonly placed on methods inside configuration classes. It is particularly useful when you need explicit control over object construction.

When would you use `@Bean` instead of `@Component`?

Use `@Component` when you control the class and want Spring to discover it through component scanning. Use `@Bean` when you want to define the construction of an object explicitly, such as a class from a third-party library. `@Bean` also lets the configuration method customize how the object is initialized. Both approaches ultimately allow Spring to manage the resulting object.