The @Bean annotation tells Spring that a method in a configuration class produces an object to register as a bean in the ApplicationContext. You use it when you need explicit control over how a bean is built, especially for classes you do not own, such as a DataSource, an ObjectMapper, or a third-party client. The method name becomes the bean name and the return type becomes its type.
@Bean is the Java equivalent of a <bean> element in old XML configuration, and it complements stereotype annotations such as @Component and @Service, which Spring detects by scanning your own classes.
@Bean methods register beans@Bean versus @Component@Bean methods register beansPut @Bean methods inside a class annotated with @Configuration. Spring calls each method once (for singleton scope), keeps the returned object, and makes it available for injection.
Here priceCalculator declares a Clock parameter, so Spring injects the clock bean. Method parameters are the preferred way to wire dependencies between @Bean methods because they work in every configuration style.
The main reason to choose @Bean over @Component is that you cannot annotate a library class. With @Bean you create and customize it in plain Java:
The same applies when construction needs conditional logic, several steps, or values read from properties.
By default the bean name is the method name. You can set one or more names, a scope, and init or destroy methods through the annotation attributes.
| Concern | How to set it with @Bean |
|---|---|
| Name | @Bean("name") or @Bean(name = {...}); defaults to the method name |
| Scope | @Scope("prototype"), @RequestScope, and so on; default is singleton |
| Init callback | initMethod attribute, or @PostConstruct on the class |
| Destroy callback | destroyMethod attribute; Spring infers close or shutdown by default |
| Conditional registration | @ConditionalOnProperty, @Profile, @ConditionalOnMissingBean |
| Primary candidate | @Primary on the method |
@Bean versus @Component| Aspect | @Bean | @Component and stereotypes |
|---|---|---|
| Placed on | A method in a configuration class | A class |
| Discovery | Explicit, declared in code | Classpath scanning |
| Third-party classes | Yes | No, you cannot edit the class |
| Construction logic | Anything you can write in Java | Constructor injection only |
| Typical use | Infrastructure and library beans | Your own services, repositories, controllers |
Use stereotypes for your own application classes, and @Bean for everything that needs custom construction.
A @Bean method inside a @Configuration class runs in full mode: Spring generates a CGLIB subclass so that calling one @Bean method from another returns the existing singleton rather than creating a second instance.
If you declare @Bean methods in a plain @Component class, or set @Configuration(proxyBeanMethods = false), Spring uses lite mode: there is no proxy, and calling repo() directly creates a new object. With proxyBeanMethods = false you should inject dependencies as method parameters. Spring Boot's own auto-configuration uses this form to start faster and to be friendlier to GraalVM native images.
@Bean be used without @Configuration?Yes. It works in lite mode on any Spring-managed class, but inter-bean method calls then create new instances, so inject dependencies through parameters.
The name of the annotated method. Use the name or value attribute to change it or to add aliases.
@Bean make a singleton?By default, yes. Use @Scope to change it, for example to prototype for a new instance on every request for the bean.
Add @ConditionalOnMissingBean (Spring Boot) to the method. It is the standard pattern for overridable defaults in auto-configuration.
@Bean registers the return value of a method as a Spring bean, giving you full control over construction, naming, scope, and lifecycle. Use it for third-party and infrastructure objects, and use @Component stereotypes for your own classes. Inside @Configuration classes Spring proxies the methods so singletons are preserved; with proxyBeanMethods = false or lite mode, wire dependencies through method parameters.