spring2.5 annotation-driven (B) @ PostConstruct @ PreDestroy

July 19, 2010
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 / destruction before the method of operation, you can also specify element init-method/destroy-method property after initialization / destruction of the operation method is called before
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 is called before we know whether it is through the realization of InitializingBean / DisposableBean interface, or through the init- elements method / destroy-method attribute can be configured for the Bean can only specify an initialization / destruction methods. But the use of annotation @ PostConstruct and @ PreDestroy but you can specify more than one initialization / destruction methods, or those who have been marked @ PostConstruct @ PreDestroy annotated method will be initialized / destroyed is executed, generally designated one, to avoid confusion < br />
package com.baobaotao;
import javax.annotation.Resource;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
publicclass Boss {
@ Resource
private Car car;
@ Resource (name = “office”)
private Office office;
@ PostConstruct
publicvoid postConstruct1 () {
System.out.println (“postConstruct1″);
}
@ PreDestroy
publicvoid preDestroy1 () {

System.out.println (“preDestroy1″);
}

}
test class code
package com.baobaotao;
import org.springframework.context.support.ClassPathXmlAp plicationContext;
publicclass AnnoIoCTest {
publicstaticvoid 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 executed, and marked with @ PreDestroy comments preDestroy1 () method will be destroyed before the closure of the Spring container Boss Bean is triggered when the execution.
use simplify configuration
Spring 2.1 adds a new context of Schema namespace, the namespace of the annotation-driven, the introduction of property file , load-time weaver into other functions provide a convenient configuration. We know that comment alone will not do anything, it only provides metadata information. For metadata information that really works, must deal with the metadata of the processor to work.
We previously described AutowiredAnnotationBeanPostProcessor and CommonAnnotationBeanPostProcessor is to deal with these annotations metadata processor. However, a Spring Bean configuration file to define these seemed more clumsy. Spring provides us a convenient way to register these BeanPostProcessor, which is . Consider the following configuration:

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
http: / / www.springframework.org / schema / beans / spring-bean s-2.5.xsd
http://www.springframework.org/schema/context
http : / / www.springframework.org / schema / context / spring-co ntext-2.5.xsd “>





< br />


the Spring container to implicitly registered AutowiredAnnotationBeanPostProcessor, CommonAnnotationBeanPostProcessor, PersistenceAnnotationBeanPostProcessor and equiredAnnotationBeanPostProcessor four BeanPostProcessor.
in the configuration file using the context namespace, you must element in the context namespace declarations.

Posted: January 6th, 2012
at 11:18am by admin

Tagged with


Categories: Uncategorized

Comments: No comments


Think E-Cigarette, Think Kimree!