Difference between @Inject and @Autowired and @Resource

Spring supports a variety of annotation methods for Bean dependency injection:
@Resource javax.annotationJSR250 (Common Annotations for Java)
@Inject javax.injectJSR330(Dependency Injection for Java)
@Autowired org.springframework.bean.factory Spring


Intuitively, @Autowired is an annotation provided by Spring, and the others are built-in annotations in JDK itself, and Spring also supports these annotations. But what is the difference between the three in use? After testing the method, the author found some interesting features. The

difference is summarized as follows:
1. @Autowired has a required attribute
that can be configured to false. In this case, if the corresponding bean is not found, an exception will not be thrown. @Inject and @Resource do not provide corresponding configuration, so they must be found or an exception will be thrown.

Second
, @Autowired and @Inject are basically the same because both use AutowiredAnnotationBeanPostProcessor to handle dependency injection,
but @Resource is an exception, it uses CommonAnnotationBeanPostProcessor to handle dependency injection.
Of course, both are BeanPostProcessors
quote
@Autowired and @Inject- default autowired by type, you can explicitly specify autowired by qualifier name through @Qualifier
If autowired by type fails (cannot find or find multiple implementations), it will degenerate to autowired by field name
@Resource- default autowired by field name- If autowired by field name fails, it will degenerate to autowired by type- You can explicitly specify autowired by qualifier name through @Qualifier. If autowired by qualifier name fails, it will degenerate to autowired by field name. But at this time, if autowired by field name fails, it will no longer degenerate to autowired by type.


https://zhidao.baidu.com/question/1306705027383695379.html

Understanding Spring-2.4 Java-based Standard Annotation Assembly-@Inject(3)-Injection through Constructor Method
http://blog.csdn.net/raylee2007/ article/details/50670490In

-depth understanding of various annotations in
springhttp ://www.cnblogs.com/digdeep/p/4525567.html The

difference between @Inject and @Autowired and @Resourcehttp
://blog.csdn.net/u012734441 /article/details/51706504

The difference between @Autowired and @Resourcehttp://blog.csdn.net/ssyan/article/details/6771568 The difference between

@ResponseBody

and @RequestBody annotationshttp
: //blog.csdn.net/qq_35246620/ article/details/59620858

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326274547&siteId=291194637
Recommended