Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between @Inject and @Autowired
@Inject and @Autowired both annotations are used for autowiring in your application.
@Inject annotation is part of Java CDI which was introduced in Java 6, whereas @Autowire annotation is part of spring framework. Both annotations fulfill same purpose therefore, anything of these we can use in our application.
| Sr. No. | Key | @Inject | @Autowired |
|---|---|---|---|
| 1 |
Basic |
It is part of Java CDI |
It is part of Spring framework |
| 2 |
Required |
It has no required attribute |
It has required attribute |
| 3 |
Default Scope |
Default scope of the autowired beans is Singleton |
Default scope of the inject beans is prototype |
| 4 |
Ambiguity |
In case of ambiguity in beans for injection then @Named qualifier should be added in your code. |
In case of ambiguity in beans for injection then @Qualifer qualifier should be added in your code. |
| 5 |
Advantage |
It is a part of Java CDI so it is not dependent on any DI framework. It makes your system loosely coupled. |
It makes your application tightly coupled with Spring framework. In the future , if you want to move to another DI framework then you need reconfigure your application. |
Example of @Injection annotation
public class InjectionExample {
@Inject
private CarBean carbean;
}
Example of @Autowired annotation
public class AutowiredExample {
@Autowired
private CarBean carbean;
}Advertisements