DEV Community

MAYUR KUMBHAR
MAYUR KUMBHAR

Posted on • Updated on

Bean vs Component in Spring

@Bean in Spring

  1. more than one instance of class can be added to spring context
  2. we can create bean of any class, from libraries classes as well as user defined classes
  3. need to write specific code methods to create the instances
  4. developer is in control of how to create & configure the objects
  5. spring will create the beans based on the instructions, configuration specified by the developer

@Component in Spring

  1. only one instance of class can be added to spring context
  2. only user defined class can be marked as component to instantiate as bean, library class objects can not instantiated
  3. only the @Component annotation is required to add on class, no more code is required
  4. developer will not have control on how to create & configure bean
  5. spring framework is in charge of creating the bean & post that developer will have access to bean

Sometimes you want to create bean based on the certain conditions are met. Developer can create the objects manually and register them to spring context with help of context.registerBean("<name_of_bean>", <class_of_bean>.class, <Supplier>) basic method. There are multiple ways you can register a bean.

Top comments (0)