The @Inject
annotation is a Java label that indicates that a class or field should be instantiated and initialized by the dependency injection framework. It is used with dependency injection frameworks like Spring, Google Guice, or CDI to specify the dependencies of classes.
When a class or field is annotated with @Inject
, the dependency injection framework automatically identifies the required dependencies and provides the instance. @Inject
allows for a more modular and flexible approach to managing dependencies in Java code. It decouples dependencies from implementation and allows easy switching between runs without changing the code that uses them.
@Inject
attributeAdd the dependency injection framework to our project:
The first step is to add the dependency injection framework to our Java project. We need to properly configure some popular DI frameworks, including Spring, Google Guice, and CDI, and add them as a dependency on our Project.
Once we have a DI framework, we can use the @Inject
annotation to mark fields or constructors for dependency injection. When creating objects, the DI framework looks for these annotations and handles the correct dependencies.
Finally, we need to configure dependencies in the DI framework. This usually involves specifying the class to inject and the implementation to use.
@Inject
on fieldsHere's an example of how to use @Inject
on a field:
public class Car {@Injectprivate Engine engine;public void start() {engine.start();}}
In the above example, the Car
class has an engine
field, annotated with @Inject
. This tells the dependency injection framework that the Car
class requires an instance of the Engine
class. The framework will then find the implementation of the Engine
class and inject it into the Car
instance.
@Inject
on constructorsHere's an example of how @Inject
can be used on a constructor:
public class Car {private Engine engine;@Injectpublic Car(Engine engine) {this.engine = engine;}public void start() {engine.start();}}
In this example, the Car
class has a constructor that takes an instance of the Engine
class as a parameter. The constructor is annotated with @Inject
, which tells the dependency injection framework that the Car
class requires an instance of the Engine
class. The framework will then find the implementation of the Engine
class and inject it into the Car
instance.
Overall, the @Inject
attribute is a powerful Java tool that helps reduce coupling and increase flexibility in our code by allowing the dependency injection framework to handle the instantiation and management of dependencies.
A simple example of using @Inject
:
import javax.inject.Inject;public class MyApp {public static void main(String[] args) {MyService service = new MyService(new MyDependency());service.doSomething();}}class MyService {private final MyDependency myDependency;@Injectpublic MyService(MyDependency myDependency) {this.myDependency = myDependency;}public void doSomething() {myDependency.doSomethingElse();}}class MyDependency {public void doSomethingElse() {System.out.println("Doing something else...");}}
In this example, we have a MyApp
class that creates a new instance of MyService
and calls its doSomething()
method. MyService
depends on MyDependency
, which is injected using constructor injection with the @Inject
annotation.
When we run the MyApp
class, we will see the output "Doing something else..." printed to the console, which comes from the doSomethingElse()
method of MyDependency.
We need to run the following command to run the program in the terminal:
javac -cp javax.inject-1.jar MyApp.java
java MyApp
Now let's practice the @Inject
attribute and see the output by clicking the "Run" button:
import javax.inject.Inject; public class MyApp { public static void main(String[] args) { MyService service = new MyService(new MyDependency()); service.doSomething(); } } class MyService { private final MyDependency myDependency; @Inject public MyService(MyDependency myDependency) { this.myDependency = myDependency; } public void doSomething() { myDependency.doSomethingElse(); } } class MyDependency { public void doSomethingElse() { System.out.println("Doing something else..."); } }
Free Resources