The @Bean annotation marks a method in a Spring configuration class as the factory for a bean. Spring calls the method, takes the returned object and registers it in the application context, where it can be injected anywhere it is needed. It replaces the old XML bean element and lets you register objects that you cannot annotate, such as classes from libraries.
This page focuses on the practical uses: defining beans, wiring dependencies between them, naming and aliasing, and choosing between @Bean and component scanning.
A @Bean method lives in a class annotated with @Configuration. The method's return type becomes the bean type and its name becomes the bean name.
You can then inject the bean by type.
The context bootstraps like this when you run it manually:
In a Spring Boot application the configuration class is found by component scanning, so you rarely create the context yourself.
Declare the dependency as a method parameter. Spring resolves it from the context before calling your method.
This approach works in every configuration mode and makes dependencies visible in the signature. Calling orderRepository(dataSource) directly from another @Bean method also returns the shared singleton in a full @Configuration class, but parameters are clearer and safer.
The default bean name is the method name. Use the attribute to choose a different name or several aliases.
When several beans share a type, disambiguate with @Primary on one of them or @Qualifier("primaryMailer") at the injection point. Without either, Spring throws NoUniqueBeanDefinitionException.
| Setting | How to set it | Default |
|---|---|---|
| Scope | @Scope("prototype"), @Scope("request") | singleton |
| Init callback | @Bean(initMethod = "start") | none |
| Destroy callback | @Bean(destroyMethod = "stop") | inferred close() or shutdown() |
| Lazy creation | @Lazy | eager for singletons |
| Dependency order | @DependsOn("other") | by injection graph |
Use @Component, @Service and @Repository for classes you own and that need no special setup. Choose @Bean when:
Large applications split beans over several classes and combine them with @Import.
Spring Boot also lets you set @Configuration(proxyBeanMethods = false) when your beans do not call each other directly; that skips CGLIB proxying and speeds up startup.
It registers the object returned by a method as a bean managed by the Spring container, with its lifecycle and injection handled for you.
Not strictly. Without it the methods run in lite mode and calls between them are not intercepted, so use @Configuration when beans call each other.
You can, but @Component with scanning is shorter when no custom construction is needed.
More than one bean matches the injected type. Mark one @Primary or add a @Qualifier.
@Bean turns a method's return value into a Spring-managed bean. Define it in a @Configuration class, pass dependencies as parameters, name or qualify beans when types clash, and prefer it over @Component for library classes and conditional or customised construction.