@Resource Annotation in Spring

spring-framework-resource-annotation-feature-image

@Resource Annotation is used to indicate a reference to a resource, it is considered an alternative to @Autowired which is an Spring standard. Similar to Java EE 5, we can use @Resource Annotation with field injection or setter method.

This tutorial will cover those kinds of injection which will be used for matching by Name, Type or Qualifier.

I. Field Injection
1. By Name

We have two implementations of interface User: FreeUser and PremiumUser.

public interface User {
	String getName();
}

public class FreeUser implements User {
	private String name;

	public FreeUser(String name) {
		System.out.println("Call constructor for FreeUser: " + name);
		this.name = name;
	}

	@Override
	public String getName() {
		return name;
	}
}

public class PremiumUser implements User{
	private String name;

	public PremiumUser(String name) {
		System.out.println("Call constructor for PremiumUser: " + name);
		this.name = name;
	}

	@Override
	public String getName() {
		return name;
	}
}

And two Beans of User:

@Configuration
public class AppConfig {

	@Bean(name = "u1")
	public FreeUser getUser1() {
		FreeUser user = new FreeUser("Jack");
		return user;
	}

	@Bean(name = "u2")
	public PremiumUser getUser2() {
		PremiumUser user = new PremiumUser("John");
		return user;
	}
}

What we need is that user1 and user2 will be injected those Beans above.

public class Show {
	private User user1;
	private User user2;
}

To apply Field Injection by Name, we add @Resource(name = “nameOfBean“) before the field that we wanna inject Bean to.

	@Resource(name = "u1")
	private User user1;

	@Resource(name = "u2")
	private User user2;
2. By Type

Injecting Bean to field can be done without adding name attribute, we just need @Resource before the field.

@Configuration
public class AppConfig {
	@Bean
	public CustomerService getCustomerService() {
		CustomerService customerService = new CustomerService("Cloud Service");
		return customerService;
	}
}

public class Show {
	@Resource
	private CustomerService customerService;
}
3. By Qualifier

We have two implementations of interface DataCenter: FreeStorage and PremiumStorage.

public interface DataCenter {...}
public class FreeStorage implements DataCenter{...}
public class PremiumStorage implements DataCenter{...}

To apply Field Injection by Qualifier with these Beans and fields:

public class Show {
	private DataCenter dataCenter1;
	private DataCenter dataCenter2;
}

@Configuration
public class AppConfig {

	@Bean(name = "dc1")
	public FreeStorage getDataCenter1() {
		FreeStorage dataCenter = new FreeStorage("BigData");
		return dataCenter;
	}

	@Bean(name = "dc2")
	public PremiumStorage getDataCenter2() {
		PremiumStorage dataCenter = new PremiumStorage("SmallData");
		return dataCenter;
	}
}

We add @Resource and @Qualifier(“…”) before fields as below:

	@Resource
	@Qualifier("dc1")
	private DataCenter dataCenter1;
	
	@Resource
	@Qualifier("dc2")
	private DataCenter dataCenter2;
II. Setter Method

Instead of using @Resource annotations before fields, we can apply them with setter methods.

1. By Name
public class ShowSetterMethod {
	private User user1;
	private User user2;

	@Resource(name = "u1")
	protected void setUser1(User user) {
		this.user1 = user;
	}
	
	// set Bean User u1 to user2
	@Resource(name = "u1")
	protected void setUser2(User user) {
		this.user2 = user;
	}
}
2. By Type
public class ShowSetterMethod {
	private CustomerService customerService;

	@Resource
	protected void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}
}
3. By Qualifier
public class ShowSetterMethod {
	private DataCenter dataCenter1;
	private DataCenter dataCenter2;

	@Resource
	@Qualifier("dc1")
	protected void setDataCenter1(DataCenter dataCenter) {
		this.dataCenter1 = dataCenter;
	}
	
	// set Bean DataCenter dc1 to dataCenter2
	@Resource
	@Qualifier("dc1")
	protected void setDataCenter2(DataCenter dataCenter) {
		this.dataCenter2 = dataCenter;
	}
}
III. Source code
1. Technology

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE (It’s OK if you use Eclipse)

2. Project Structure

resource-annotation-structure

3. Download Link

spring-resource-annotation

4. Check results

– Config maven build:
clean install
– Run project with mode Java Application
– Check results in Console Screen:

Call constructor for CustomerService: Cloud Service
Call constructor for FreeStorage: BigData
Call constructor for FreeUser: Jack
Call constructor for PremiumUser: John
Call constructor for PremiumStorage: SmallData
=== Check injecting data ===
user1: Jack
user2: John
customerService: Cloud Service
dataCenter1: BigData
dataCenter2: SmallData
=== Check setter injecting data ===
user1: Jack
user2: Jack
customerService: Cloud Service
dataCenter1: BigData
dataCenter2: BigData


By grokonez | November 3, 2016.

Last updated on September 25, 2018.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*