The @Bean annotation tells Spring that a method produces an object the container must create, manage and make available for injection. You put it on a method inside a @Configuration class, and the method's return value is registered as a bean named after the method. It is the standard way to register objects you cannot annotate yourself, such as third-party classes, or objects that need custom construction logic.
Unlike @Component, which marks your own class for scanning, @Bean works at the method level and gives you full control over how the instance is built.
When the application context starts, Spring processes every @Configuration class, calls each @Bean method once (for singleton scope) and stores the result under a bean name.
The bean clock has type Clock. The priceService method declares a Clock parameter, and Spring injects the bean defined above. The method name is the default bean name; set another with @Bean("utcClock") or @Bean(name = {"utcClock", "clock"}) to add aliases.
| Aspect | @Bean | @Component (and @Service, @Repository) |
|---|---|---|
| Placed on | A method | A class |
| Discovery | Explicit, in a configuration class | Component scanning |
| Third-party classes | Yes | No, you cannot edit their source |
| Custom construction | Full control | Constructor injection only |
| Conditional registration | Easy per method | Needs @Conditional on the class |
Use @Component for your own application classes and @Bean for library types, factories and anything that needs setup code.
Beans are singletons by default. Add @Scope to change that, and use initMethod and destroyMethod for lifecycle hooks.
Spring infers a public no-argument close() or shutdown() method as the destroy method automatically. Set destroyMethod = "" to turn that off. For your own classes, @PostConstruct and @PreDestroy (from jakarta.annotation) are an alternative.
A class annotated with @Configuration is proxied with CGLIB so that calling one @Bean method from another returns the existing singleton rather than creating a second instance.
This inter-bean call is safe only in full mode. If you set @Configuration(proxyBeanMethods = false), or put @Bean methods in a plain @Component class (lite mode), the call creates a new object. Spring Boot uses proxyBeanMethods = false in its own auto-configuration for faster startup; prefer method parameters for dependencies, as in the first example, because they work in every mode.
Combine @Bean with conditions to register an object only when it makes sense.
@ConditionalOnMissingBean is a Spring Boot annotation and is the basis of "back-off" behaviour: your own bean replaces the auto-configured one.
@Bean methods that return a BeanFactoryPostProcessor or BeanDefinitionRegistryPostProcessor static, so they are created early before the configuration class itself.@Bean methods must not be private or final in a proxied configuration class, because CGLIB must override them.NoUniqueBeanDefinitionException; mark one @Primary or inject with @Qualifier.Object) hides the real type from injection points; declare the most specific useful type.Yes, in lite mode, for example in a @Component class. Inter-bean method calls are then plain Java calls and are not intercepted.
The method name. Override it with the name or value attribute.
Yes. Spring resolves each parameter from the context, so this is the preferred way to wire dependencies.
Yes. Use @Scope("prototype") or another scope for a new instance per request.
@Bean registers the return value of a method as a Spring-managed bean. Use it for third-party types, custom construction and conditional registration, pass dependencies as method parameters, and rely on @Component scanning for your own classes.