Differences in @inject, @Autowired, @Resource annotations under Spring

Source: Internet
Author: User

1. @Inject

    • Javax.inject
    • JSR330 (Dependency injection for Java)

This is the specification in JSR330, which is implemented by the Autowiredannotationbeanpostprocessor class by dependency injection.

@Inject use

The following is the use of @inject, without adding @named annotations, you need to configure the same as the variable name.

@Inject @named ("MONGO") Private Mongo Mongo;

2. @Autowired

    • Org.springframework.bean.factory
    • Spring

@Autowired is the annotation provided by spring, which is interchangeable with @inject through dependency injection implemented by the Autowiredannotationbeanpostprocessor class.

Use of @Autowired

@Autowired has a property of required, which can be configured to False, and if it is configured to False, the system will not throw a mistake when the corresponding bean is not found.

@Autowired Private Mongotemplate mongotemplate;

3. @Resource

    • Javax.annotation
    • JSR250 (Common Annotations for Java)

This is the implementation of the JSR250 specification, @Resource implementation of dependency injection through the Commonannotationbeanpostprocessor class.

Use of @Resource

@Resource will typically specify a name attribute, as follows:

@Resource (name = "Usermapper")private usermapper usermapper;

Differences between 4 and three annotations

@Autowired and @inject are basically the same, because both use Autowiredannotationbeanpostprocessor to handle dependency injection. But @resource is an exception, and it uses commonannotationbeanpostprocessor to handle dependency injection. Of course, both are beanpostprocessor.

@Autowired and @inject

The default autowired by type, you can explicitly specify autowired by qualifier name through @qualifier.

@Resource

The default autowired by field name, if autowired by field name fails, is degraded to autowired by type and can be explicitly specified by @qualifier autowired by qualifier name If autowired by qualifier name fails, it is degraded to autowired by field name. However, if autowired by field name fails, it will no longer be degraded to autowired by type.

5. Summary

In the use of individuals, more emphasis on the use of @inject, which is the implementation of the JSR330 specification, and @autowired is the implementation of spring, if not spring generally do not use this, and @resource is the implementation of JSR250, which is the norm for many years ago.

Reference:

http://blog.csdn.net/u012734441/article/details/51706504 (the above content is transferred from this article)

Differences in @inject, @Autowired, @Resource annotations under Spring (RPM)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.