Difference Between @Resource, @Autowired and @Inject in Spring Injection
source link: http://adnjavainterview.blogspot.com/2023/09/difference-between-resource-autowired-inject-spring-injection.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Difference Between @Resource, @Autowired and @Inject in Spring Injection
In this post, we will learn the difference between Resource, Autowired and Inject annotation. All these annotations are used to inject the bean dependencies. Resource and Inject annotations are defined in Java and Autowired annotation defined in Spring framework.
@Resource – It's defined in the javax.annotation package and it's part of java.
@Inject – It's defined in the javax.inject package and it's part of Java
@Autowired – It's defined in the package org.springframework.bean.factory and part of Spring framework.
Internally @Inject and @Autowired annotations use the AutowiredAnnotationBeanPostProcessor to inject the dependencies. But @Resource annotation internally use CommonAnnotationBeanPostProcessor to inject the dependencies. Execution path/step is same for both Inject and Autowired annotations, Resource annotation execution paths also same but order of execution paths are not same.
- @Inject and @Autowired
- @Resource
public interface Notification { // some methods }
import org.springframework.stereotype.Component; @Component public class Email implements Notification { // some methods }
import org.springframework.stereotype.Component; @Component public class SMS implements Notification { // some methods }
@Resource @Qualifier("invalid") private Notification email; @Autowired @Qualifier("invalid") private Notification email; @Inject @Qualifier("invalid") private Notification email;
org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.prj.basics.notification.Notification]
@Resource @Qualifier("email") private Notification email; @Autowired @Qualifier("email") private Notification email; @Inject @Qualifier("email") private Notification email;
@Resource private Email email; @Autowired private Email email; @Inject private Email email;
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK