Spring JSR 250 Annotations

Tutorial Content: Spring tutorial for beginners

Spring supports JSR 250 Annotations such as @PostConstruct, @PreDestroy and @Resource annotations.Lets  discuss each in brief.

@Resource:

Spring also supports injection using the JSR-250 @Resource annotation on fields or bean property setter methods. This is a common pattern found in Java EE 5 and Java 6 which Spring supports for Spring-managed objects as well.
@Resource takes name as attribute and by default Spring will interpret that value as the bean name to be injected. In other words, it follows by-name semantics.If you do not specify name attribute then it will interpret property name as bean name to be injected and then it will search in XML configuration file.

Example:

For configuring spring in your eclipse ide please refer hello world example

1.Country.java:

This is simple pojo class having some attributes so here country has name and object of Capital class.
Create Country.java under package org.arpit.javapostsforlearning.Copy following content into Country.java.
package org.arpit.javapostsforlearning;
import org.springframework.beans.factory.annotation.Autowired;
public class Country {

    String countryName;

    @Resource(name="capitalA") 
    Capital capital;
    public String getCountryName() {
        return countryName;
    }
    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }
    public Capital getCapital() {
        return capital;
    }
}

2.Capital.java

This is also simple pojo class having one attribute called “capitalName”.

Create Capital.java under package org.arpit.javapostsforlearning.java.Above Country class contains object of this class.Copy following content into Capital.java

package org.arpit.javapostsforlearning;

public class Capital {

    String capitalName;

    public String getCapitalName() {
        return capitalName;
    }

    public void setCapitalName(String capitalName) {
        this.capitalName = capitalName;
    }
}

3.QualifierAnnotationInSpringMain.java

This class contains main function.Create QualifierAnnotationInSpringMain.java under package org.arpit.javapostsforlearning.Copy following content into QualifierAnnotationInSpringMain.java
 package org.arpit.javapostsforlearning;

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

public class QualifierAnnotationInSpringMain{

    public static void main(String[] args) {
        ApplicationContext appContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");

        Country countryObj = (Country) appContext.getBean("CountryBean");
        String countryName=countryObj.getCountryName();
        Capital capital=countryObj.getCapital();
        String capitalName=capital.getCapitalName();
        System.out.println(capitalName+" is capital of "+countryName);

    }
}

4.ApplicationContext.xml



 
 
  
 
 
  
 
 
  
 

As you can note here we are having two beans of same type.In Country.java we have used @Resource(name-“capitalA”) it means we want to autowire capital property of country with bean id=”capitalA” in XML configuration file.

5.Run it:

When you will run above application,you will get following as output.

Delhi is capital of India

@PostContruct and @PreDestroy:

These annotations are alternative methods for defining initialization and destruction callback functions.
We have used all other method in Spring lifetime callbacks.

Example:

For configuring spring in your eclipse ide please refer  hello world example

1.Country.java:

This is simple pojo class having some attributes so here country has name.
Create Country.java under package org.arpit.javapostsforlearning.Copy following content into Country.java.
package org.arpit.javapostsforlearning;

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

public class Country {

 String countryName ;

 public String getCountryName() {
  return countryName;
 }

 public void setCountryName(String countryName) {
  this.countryName = countryName;
 }

 @PostConstruct
 public void init()
 {
  System.out.println("In init block of country");
 }

 @PreDestroy 
 public void destroy()
 {
  System.out.println("In destroy block of country");
 }
}

2.LifetimeCallbacksMain.java

This class contains main function.Create LifetimeCallbacksMain.java under package org.arpit.javapostsforlearning.Copy following content into LifetimeCallbacksMain.java
package org.arpit.javapostsforlearning;

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

public class LifetimeCallbacksMain{

 public static void main(String[] args) {

  AbstractApplicationContext appContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
  Country countryObj = (Country) appContext.getBean("country");
  System.out.println("Country Name: "+countryObj.getCountryName());
  appContext.registerShutdownHook();
 }
}

Here you need to register a shutdown hook registerShutdownHook() method that is declared on the AbstractApplicationContext class. This will ensures a graceful shutdown and calls the relevant destroy methods.

3.ApplicationContext.xml




 
 
 

4.Run it

When you will run above application,you will get following as output.
In init block of country
Country Name: India
In destroy block of country
[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.