Spring @Resource Annotation With Example

Spring @Resource Annotation With Example

spring supports dependency injection using JSR-250 @Resource annotation. @Resource annotation can be applied to fileds or setter methods of a spring bean. @Resource takes name attribute , Spring checks the value of name attribute if it is same as as the bean name then spring container will do the dependency injection. we can say it is same as autowiring by-name startegy.

Lets See Example

Tools Used In This Article

  • Spring 4.3.2
  • Maven 4.0.0
  • JDK 1.6

First create Maven Project from the Ecclipse and below is Project Directory Structure

Project Structure

Maven Dependencies

Add The Following Dependencies to the pom.xml

  • spring-core
  • spring-beans
  • spring-context

pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SpringResourceAnnDemo</groupId>
<artifactId>SpringResourceAnnDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>

</dependencies>
</project>

Laptop.java


package com.java2study;

import javax.annotation.Resource;


public class Laptop {
public String laptopName;
@Resource(name = "series1")
public Series series;

public String getLaptopName() {
return laptopName;
}
public void setLaptopName(String laptopName) {
this.laptopName = laptopName;
}
public Series getSeries() {
return series;
}
public void setSeries(Series series) {
this.series = series;
}
public void showSeries(){
System.out.println("The laptop Name is:"+laptopName);
System.out.println("the series is :"+series);

}



}



Series.java


package com.java2study;
public class Series {
public String seriesName;

public String getSeriesName() {
return seriesName;
}

public void setSeriesName(String seriesName) {
this.seriesName = seriesName;
}

@Override
public String toString() {
return "seriesName=" + seriesName;
}


}

applicationContext.xml


<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-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">

<context:annotation-config/>
<bean id="laptop" class="com.java2study.Laptop">
<property name="laptopName" value="xxxx" />
</bean>
<bean id="series" class="com.java2study.Series">
<property name="seriesName" value="A" />
</bean>
<bean id="series1" class="com.java2study.Series">
<property name="seriesName" value="B" />
</bean>

</beans>

Create Test class And Run the code

Test.java


package com.java2study;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Test {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
Laptop laptop = (Laptop)context.getBean("laptop");
laptop.showSeries();
}

}


output


INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
The laptop Name is:xxxx
the series is :seriesName=B
-->