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;
No comments:
Post a Comment