Spring Boot @Autowired vs @Resource

By Arvind Rai, October 01, 2023
On this page we will learn the difference between @Autowired and Jakarta @Resource annotations in our Spring Boot application.
1. The Spring @Autowired and @Resource both annotations are used to resolve dependency injection.
2. The @Autowired annotation can be applied on fields, constructors, and methods. The @Resource annotation can be applied on fields and methods and not on constructors.
3. The @Autowired annotated method can have multi-arguments whereas @Resource annotated method can have only single argument.
4. The @Autowired as well as @Resource annotation can be applied on private fields, too.

@Autowired

Spring @Autowired annotation marks a constructor, field, setter method, or config method as to be autowired by dependency injection. Find its element.
required : Boolean value to decide if annotated dependency is required.

Example :
Address.java
@Component
public class Address {
	public String getCityName() {
		return "PrayagRaj";
	}
} 
Customer.java
@Component
public class Customer {
	public String getName() {
		return "Mahesh";
	}
} 
CustomerService.java
@Service
public class CustomerService {
	private Customer customer;
	private Address address;

	@Autowired
	public CustomerService(Customer customer, Address address) {
		this.customer = customer;
		this.address = address;
	}

	public Address getAddress() {
		return address;
	}

	public Customer getCustomer() {
		return customer;
	}
} 
SpringBootAppStarter.java
@SpringBootApplication
public class SpringBootAppStarter {
	public static void main(String[] args) throws Exception {
		ConfigurableApplicationContext ctx = SpringApplication.run(SpringBootAppStarter.class, args);
		CustomerService service = ctx.getBean(CustomerService.class);
		System.out.println(service.getCustomer().getName());
		System.out.println(service.getAddress().getCityName());
		ctx.close();
	}
} 
Output
Mahesh
PrayagRaj 

@Resource

1. The @Resource annotation is from jakarta.annotation package. This annotation may be applied to class, field or method level.
2. When the @Resource annotation is applied at method and field level, the container will inject the requested resource into the application component on class initialization.
3. When the @Resource annotation is applied at class level, the annotation declares a resource that the application will look up at runtime.
4. The @Resource annotation can also be applied on private field.

Find the elements of @Resource annotation.
authenticationType : Authentication type to use for the resource.
description : Description of the resource.
lookup : Name of the resource that the reference points to.
mappedName : A product-specific name that this resource should be mapped to.
name : The JNDI name of the resource.
shareable : Boolean value to decide if the resource can be shared between this component and other components.
type : The Java type of the resource.

Example :
CustomerService.java
@Service
public class CustomerService {
	@Resource
	private Customer customer;
	@Resource
	private Address address;

	public Address getAddress() {
		return address;
	}

	public Customer getCustomer() {
		return customer;
	}
} 

References

POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us

¥