Spring JSR-250 Annotations with Examples

In this article, we will explore about the JSR-250 Annotations with the Spring Framework. JSR 250, as a Java Specification Request, has the objective to define a set of annotations that address common semantic concepts and therefore can be used by many Java EE and Java SE components. This is to avoid redundant annotations across those components. JSR-250 Annotations were released on the 11th 05 2006. As Declarative annotation-driven configuration is more and more used in Java frameworks and applications, e.g. Spring makes more components of its framework configurable via annotations, the importance of JSR 250 is likely to increase in the future.

JSR-250 Annotations

JSR-250 Annotations in the Spring

Spring also provides support for JSR-250 annotations which include @PostConstruct, @PreDestroy and @Resource annotations. JSR-250 aka Common Annotations for the Java Platform was introduced as part of Java EE 5 an is usually used to annotated EJB3s.
  • @PostContruct This annotation is applied to a method to indicate that it should be invoked after all dependency injection is complete.
  • @PreDestroy This is applied to a method to indicate that it should be invoked before the bean is removed from the Spring context, i.e. just before it’s destroyed.
  • @Resource This duplicates the functionality of @Autowired combined with @Qualifier as you get the additional benefit of being able to name which bean you’re injecting, without the need for two annotations.

 

Annotation @Resource

              Target:

component class, or to fields or methods of the component class.

Description:

The @Resource annotation marks a resource that is needed by the application. This annotation may be applied to an application component class, or to fields or methods of the component class. When the annotation is applied to a field or method, the container will inject an instance of the requested resource into the application component when the component is initialized. If the annotation is applied to the component class, the annotation declares a resource that the application will look up at runtime.

Even though this annotation is not marked Inherited, deployment tools are required to examine all superclasses of any component class to discover all uses of this annotation in all superclasses. All such annotation instances specify resources that are needed by the application component. Note that this annotation may appear on private fields and methods of superclasses; the container is required to perform injection in these cases as well.

@Resource takes a “name” attribute, and by default Spring will interpret that value as the bean name to be injected. In other words, it follows by-name semantics as demonstrated in this example:

public class Circle
{ 
 private Point center;
 
 @Resource(name="pointB")
 public void setCenter(Point center) 
 {
      this.center = center;
 }
}

 

If no name is specified explicitly, then the default name will be derived from the name of the field or setter method: In case of a field, it will simply be equivalent to the field name; in case of a setter method, it will be equivalent to the bean property name. So the following example is going to have the bean with name “center” injected into its setter method:
public class Circle
{ 
 private Point center;
 
 @Resource
 public void setCenter(Point center) 
 {
      this.center = center;
 }
}

The name provided with the annotation will be resolved as a bean name by the BeanFactory of which the CommonAnnotationBeanPostProcessor is aware.
See the full Example

Annotation @PostContruct :

              Target:

methods of the component class.

Description:

The @PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization. This method MUST be invoked before the class is put into service. This annotation MUST be supported on all classes that support dependency injection. The method annotated with @PostConstruct MUST be invoked even if the class does not request any resources to be injected. Only one method can be annotated with this annotation. The method on which the @PostConstruct annotation is applied MUST fulfill all of the following criteria – – The method MUST NOT have any parameters except in the case of EJB interceptors in which case it takes an InvocationC ontext object as defined by the EJB specification. – The return type of the method MUST be void. – The method MUST NOT throw a checked exception. – The method on which @PostConstruct is applied MAY be public, protected, package private or private. – The method MUST NOT be static except for the application client. – The method MAY be final. – If the method throws an unchecked exception the class MUST NOT be put into service except in the case of EJBs where the EJB can handle exceptions and even recover from them.

public class Circle
{
   @PostConstruct
   public void initializeCircle()
   {
      //populates the circle data cache upon initialization...
      System.out.println("Init of Circle");
   }
} 

See the full Example

Annotation @PreDestroy :

              Target:

methods of the component class.

Description:

The @PreDestroy annotation is used on methods as a callback notification to signal that the instance is in the process of being removed by the container. The method annotated with @PreDestroy is typically used to release resources that it has been holding. This annotation MUST be supported by all container managed objects that support @PostConstruct except the application client container in Java EE 5. The method on which the @PreDestroy annotation is applied MUST fulfill all of the following criteria – – The method MUST NOT have any parameters except in the case of EJB interceptors in which case it takes an InvocationContext object as defined by the EJB specification. – The return type of the method MUST be void. – The method MUST NOT throw a checked exception. – The method on which PreDestroy is applied MAY be public, protected, package private or private. – The method MUST NOT be static. – The method MAY be final. – If the method throws an unchecked exception it is ignored except in the case of EJBs where the EJB can handle exceptions.
public class Circle
{
   @PreDestroy
   public void destroyCircle()
   {
        //clears the circle related cache upon destruction..
        System.out.println("Destroy of Circle");
   }
} 

See the full Example

 

Spring Related Topics you may like

 

 

Previous
Next