You are currently viewing Wiring in Spring : @Autowired, @Resource and @Inject

Wiring in Spring : @Autowired, @Resource and @Inject

Introduction

In Spring Framework, dependency injection is a key concept for achieving loose coupling and promoting maintainability and testability in your applications. To wire dependencies, Spring provides several annotations, including @Autowired, @Resource, and @Inject. In this tutorial, we’ll explore each of these annotations, their differences, and when to use them.

1. @Autowired:

The @Autowired annotation is used to automatically wire dependencies by type. When Spring encounters @Autowired annotation, it searches for a bean of the same type to inject. If there is exactly one bean of the type, it will be injected. If there are multiple beans of the same type, Spring will try to match by the name of the bean. @Autowired can be applied to fields, constructors, or setter methods.

Example:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MyComponent {

    private MyService myService;

    @Autowired
    public MyComponent(MyService myService) {
        this.myService = myService;
    }

    // Other methods using myService
}

In this example, MyComponent is annotated with @Component, indicating it is a Spring-managed bean. The constructor is annotated with @Autowired, allowing Spring to inject an instance of MyService automatically.

2. @Resource:

The @Resource annotation is used for wiring dependencies by name. It allows you to specify the name of the bean to inject. If no name is specified, it will use the default name, which is the name of the annotated field or method. @Resource can be applied to fields, setter methods, and constructor arguments.

Example:

import javax.annotation.Resource;
import org.springframework.stereotype.Component;

@Component
public class MyService {
    
    @Resource(name = "myBean")
    private SomeBean myBean;

    public void performAction() {
        myBean.doSomething();
    }
}

In this example, MyComponent is annotated with @Component. The field myService is annotated with @Resource, indicating that Spring should inject a bean named “myService”.

3. @Inject:

The @Inject annotation is a part of the Java Dependency Injection (JSR-330) standard. It is similar to @Autowired, as it is used for automatic dependency injection by type. It can be applied to fields, constructors, and setter methods.

Example:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.inject.Inject;

@Component
public class MyComponent {

    private MyService myService;

    @Inject
    public MyComponent(MyService myService) {
        this.myService = myService;
    }

    // Other methods using myService
}

In this example, MyComponent is annotated with @Component. The constructor is annotated with @Inject, indicating that Spring should inject an instance of MyService.

Differences:

  • @Autowired is a Spring-specific annotation, while @Resource and @Inject are part of the Java Dependency Injection (JSR-330) standard.
  • @Autowired and @Inject are primarily used for wiring dependencies by type, while @Resource is used for wiring dependencies by name.

Conclusion:

In conclusion, @Autowired, @Resource, and @Inject are annotations used for dependency injection in Spring applications. Each annotation has its use cases and behaviors, so understanding their differences is essential for effective dependency management in your Spring projects. Choose the appropriate annotation based on your requirements and coding standards.

I hope this tutorial clarifies the usage of @Autowired, @Resource, and @Inject annotations in Spring Framework. Feel free to experiment with these annotations in your own Spring projects to gain a better understanding of their capabilities.

Leave a Reply