Spring Annotation (V)
June 10, 2010
use the JSR-250 annotation support not only their own definition of Spring @ Autowired notes, also supports several JSR-250 specification defined by the comments, they are @ Resource, @ PostConstruct and @ PreDestroy. @ Resource @ Resource role is equivalent to @ Autowired, but @ Autowired by byType automatic injection of @ Resource face automatic default by byName into nothing. @ Resource has two attributes is more important, are the name and type, Spring @ Resource annotation will resolve the name attribute name for the Bean, but Bean type attribute is parsed as the type. So if you use the name attribute is used byName automatic injection strategy, and use the type attribute is used when byType automatic injection strategy. If neither name nor specify the type attribute specified, then the use of byName automatically by reflection into the strategy. Resource annotation class in the Spring distribution of lib/j2ee/common-annotations.jar class package, so before use must be added to the class library project. Look at an example using @ Resource: Listing 16 Using the @ Resource annotation Boss.java package com.baobaotao;
import javax.annotation.Resource;
public class Boss {
/ / auto-injection type of Car, Bean
@ Resource
private Car car;
/ / bean name is automatically injected into the office of the Bean
@ Resource (name = “office”)
private Office office;
}
In general, we need to use something like @ Resource (type = Car.class) notes, as this type of information can be Bean Java reflection obtained from the code. JSR-250 annotations to make the entry into force, except those marked in the Bean class notes, but also need to register in a Spring container is responsible for dealing with these comments BeanPostProcessor: CommonAnnotationBeanPostProcessor achieve BeanPostProcessor interface, which is responsible for scanning using the JSR-250 annotations Bean, and they act accordingly. @ PostConstruct and @ PreDestroy Spring container Bean is a life-cycle, Spring allows Bean and Bean in the initialization is complete destruction of the former to perform specific operations, you can only achieve InitializingBean / DisposableBean interface to customize the initialization / method of operation prior to destruction, you can also specify attributes
JSR-250 is initialized / destroyed before the specified method defines two comment categories, namely @ PostConstruct and @ PreDestroy, this method can only be applied on the two notes. Marked with @ PostConstruct annotation method is called after the class is instantiated, the method marked @ PreDestroy destroyed in the class before the call. Listing 17 Using the @ PostConstruct and @ PreDestroy comments Boss.java package com.baobaotao;
import javax.annotation.Resource;
import javax.annotation.PostConstruct; < br />
import javax.annotation.PreDestroy;
public class Boss {
@ Resource
private Car car;
@ Resource (name = “office”)
private Office office;
@ PostConstruct
public void postConstruct1 () {
System.out.println (“postConstruct1″);
}
@ PreDestroy
public void preDestroy1 () {
System.out.println (“preDestroy1″);
}
…
} < br />
You only need to mark the way before the @ PostConstruct or @ PreDestroy, these methods will be initialized in the Bean before or after the destruction carried out by the Spring container. We know, whether it is through the realization of InitializingBean / DisposableBean interface, or through
import org.springframework.context . support.ClassPathXmlAp plicationContext;
public class AnnoIoCTest {
public static void main (String [] args) {
String [] locations = {“beans.xml”};
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext (locations);
Boss boss = (Boss) ctx.getBean (“boss”);
System.out.println (boss);
ctx.destroy ();// close the Spring container, in order to trigger the execution of methods of destruction Bean
}
}
At this time, you will see marked @ PostConstruct of postConstruct1 () method will start in the Spring container to create Boss Bean is triggered when the execution The marked @ PreDestroy comments preDestroy1 () method will be destroyed before the Spring container closure is triggered when the Boss Bean implementation.
Posted: January 6th, 2012
at 12:04pm by admin
Tagged with disposablebean
Categories: Uncategorized
Comments: No comments













