Sobes.tech
Junior — Middle

What is the difference between a Spring component and a bean, and what are their main features?

sobes.tech AI

Answer from AI

In the context of Spring, a bean is an object managed by the Spring IoC container. A component is a special annotation (@Component) that indicates that a class should be automatically detected and registered as a bean.

Main features:

  • @Component is a way to mark a class for automatic scanning and registration in the container.
  • Bean is any object that Spring creates and manages its lifecycle, including components, services (@Service), repositories (@Repository), and controllers (@Controller).

Thus, a component is one of the ways to create a bean. All components are beans, but not all beans are necessarily marked as components (for example, beans created manually via @Bean).