Thursday 11 May 2017

Spring: Resource annotation example

Spring supports @Resource annotation, by default it injects a bean by name.

Example
@Resource(name = "Osho")
public void setAuthor1(Author author1) {
         this.author1 = author1;
}

Following is the complete working application.

Author.java
package com.sample.pojo;

public class Author {
 private String firstName;
 private String lastName;
 private String dateOfBirth;

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public String getDateOfBirth() {
  return dateOfBirth;
 }

 public void setDateOfBirth(String dateOfBirth) {
  this.dateOfBirth = dateOfBirth;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Author [firstName=").append(firstName).append(", lastName=").append(lastName)
    .append(", dateOfBirth=").append(dateOfBirth).append("]");
  return builder.toString();
 }

}

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

 <context:annotation-config />

 <bean id="Osho" name="Osho" class="com.sample.pojo.Author" primary="true">
  <property name="firstName" value="Chandra Mohan" />
  <property name="lastName" value="Jain" />
  <property name="dateOfBirth" value="11 December 1931" />
 </bean>

 <bean id="Krishna" name="Krishna" class="com.sample.pojo.Author" primary="true">
  <property name="firstName" value="Hari Krishna" />
  <property name="lastName" value="Gurram" />
  <property name="dateOfBirth" value="6 May 1989" />
 </bean>

 <bean id="Sravya" name="Sravya" class="com.sample.pojo.Author" primary="true">
  <property name="firstName" value="Sravya" />
  <property name="lastName" value="Guruju" />
  <property name="dateOfBirth" value="06 June 1988" />
 </bean>
 
 <bean id="authors" class="com.sample.pojo.Authors" />
</beans>

Authors.java
package com.sample.pojo;

import javax.annotation.Resource;

public class Authors {
 private Author author1;
 private Author author2;
 private Author author3;

 @Resource(name = "Osho")
 public void setAuthor1(Author author1) {
  this.author1 = author1;
 }

 public Author getAuthor2() {
  return author2;
 }

 @Resource(name = "Krishna")
 public void setAuthor2(Author author2) {
  this.author2 = author2;
 }

 public Author getAuthor3() {
  return author3;
 }

 @Resource(name = "Sravya")
 public void setAuthor3(Author author3) {
  this.author3 = author3;
 }

 public Author getAuthor1() {
  return author1;
 }

}

HelloWorld.java
package com.sample.test;

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

import com.sample.pojo.Authors;

public class HelloWorld {
 public static void main(String args[]) {
  ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "myConfiguration.xml" });

  Authors authors = context.getBean("authors", Authors.class);

  System.out.println(authors.getAuthor1());
  System.out.println(authors.getAuthor2());
  System.out.println(authors.getAuthor3());
  
  ((ClassPathXmlApplicationContext) context).close();
 }
}

Output
Author [firstName=Chandra Mohan, lastName=Jain, dateOfBirth=11 December 1931]
Author [firstName=Hari Krishna, lastName=Gurram, dateOfBirth=6 May 1989]
Author [firstName=Sravya, lastName=Guruju, dateOfBirth=06 June 1988]

If you don’t specify the name attribute to Resource annotation, the default name is derived from the field name or setter method. In case of a field, it takes the field name; in case of a setter method, it takes the bean property name.

Example
@Resource
public void setKrishna(Author krishna) {
         this.krishna = krishna;
}

Update Authors.java like below.

Authors.java
package com.sample.pojo;

import javax.annotation.Resource;

public class Authors {
 private Author osho;
 private Author krishna;
 private Author sravya;

 public Author getOsho() {
  return osho;
 }

 @Resource
 public void setOsho(Author osho) {
  this.osho = osho;
 }

 public Author getKrishna() {
  return krishna;
 }

 @Resource
 public void setKrishna(Author krishna) {
  this.krishna = krishna;
 }

 public Author getSravya() {
  return sravya;
 }

 @Resource
 public void setSravya(Author sravya) {
  this.sravya = sravya;
 }

}

Update configuration file like below.

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

 <context:annotation-config />

 <bean id="osho" name="osho" class="com.sample.pojo.Author" primary="true">
  <property name="firstName" value="Chandra Mohan" />
  <property name="lastName" value="Jain" />
  <property name="dateOfBirth" value="11 December 1931" />
 </bean>

 <bean id="krishna" name="krishna" class="com.sample.pojo.Author" primary="true">
  <property name="firstName" value="Hari Krishna" />
  <property name="lastName" value="Gurram" />
  <property name="dateOfBirth" value="6 May 1989" />
 </bean>

 <bean id="sravya" name="sravya" class="com.sample.pojo.Author" primary="true">
  <property name="firstName" value="Sravya" />
  <property name="lastName" value="Guruju" />
  <property name="dateOfBirth" value="06 June 1988" />
 </bean>
 
 <bean id="authors" class="com.sample.pojo.Authors" />
</beans>

HelloWorld.java
package com.sample.test;

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

import com.sample.pojo.Authors;

public class HelloWorld {
 public static void main(String args[]) {
  ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "myConfiguration.xml" });

  Authors authors = context.getBean("authors", Authors.class);

  System.out.println(authors.getKrishna());
  System.out.println(authors.getOsho());
  System.out.println(authors.getSravya());
  
  ((ClassPathXmlApplicationContext) context).close();
 }
}

Output
Author [firstName=Hari Krishna, lastName=Gurram, dateOfBirth=6 May 1989]
Author [firstName=Chandra Mohan, lastName=Jain, dateOfBirth=11 December 1931]
Author [firstName=Sravya, lastName=Guruju, dateOfBirth=06 June 1988]



Previous                                                 Next                                                 Home

No comments:

Post a Comment