What is the purpose of the @Primary annotation in Spring?

  • Define a bean's name
  • Define a bean's scope
  • Mark a bean as deprecated
  • Specify the primary bean
The purpose of the @Primary annotation in Spring is to specify the primary bean when multiple beans of the same type exist. When multiple beans qualify as dependencies for injection, the one marked with @Primary is the one that Spring will choose by default. This is useful in scenarios where you have multiple implementations of an interface or class, and one should be considered the primary choice.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *