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
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-
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
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
Xml version = "1.0" encoding = "UTF-8"?>
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 “>
bean>
beans>
in the configuration file using the context namespace, you must
Posted: January 6th, 2012
at 11:18am by admin
Tagged with disposablebean
Categories: Uncategorized
Comments: No comments













