Spring Beans @Component vs @Bean

What is the difference between @Component and @Bean.

I received a question from a student in my Spring Boot Introduction course that I would like to share with you. 

Dan uses @Service annotation for the NotificationService to add it to the application context yet the User class doesn’t need any annotation to be added to the class itself.  He just used the @Bean annotation in the SpringBeansApplication class for it to be added to the application context.  Why is that? 

Spring Beans

This is actually something that a lot of people are confused by. In this article, we will look at the difference between @Component (Spring Stereotype Annotations) and @Bean.

What are Spring Stereotype Annotations?

Before we get into the differences between @Bean and @Component I think it’s important that we understand what @Component is. I could break this down for you here but luckily for me, I already wrote up a nice long post on what Spring Stereotype Annotations are and how to use them. Please read this article first and then come back and we will break this down. 

Spring Stereotype Annotations

I hope you enjoyed that article and I hope you have a better understanding of the different annotations. 

@Component

If we mark a class with @Component or one of the other Stereotype annotations these classes will be auto-detected using classpath scanning. As long as these classes are in under our base package or Spring is aware of another package to scan, a new bean will be created for each of these classes. 

There’s an implicit one-to-one mapping between the annotated class and the bean (i.e. one bean per class). Control of wiring is quite limited with this approach since it’s purely declarative. 

It is also important to note that the stereotype annotations are class level annotations. 

@Bean

@Bean is used to explicitly declare a single bean, rather than letting Spring do it automatically like we did with @Controller. It decouples the declaration of the bean from the class definition and lets you create and configure beans exactly how you choose.

With @Bean you aren’t placing this annotation at the class level. If you tried to do that you would get an invalid type error. The @Bean documentation defines it as:

Indicates that a method produces a bean to be managed by the Spring container.

Typically, @Bean methods are declared within @Configuration classes. In this example, we have a user class that we needed to instantiate and then create a bean using that instance. This is where I said earlier that we have a little more control over how the bean is defined. 

The name of the method is actually going to be the name of our bean. If we pull up the /beans endpoint in the actuator we can see the bean defined. 

 

Conclusion

I hope that cleared up some things on when to use @Component and when to use @Bean. It can be a little confusing but as you start to write more applications it will become pretty natural. 

Hi, I’m Dan Vega. I am a Software Engineer living just outside of one of my favorite places in the world, Cleveland Ohio. I am a self-taught programmer who firmly believes that you can accomplish anything in this life if you put your mind to it, roll up your sleeves and are willing to put in the work.

I have been writing software for over 17+ years now and I use my knowledge, passion, and influence to help other developers achieve their goals. I teach online and currently, have over 20,000 students.

When I am not writing software I have a number of things that really keep me busy. I love to read books, listen to podcasts and watch online courses. I have a real thirst for knowledge and it consumes me every single day of the week. I also love to run & lift heavy weights which keep me balanced throughout the week.

Please note: I reserve the right to delete comments that are offensive or off-topic.

  • Mark Tiahrt

    Thanks for the break down on the @Bean vs Stereotype annotations. This brings more light to the confusion I had between the two.