@Inject and @Autowired both annotations are used in Java and Spring do achieve Dependency Injection respectively. Please read our blog Dependency Injection for better understanding.
@Inject
is part of the Java CDI (Contexts and Dependency Injection) standard introduced in Java EE 6 (JSR-299). Basically, Classes typically acquire references to other Classes through dependency injection. The dependency injection mechanism is completely type safe and CDI allows us to use Dependency Injection using @Inject
annotation. @Inject
identifies a point at which a dependency on a Java class or interface can be injected and the container then provides the needed resource.
E.g
public class Login { @Inject Credentials credentials; } or class Login { private Credentials credentials; @Inject Login (Credentials credentials) { this.credentials = credentials; } }
After doing this, the container will inject the Credentials bean into Login bean.
Along with @Inject
there are other annotations as well that help us to achieve Dependency Injection.
@Inject
. Identifies injectable constructors, methods, and fields.@Qualifier
. Identifies qualifier annotations. Qualifiers are strongly-typed keys that help distinguish different uses of objects of the same type.@Scope
. Identifies scope annotations.@Singleton
. Identifies a type that the injector only instantiates once@Named
. AString
-based qualifier.
@Autowired
annotation comes from Spring 2.5 world. All the things that we can do with @Inject
can be done in Spring using @Autowired
and then it becomes Spring containers’s job to resolve dependencies for classes. Spring @Autowired also comes with one more optional attribute ‘required‘. To handle the situation in which there is no wiring beans are available we set required
attribute to false
. But what do I mean by required
attribute. From the Spring doc.
“By default, the autowiring fails whenever zero candidate beans are available; the default behavior is to treat annotated methods, constructors, and fields as indicating required dependencies. This behavior can be changed by setting the required attribute to false”. E.g: @Autowired(required=false)
. In simple terms, “The required
attribute indicates that the property is not required for autowiring purposes, the property is ignored if it cannot be autowired“.
E.g
@Autowired(required=false)
private Party person;
By Spring 3.0, Spring offers support for JSR-330 dependency injection annotations (@Inject
, @Named
, @Singleton
).
The only difference between @Autowired
and @Inject
is that @Autowired
has required
attribute.
Note:
By Spring 4.3 release. If a bean has single constructor, we can omit @Autowired
annotation. But there are some cases like below.
1. When single constructor is present and setter is marked with @Autowired annotation, than both constructor & setter injection will be performed one after another:
@Component
public class Employee {
private Address address;
public Employee(Address address) {
this.address = address; //Called first
}
@Autowired
public void setAddress(Address add) {
this.address = add; //Called second
}
}
2. At the other hand, if there is no @Autowire at all, then object will be injected once via constructor and setter can be used in it’s common way without any injections.
Hope this clears you doubt!!