Home  >  Spring Core

@Resource Annotation in Spring

By Arvind Rai, February 13, 2013
On this page we will provide @Resource annotation in spring. Java supports javax.annotation.Resource annotation using JSR-250 that is applied on component class and fields or methods of component class. When @Resource is applied on fields or methods, the container injects the requested resource. Spring supports @Resource annotation. It behaves same as @Autowired annotation of spring. @Autowired annotation automatically injects the required bean. @Resource has name attribute. It can be used in two ways.

1. @Resource with name attribute as follows
@Resource(name="company")
private Company company; 
Where value of attribute company is a bean name.

2. @Resource without name attribute.
@Resource
private Address address; 
Here @Resource will look for property class instance and will inject the dependency.

@Resource with name attribute Example

Here we will provide a complete example of @Resource annotation with name attribute using XML application context.
Employee.java
package com.concretepage.bean;
import javax.annotation.Resource;
public class Employee {
	@Resource(name="company")
	private Company company;
	@Resource(name="address")
	private Address address;
	public Company getCompany() {
		return company;
	}
	public Address getAddress() {
		return address;
	}
} 
Address.java
package com.concretepage.bean;
public class Address {
	private String city;
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
} 
Company.java
package com.concretepage.bean;
public class Company {
	private String compName;
	public String getCompName() {
		return compName;
	}
	public void setCompName(String compName) {
		this.compName = compName;
	}
} 
app-conf.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

		<context:annotation-config/>
		<bean class="com.concretepage.bean.Employee"/>
		<bean id="address" class="com.concretepage.bean.Address">
			<property name="city" value="Varanasi"/>
		</bean>		
		<bean id="company" class="com.concretepage.bean.Company">
			<property name="compName" value="ABCD Ltd"/>
		</bean>		
</beans> 
SpringDemo.java
package com.concretepage;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.concretepage.bean.Employee;
public class SpringDemo {
	public static void main(String[] args) {
		AbstractApplicationContext  context = new ClassPathXmlApplicationContext("app-conf.xml");
		Employee employee = context.getBean(Employee.class);
		System.out.println(employee.getAddress().getCity());
		System.out.println(employee.getCompany().getCompName());		
		context.close();
	}
} 
Find the output.
Varanasi
ABCD Ltd 

@Resource without name attribute Example

Here we will provide a complete example of @Resource annotation without using name attribute. We will use here java configuration.
Employee.java
package com.concretepage.bean;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
@Component
public class Employee {
	@Resource
	private Company company;
	@Resource
	private Address address;
	public Company getCompany() {
		return company;
	}
	public Address getAddress() {
		return address;
	}
} 
Address.java
package com.concretepage.bean;
public class Address {
	private String city;
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
} 
Company.java
package com.concretepage.bean;
public class Company {
	private String compName;
	public String getCompName() {
		return compName;
	}
	public void setCompName(String compName) {
		this.compName = compName;
	}
} 
AppConfig.java
package com.concretepage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import com.concretepage.bean.Address;
import com.concretepage.bean.Company;
@Configuration
@ComponentScan(basePackages="com.concretepage.bean")
public class AppConfig {
	@Bean
	public Address address(){
		Address address = new Address();
		address.setCity("Varanasi");
		return address;
	}
	@Bean
	public Company company() {
		Company company = new Company();
		company.setCompName("ABCD Ltd");
		return company;
	}
} 
SpringDemo.java
package com.concretepage;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.concretepage.bean.Employee;
public class SpringDemo {
	public static void main(String[] args)  {
	    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	    ctx.register(AppConfig.class);
	    ctx.refresh();
	    Employee employee = ctx.getBean(Employee.class);
	    System.out.println(employee.getAddress().getCity());
	    System.out.println(employee.getCompany().getCompName());
    	    ctx.close();
	}
}  
Find the output.
Varanasi
ABCD Ltd 

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us