Pages

Friday, August 3, 2012

Spring JSR 250 annotations

Below are some commonly used annotations in spring

1) resource annotation

@Resource(name="someBean")
public void setProperty(somebeantype property){
   this.property = property;
}
Here the @Resource annotation is used to locate the resource. The property is set using the .xml file and it should have a bean named "someBean". If the parenthesis and the arguments within it is not given with the annotation, then spring looks for a bean named in the xml file as the same name as the property. If not found you will get an exception.

2) required annotation.

Say you need to set a certain property when you initialized a bean and it is a must. Then you can add the @required annotation for the set method. In this way if the property is not set or the setter is not called for, Spring gives the exception even before running the business logic (That is in the initialization phase).

@required
public void someSetterMethod(Obj obj){
   this.obj  = obj;
}

3) post processor annotations.

@PostConstruct
   You can use this annotation to call a method just after the construction of a bean.

@PreDestroy
   You can use this annotation to call a method just before the bean is destroyed. This will be useful to do some clean up work.

No comments:

Post a Comment