Wednesday 24 May 2023

Difference between @Autowired and @Qualifier Annotation in Spring Framework? Example

In the Spring Framework, dependency injection plays a vital role in managing object dependencies. When working with Spring's dependency injection mechanism, two commonly used annotations are @Autowired and @Qualifier. While they both assist in resolving dependencies, they serve different purposes and have distinct functionalities. This article aims to explain the difference between @Autowired and @Qualifier annotations in the Spring Framework, along with examples to illustrate their usage.

@Autowired Annotation: The @Autowired annotation is used for automatic dependency injection. It allows Spring to automatically wire beans together by matching the required dependency type with the available beans in the container. Here are a few key points to understand about @Autowired:
  1. Dependency Resolution: @Autowired performs dependency resolution by type. When a bean with a matching type is found in the container, it is automatically injected into the dependent bean. If there are multiple beans of the same type available, Spring throws an exception. In such cases, @Qualifier can be used to specify the specific bean to be injected.
  2. Example Usage: Consider the following example, where we have a UserService interface and multiple implementations:
java
public interface UserService { void createUser(); } @Service("userService1") public class UserServiceImpl1 implements UserService { // Implementation details } @Service("userService2") public class UserServiceImpl2 implements UserService { // Implementation details } @Component public class UserController { @Autowired private UserService userService; // Other methods }

In this example, the @Autowired annotation is used to inject an instance of the UserService interface into the UserController class. Spring will automatically resolve the dependency by matching the type UserService with the available beans in the container. As there are two beans of UserService so spring will not able to decide which bean needs to be autowire that's why it will throw the NoUniqueBeanDefinitionException exception because spring doesn't know which bean should autowire.

@Qualifier Annotation: The @Qualifier annotation is used in conjunction with @Autowired to specify a specific bean to be injected when there are multiple beans of the same type. It helps Spring differentiate between beans with the same type. Here are a few key points to understand about @Qualifier:

  • Bean Identification: @Qualifier allows you to provide a unique identifier or name to the beans, which can be used for dependency resolution. It helps Spring determine the exact bean to be injected when there are multiple beans of the same type.
  • Example Usage: Let's modify the previous example to include @Qualifier:

java
@Component public class UserController { @Autowired @Qualifier("userService2") private UserService userService; // Other methods }


In this updated example, the @Qualifier("userService2") annotation is used alongside @Autowired to specify that the UserService bean with the identifier "userService2" should be injected into the UserController. This resolves any ambiguity when multiple beans of type UserService are available.

Conclusion: In summary, @Autowired and @Qualifier are annotations used for dependency injection in the Spring Framework. @Autowired performs automatic dependency resolution by type, while @Qualifier helps differentiate between beans of the same type by providing a unique identifier or name. By combining these annotations, you can achieve precise control over dependency injection, ensuring the correct beans are wired together in your application.

No comments:

Post a Comment

Seven front-end development trends in 2023-2024

With the increasing prevalence of apps in the digital landscape , the role of front-end developers remains essential. While apps aim to ove...

Popular Posts