In this post, We will learn about the Spring JSR-250 Annotations with Examples using a Demo Project.
Spring Framework also supports JSR-250 annotations that include @Resource, @PostConstruct, and @PreDestroy annotations.
These annotations are not really required because We already have other alternates or options to achieve the same functionalities that we achieve using these annotations
@Resource Annotation
We can use @Resource annotation on or before fields or setter methods. The @Resource annotation usually takes a ‘name’ attribute that will be interpreted as the bean name to be injected.
If We do not specify the ‘name’ attribute explicitly then the default name is derived from the class field name or setter method. In the case of a field, it takes the field name and in the case of a setter method, it takes the bean property name.
@PostConstruct and @PreDestroy Annotations:
To set up and teardown functionalities for a Spring bean, we simply declare the <bean> with init-method and/or destroy-method parameters. The init-method attribute usually specifies a method that is to be called on the bean immediately after instantiation. On another side, We can use a destroy-method attribute to specify a method that is called just before a bean is removed or destroyed from the spring container.
We can usually use @PostConstruct annotation as an alternate of initialization callback and @PreDestroy annotation as an alternate of destruction callback.
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kkhindigyan.org</groupId> <artifactId>JSR250AnnotationsExample</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <org.springframework.version>5.2.13.RELEASE</org.springframework.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework.version}</version> </dependency> </dependencies> </project> |
Employee.java
Java Class which is registered as Spring bean in Spring Container.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
package com.kkhindigyan.model; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.annotation.Resource; public class Employee { private Integer employeeId; private String employeeName; private Double salary; /*@Autowired @Qualifier("address2")*/ @Resource(name = "address2") private Address address; public Integer getEmployeeId() { return employeeId; } public void setEmployeeId(Integer employeeId) { this.employeeId = employeeId; } public String getEmployeeName() { return employeeName; } public void setEmployeeName(String employeeName) { this.employeeName = employeeName; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } public void setAddress(Address address) { this.address = address; } public Address getAddress() { return address; } @PostConstruct public void init() { System.out.println("Employee bean is intialized.."); } @PreDestroy public void destroy() { System.out.println("Employee bean is going to destroy.."); } @Override public String toString() { return "Employee [employeeId=" + employeeId + ", employeeName=" + employeeName + ", salary=" + salary + "]"; } } |
Address.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.kkhindigyan.model; public class Address { private String addressLine1; private String addressLine2; private String city; private Long zipCode; public String getAddressLine1() { return addressLine1; } public void setAddressLine1(String addressLine1) { this.addressLine1 = addressLine1; } public String getAddressLine2() { return addressLine2; } public void setAddressLine2(String addressLine2) { this.addressLine2 = addressLine2; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public Long getZipCode() { return zipCode; } public void setZipCode(Long zipCode) { this.zipCode = zipCode; } @Override public String toString() { return "Address [addressLine1=" + addressLine1 + ", addressLine2=" + addressLine2 + ", city=" + city + ", zipCode=" + zipCode + "]"; } } |
applicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <bean id="employee" class="com.kkhindigyan.model.Employee"> <property name="employeeId" value="102883"></property> <property name="employeeName" value="Sean"></property> <property name="salary" value="80000"></property> </bean> <bean id="address1" class="com.kkhindigyan.model.Address"> <property name="addressLine1" value="Address Line 1"></property> <property name="addressLine2" value="Address Line 2"></property> <property name="city" value="Delhi"></property> <property name="zipCode" value="959049"></property> </bean> <bean id="address2" class="com.kkhindigyan.model.Address"> <property name="addressLine1" value="Address Line 1"></property> <property name="addressLine2" value="Address Line 2"></property> <property name="city" value="Mumbai"></property> <property name="zipCode" value="9066249"></property> </bean> </beans> |
ClientTest.java
Client Program with the main method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package com.kkhindigyan.org; import org.springframework.context.ApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.kkhindigyan.model.Address; import com.kkhindigyan.model.Employee; public class ClientTest { public static void main(String[] args) { ApplicationContext applicationContext = null; try { //Creating Instance of ApplicationContext Spring Container applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); //Asking Spring Container to return Spring bean with Specific Id or name Employee employee = (Employee) applicationContext.getBean("employee"); System.out.println(employee); Address address = employee.getAddress(); //Address address = (Address) applicationContext.getBean("address"); System.out.println(address); } catch (Exception e) { e.printStackTrace(); }finally { if (applicationContext != null) ((AbstractApplicationContext) applicationContext).close(); } } } |
If you run ClientTest.java as Java Application then it will give the below output:
1 2 3 4 |
Employee bean is intialized.. Employee [employeeId=102883, employeeName=Sean, salary=80000.0] Address [addressLine1=Address Line 1, addressLine2=Address Line 2, city=Mumbai, zipCode=9066249] Employee bean is going to destroy.. |
That’s all about Spring JSR-250 Annotations with Example
You May Also Like:
Spring BeanFactory Container Example
Spring ApplicationContext Container Example
Annotation-based Configuration in Spring Framework Example
Spring Java-based Configuration Example
Spring Setter Dependency Injection Example
Spring @Autowired Annotation With Setter Injection Example
Spring Constructor based Dependency Injection Example
Spring @Autowired Annotation With Constructor Injection Example
Spring Autowiring byName & byType Example
getBean() overloaded methods in Spring Framework
Spring Inner bean example
Spring Dependency Injection with Factory Method
Spring Framework @Qualifier example
Injecting Collections in Spring Framework Example
Spring Bean Definition Inheritance Example
Spring bean scopes with example
Spring BeanPostProcessor Example
Spring JDBC Integration Example
Spring JDBC Annotation Example
Spring with Jdbc java based configuration example
Spring JDBC NamedParameterJdbcTemplate Example
How to call stored procedures in the Spring Framework?
If you have any feedback or suggestion please feel free to drop in below comment box.