Wednesday 5 April 2017

Spring Tutorial 20: Some JSR-250 Annoations

JSR-250 is Java Specification Request. Spring support some JSR-250 annotations we are going to look at some of them in this tutorial. The first annotation which we are looking is @Resource. @Resource is another annotation that we can use to inject dependency.
Circle.java
import javax.annotation.Resource;
public class Circle implements Shape{
    private Point pointA;
    public Point getPointA() {
        return pointA;
    }
    @Resource
    public void setPointA(Point pointA) {
        this.pointA = pointA;
    }
    @Override
    public void draw() {
        System.out.println("Drawing Circle:");
        System.out.println("Drawing Circle:("+pointA.getX() +","+pointA.getY()+")");
    }
}
Shape.java
package com.java.spring.bean;
public interface Shape {
 public void draw();
}
Point.java
package com.java.spring.bean;

public class Point {
 private int x;
 private int y;
 public int getX() {
  return x;
 }
 public void setX(int x) {
  this.x = x;
 }
 public int getY() {
  return y;
 }
 public void setY(int y) {
  this.y = y;
 }
}
spring.xml
<beans xmlns:context="http://www.springframework.org/schema/context" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://www.springframework.org/schema/beans" 
xsi:schemalocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:annotation-config>
 <bean class="com.java.spring.bean.Point" id="pointA">
  <property name="x" value="0">
  <property name="y" value="0">
 </property></property></bean>
 <bean class="com.java.spring.bean.Point" id="pointB">
  <property name="x" value="-20">
  <property name="y" value="-20">
 </property></property></bean>
 <bean class="com.java.spring.bean.Point" id="pointC">
  <property name="x" value="20">
  <property name="y" value="20">
 </property></property></bean>
 <bean class="com.java.spring.bean.Circle" id="circle">
 </bean>
</context:annotation-config></beans>   
 
DrawingApp.java
package com.java.spring.bean;

import org.springframework.context.support.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DrawingApp {
 private static ApplicationContext context;
 public static void main(String[] args) {
  context = new ClassPathXmlApplicationContext("spring.xml");
  Shape shape = (Shape) context.getBean("circle");
  shape.draw();
 }
}

In the above example I want pointA as a dependency that will get inject. For that I've defined @Resource annotation. It is from javax.annotation. It inject by the byName.If it find the bean name same member variable name then it inject it. So, I didn't define the name in the example above. It is same as autowire by name using JSR-250 annotation. Suppose if I want to inject pointB then I can define in @Resource as:
@Resource(name="pointB")
In the above scenario it will inject pointB. It will search bean with name as pointB in xml file and once it find bean then it will inject it.
Output I've used here name as pointB
Drawing Circle:
Drawing Circle:(-20,-20)
Next Annotation is @PostConstruct and @PreDestroy.
Circle.java
package com.java.spring.bean;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

public class Circle implements Shape{ 
 private Point pointA;
 public Point getPointA() {
  return pointA;
 }
 @Resource(name="pointB")
 public void setPointA(Point pointA) {
  this.pointA = pointA;
 }
 @Override
 public void draw() {
  System.out.println("Drawing Circle:");
  System.out.println("Drawing Circle:("+pointA.getX() +","+pointA.getY()+")");
 }
 @PostConstruct
 public void AfterInitialization(){
  System.out.println("Bean initialization is done");
 }
 @PreDestroy
 public void BeforeDestroyOfBean(){
  System.out.println("Before Destroy Bean");
 }
}

DrawingApp.java
package com.java.spring.bean;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DrawingApp {
 private static AbstractApplicationContext context;
 public static void main(String[] args) {
  context = new ClassPathXmlApplicationContext("spring.xml");
  context.registerShutdownHook();
  Shape shape = (Shape) context.getBean("circle");
  shape.draw();
 }
}

Output
Bean initialization is done
Drawing Circle:
Drawing Circle:(-20,-20)
Before Destroy Bean

@PostConstruct: It is from javax.annotation package. This is execute after bean is initialized.  
@PreDestory: It is from javax.annoation package. It get called before the bean is destroy. For this we have used AbstractApplicationContext container so that we can call shutdownHook method.