Quick revision
The short answer
Use @Bean when we do not own the class, such as a third-party client or a DataSource, when construction needs logic or configuration values, when we need several beans of the same type, or when we want domain classes free of Spring annotations. Use @Component, through stereotypes like @Service, for our own application classes whose construction is simple constructor injection. @Bean methods can also carry conditions, which is exactly how Spring Boot's auto-configuration is built.
What to remember
In this answer, we saw that @Bean methods are the right choice for classes we do not own, for construction that needs logic or configuration values, for several beans of the same type, and for keeping domain classes free of Spring annotations. @Component suits our own application classes with simple constructor injection. @Bean methods also combine naturally with conditions, which makes them the basis of Spring Boot's auto-configuration.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Use @Bean when we do not own the class, such as a third-party client or a DataSource, when construction needs logic or configuration values, when we need several beans of the same type, or when we want domain classes free of Spring annotations. Use @Component, through stereotypes like @Service, for our own application classes whose construction is simple constructor injection. @Bean methods can also carry conditions, which is exactly how Spring Boot's auto-configuration is built.
Why this question is asked
Every Spring codebase mixes both styles, and the choice affects how readable and flexible the configuration is. A team that puts @Component on everything finds that it cannot create two differently configured instances of one class; a team that writes @Bean for everything ends up with huge configuration classes. Interviewers ask this to see whether we have a principle for choosing. In this answer, we will recap what each declaration does, go through the cases where @Bean is the right tool with code for each, see where @Component is simpler, and finish with conditions and auto-configuration.
1A Quick Recap of the Two Declarations
@Component goes on the class itself. Component scanning finds the class, and Spring creates the bean by calling its constructor, injecting constructor parameters from the container. The class decides that it is a bean, and Spring decides how to build it.
@Bean goes on a method in a configuration class. Spring calls the method, and whatever it returns becomes the bean, registered under the method's name. The class of the returned object knows nothing about Spring; the configuration method decides both that it is a bean and exactly how it is built. Both end up as ordinary beans in the same container, with the same injection, lifecycle and proxy support.
2When We Do Not Own the Class
We cannot put @Component on a class from a library: the source is not ours. Third-party clients, connection pools, serializers and SDK objects are therefore declared with @Bean methods. This is the most common reason for @Bean.
Notice that the builder call is code we control fully: which region, which credentials provider, which HTTP client. A @Component would have no place to express that.
3When Construction Needs Logic or Several Instances
Some objects need more than a constructor call: reading configuration, choosing an implementation, calling a builder, or validating input. A @Bean method is just Java, so it can do all of that. And because each @Bean method creates one bean, several methods returning the same type give several differently configured beans, something @Component cannot do since it registers one bean per class.
| Situation | Choose | Why |
|---|---|---|
| Third-party class | @Bean | we cannot annotate it |
| Built with a builder or from config values | @Bean | construction is code |
| Several beans of one type | @Bean | one method per bean |
| Domain class that should not know Spring | @Bean | no annotation on the class |
| Optional or conditional infrastructure | @Bean with @Conditional... | basis of auto-configuration |
| Our own service, plain constructor injection | @Component / @Service | less code, scanned |
Two RestClient beans now exist, distinguished by name, and the gateway implementation is chosen from configuration. Notice that each method uses clone() on the shared builder, because RestClient.Builder is mutable and we do not want the two clients to share settings by accident.
4Keeping Domain Classes Free of Spring
Some teams want the core business classes to have no framework annotations at all, for example in a hexagonal architecture where the domain module does not depend on Spring. With @Bean, the domain class is a plain Java class with a constructor, and the wiring lives in a configuration class in the application module. The domain can be tested, reused in a batch tool or moved to another framework without change.
This is a design choice rather than a rule. For most applications, @Service on a service class is perfectly fine and the dependency on one Spring annotation costs little. But when the separation matters, @Bean is how we achieve it.
5Where @Component Is the Better Choice, and Conditions
For our own application classes, controllers, services, repositories, event listeners, whose construction is just constructor injection, @Component and its stereotypes are simpler. The class declares itself, scanning picks it up, and there is no separate method to keep in sync with the constructor. Writing a @Bean method for each of fifty services would add code without adding information.
@Bean methods have one more strength: they combine naturally with conditions. @ConditionalOnMissingBean, @ConditionalOnProperty and @ConditionalOnClass on a @Bean method decide per bean whether it is registered. That is precisely how Spring Boot's auto-configuration is built: configuration classes full of conditional @Bean methods that step aside when the application defines its own bean. Conditions also work on @Component classes, but for infrastructure with defaults that users may override, conditional @Bean methods in auto-configuration are the standard pattern.
6Follow-up questions
Can a class have both @Component and be returned from a @Bean method?
It can, but then two beans of that type are registered, one from scanning and one from the method, which usually causes a NoUniqueBeanDefinitionException or a bean overriding error. Pick one declaration per class; if a class is created by a @Bean method, remove its stereotype annotation.
Do @Bean methods have to be in a @Configuration class?
No. They also work in a @Component class, in what Spring calls lite mode, but then calls between @Bean methods are plain Java calls and are not intercepted. In either case, taking dependencies as method parameters rather than calling other @Bean methods keeps behaviour correct.
Which is faster at startup?
The difference is small. @Component classes must be found by scanning, which costs a little time for large packages, while @Bean methods are found by parsing configuration classes. With Spring AOT both are turned into generated registration code, so the difference largely disappears.
7Conclusion
In this answer, we saw that @Bean methods are the right choice for classes we do not own, for construction that needs logic or configuration values, for several beans of the same type, and for keeping domain classes free of Spring annotations. @Component suits our own application classes with simple constructor injection. @Bean methods also combine naturally with conditions, which makes them the basis of Spring Boot's auto-configuration.
Open with
I use @Bean whenever the configuration method has something to say that the class itself cannot. The clearest case is a class I do not own, like an AWS SDK client, a connection pool or a Clock: I cannot annotate it, so a @Bean method builds it.
Then, if they want more
The second case is construction that needs logic or configuration values, like using a builder, choosing between a sandbox and a live implementation from a property, or validating settings.
Third, when I need several beans of the same type, for example two RestClients with different base URLs, because each @Bean method creates one bean, while @Component gives one bean per class; I then inject them with qualifiers.
Fourth, when I want domain classes to stay free of Spring, as in a hexagonal design, the wiring lives in configuration and the domain class is plain Java. For my own controllers, services and repositories with plain constructor injection, I use @Component through the stereotypes, because it is less code and the class documents itself.
And I would mention that @Bean methods combine naturally with conditions like @ConditionalOnMissingBean and @ConditionalOnProperty, which is exactly how Spring Boot's auto-configuration provides defaults that applications can override. The one thing to avoid is declaring the same class both ways, which registers two beans.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Core technologies
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "When should you declare beans with @Bean methods instead of annotating the class with...": https://scrivio-interview-library.netlify.app/spring-boot/ioc-container/c8668fb904/