Spring JSR-250 Annotations:
JSR-250 is a standard, Java Specifications Request that defines all the standard annotations that would apply across different technology and different frameworks. Spring supports some of the standard JSR-250 annotations
@Resource :
@Resource is another annotation that can be used to inject dependencies into member variables of a class. @Resource annotation can do a standard dependency injection by name.
We have HDFC.java that has a member variable’branch’.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.j2eereference.spring; import javax.annotation.Resource; public class HDFC implements Bank{ private Branch branch; public Branch getBranch() { return branch; } @Resource(name="branchB") public void setBranch(Branch branch) { this.branch = branch; } @Override public void getBranchInfo() { System.out.println("HDFC Branch code : "+branch.getCode()+" and Address : "+branch.getAddress() ); } } |
And we have the interface Bank.java
1 2 3 4 5 6 7 |
package com.j2eereference.spring; public interface Bank { public void getBranchInfo(); } |
BankApp.java that gets the ‘hdfc’ bean in the application context.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.j2eereference.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class BankApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); Bank bank = (Bank)context.getBean("hdfc"); bank.getBranchInfo(); } } |
spring.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="hdfc" class="com.j2eereference.spring.HDFC" > </bean> <bean id="branchA" class="com.j2eereference.spring.Branch" > <property name="code" value="1234"></property> <property name="address" value="BranchA Address"></property> </bean> <bean id="branchB" class="com.j2eereference.spring.Branch" > <property name="code" value="5678"></property> <property name="address" value="BranchB Address"></property> </bean> </beans> |
In the spring.xml, notice that we have not initialized the ‘branch’ property of ‘hdfc’ bean. But we have injected the dependency using @Resource annotation on the setBranch() method (setter method of ‘branch’ member variable). As already mentioned above, it does the auto-wiring by name, so we specify name=”branchB”. Thus, the member variable ‘branch’ gets initialized with ‘branchB’ bean. When we run this,
Output:
HDFC Branch code : 5678 and Address : BranchB Address
There are some defaults that we can look here. Suppose we do not specify the name for the annotation, spring will look for the bean with same name as the member variable and does the dependency injection. This is how @Resource is similar to auto-wire by name.
@PostConstruct and @PreDestroy.
Suppose we have an init method initializeHDFC() and destroy method destroyHDFC() in our HDFC.java. We want these methods to be called when the HDFC bean itself is initialized and destroyed respectively.
If we had to configure this using XML, we would have init and destroy methods for the hdfc definition or we would have to implement the initializing bean and disposable bean interfaces and specify the right names for the methods. Here, we have custom names for init and destroy method names and we are not even entering the configuration in the xml. We will use the annotations for initializing and destroying the HDFC bean. These annotations are @PostConstruct and @PreDestroy. The names are self explanatory. @PostConstruct is executed soon after the bean is initialized and @PreDestroy is executed just before the bean is destroyed. In order to execute the PreDestroy() method, make sure you register the shutdown hooks in the application context.
HDFC.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 |
package com.j2eereference.spring; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.annotation.Resource; public class HDFC implements Bank{ private Branch branch; public Branch getBranch() { return branch; } @Resource(name="branchB") public void setBranch(Branch branch) { this.branch = branch; } @Override public void getBranchInfo() { System.out.println("HDFC Branch code : "+branch.getCode()+" and Address : "+branch.getAddress() ); } @PostConstruct public void initializeHDFC() { System.out.println("Initializing HDFC..."); } @PreDestroy public void destroyHDFC() { System.out.println("Destroying HDFC..."); } } |
Notice the changes in BankApp.java to register the shutdown hooks.
BankApp.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.j2eereference.spring; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class BankApp { public static void main(String[] args) { AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); context.registerShutdownHook(); Bank bank = (Bank)context.getBean("hdfc"); bank.getBranchInfo(); } } |
Output:
Initializing HDFC…
HDFC Branch code : 5678 and Address : BranchB Address
Destroying HDFC…
Leave a Reply