How to use the @Inject attribute in Java

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.

Java dependency injection
Java dependency injection

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.

How to use the @Inject attribute

Add the dependency injection framework to our project:

  1. 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.

  2. 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.

  3. Finally, we need to configure dependencies in the DI framework. This usually involves specifying the class to inject and the implementation to use.

Use @Inject on fields

Here's an example of how to use @Inject on a field:

public class Car {
@Inject
private 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.

Use @Inject on constructors

Here's an example of how @Inject can be used on a constructor:

public class Car {
private Engine engine;
@Inject
public 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.

Example

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;
@Inject
public 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:

  • The command to compile the program:
    javac -cp javax.inject-1.jar MyApp.java
    
  • The command to run the program:
    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...");
    }
}
Playground for @Inject attribute in Java

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved