There are three ways to define operations before the spring container initializes the bean and destroys it:
First: Operations performed prior to initializing and destroying beans through @postconstruct and @PreDestroy methods
The second is: by defining the Init-method and Destory-method methods in XML
The third is the implementation of the Initializingbean and Disposablebean interfaces via beans
The following shows the @PostConstruct and @PreDestory
1: Define the relevant implementation class:
PackageCom.myapp.core.annotation.init; Importjavax.annotation.PostConstruct; ImportJavax.annotation.PreDestroy; Public classPersonservice {PrivateString message; PublicString getMessage () {returnmessage; } Public voidsetmessage (String message) { This. Message =message; } @PostConstruct Public voidinit () {System.out.println ("I ' m init method using @PostConstrut ..." +message); } @PreDestroy Public voiddostory () {System.out.println ("I ' m destory method using @PreDestroy ..." +message); } }
2: Define the relevant configuration file:
<?XML version= "1.0" encoding= "UTF-8"?> <Beansxmlns= "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 http://www.springframework.org/schema/beans/ Spring-beans-3.1.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-3.1.xsd "> <!--<context:component-scan base-package= "com.myapp.core.jsr330"/> - <Context:annotation-config/> <BeanID= "Personservice"class= "Com.myapp.core.annotation.init.PersonService"> < Propertyname= "message"value= "123"></ Property> </Bean> </Beans>
Where <context:annotation-config/> tells the spring container to use annotation configuration: Scan annotation configuration;
Test class:
PackageCom.myapp.core.annotation.init; ImportOrg.springframework.context.ApplicationContext; ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classMaintest { Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Resource/annotation.xml"); Personservice Personservice= (Personservice) context.getbean ("Personservice"); Personservice.dostory (); } }
Test results:
I ' m init method using @PostConstrut .... 123
I ' m destory method using @PreDestroy ..... 123
The org.springframework.context.annotation.CommonAnnotationBeanPostProcessor can also be loaded by stating
class to tell the spring container how to use the Common annotation configuration:
You only need to modify the configuration file to:
<?XML version= "1.0" encoding= "UTF-8"?> <Beansxmlns= "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 http://www.springframework.org/schema/beans/ Spring-beans-3.1.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-3.1.xsd "> <!--<context:component-scan base-package= "com.myapp.core.jsr330"/> - <!--<context:annotation-config/> - <Beanclass= "Org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /> <BeanID= "Personservice"class= "Com.myapp.core.annotation.init.PersonService"> < Propertyname= "message"value= "123"></ Property> </Bean> </Beans>
The output of the above test can also be obtained.
Operations prior to initializing and destroying beans through spring @PostConstruct and @PreDestroy methods