Java Login

About javalogin.com

Hello guys,
javalogin.com is for Java and J2EE developers, all examples are simple and easy to understand 

It is developed and maintained by Vaibhav Sharma. The views expressed on this website are his own and do not necessarily reflect the views of his former, current or future employers. I am professional Web development. I work for an IT company as Senior Consultant. Primary I write about spring, hibernate and web-services. I am trying to present here new technologies.


     << Previous
Next >>     


JSR 250 Annotations

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. 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.

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

@Resource Annotation with Example

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. @Resource is an explicit request for injection of a dependency by name, so is not only more concise than @Autowired with @Qualifier, but clearer in its intent.

Employee.java

package annotation;

import javax.annotation.Resource;

public class Employee {
private int id;
private String name;
private Department dept;

public Department getDept() {
return dept;
}

@Resource(name="department")
public void setDept(Department dept) {
this .dept = dept;
}

public int getId() {
return id;
}

public void setId(int id) {
this .id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this .name = name;
}

}

In the above class @Resource(name="department") annotation is used to inject the property of dept just like autwoired annotation. name attribute with resource annotation is used to inject exact property name match from xml file (below content.xml). If property name and xml file mapping bean name is same, then there is no need to define name attribute with @resource annnotation.

Department.java
package annotation;

public class Department {
String departmentName;

public String getDepartmentName() {
return departmentName;
}

public void setDepartmentName(String departmentName) {
this .departmentName = departmentName;
}
}

context.xml

........
........
<bean id="emp" class="annotation.Employee">
<property name="id" value="1" />
<property name="name" value="Test" />
<property name="dept" ref="dept"/>
</bean>

<bean id="department" class="annotation.Department">
<property name="departmentName" value="Marketing"/>
</bean>

<context:annotation-config/>

</beans>

Main.java

package annotation;

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

public class Main {

public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext
("annotation/context.xml");
Employee emp = (Employee)context.getBean("emp");
System.out.println(emp.getName()+" "
+emp.getDept().getDepartmentName());
}
}

@PostConstruct and @PreDestroy annotations are not exclusive to Spring: they are a standard and consequently widely used in many container managed environments. Spring container is no exception so you can use these annotations in your Spring beans.

@PostConstruct annotation defines a method that will be called after a bean as been fully initialized. In other words it will be called after bean construction and all dependency injection. This annotation just like init method configuration

@PreDestroy annotation defines a method that will be called just before a bean is destroyed. This is usually useful for resource clean up. This annotation just like destroy method configuration

Employee.java

package annotation;

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

public class Employee {
private int id;
private String name;
private Department dept;

public Department getDept() {
return dept;
}

@Resource(name="department")
public void setDept(Department dept) {
this .dept = dept;
}

@PostConstruct
public void initializeEmployee() {
System.out.println("Init of Employee");
}

@PreDestroy
public void destroyEmployee() {
System.out.println("Destroy of Employee");
}

public int getId() {
return id;
}

public void setId(int id) {
this .id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this .name = name;
}

}

Main.java

package annotation;

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

public class Main {

public static void main(String[] args) {
AbstractApplicationContext context
= new ClassPathXmlApplicationContext("annotation/context.xml");
Employee emp = (Employee)context.getBean("emp");
System.out.println(emp.getName()+" "
+emp.getDept().getDepartmentName());
context.registerShutdownHook(); }
}


     << Previous
Next >>