1.

What is @Autowired annotation? How is @Qualifier used with it?

Answer»

@Autowired ANNOTATION is used to AUTOWIRE i.e. inject dependent bean on the constructor, setter method or a field/property. When @Autowired is used on dependency, the application context searches for a matching dependency and provides as required. This helps us to avoid writing EXPLICIT injection logic.

However, by default, all dependencies that are Autowired are required. So, in scenarios where a required dependency is not available or if there is conflict; it results in an exception like NoUniqueBeanDefinitionException.

There are a few options available to turn off the default behavior:

  1. By using (required=false) OPTION with @Autowired to MAKE it non-mandatory for a specific bean property.

@Autowired (required=false)

private Contract contractBean;

  1. By using @Qualifier, we can further qualify autowiring; in scenarios when two beans are created with the same name.

@Qualifier ("design")

private Contract contractBean;



Discussion

No Comment Found