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:
[Java]View PlainCopy
- Package com.myapp.core.annotation.init;
- Import javax.annotation.PostConstruct;
- Import Javax.annotation.PreDestroy;
- Public class Personservice {
- private String message;
- Public String GetMessage () {
- return message;
- }
- public void Setmessage (String message) {
- this.message = message;
- }
- @PostConstruct
- public void init () {
- System.out.println ("I ' m init method using @PostConstrut ...." +message);
- }
- @PreDestroy
- public void Dostory () {
- System.out.println ("I ' m destory method using @PreDestroy ..." +message);
- }
- }
2: Define the relevant configuration file:
[HTML]View PlainCopy
- <? 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: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 />
- <Bean id= "personservice" class="Com.myapp.core.annotation.init.PersonService">
- < name="message" value="123"> </Property>
- </Bean>
- </Beans>
Where <context:annotation-config/> tells the spring container to use annotation configuration: Scan annotation configuration;
Test class:
[Java]View PlainCopy
- Package com.myapp.core.annotation.init;
- Import Org.springframework.context.ApplicationContext;
- Import Org.springframework.context.support.ClassPathXmlApplicationContext;
- Public class Maintest {
- public static void Main (string[] args) {
- ApplicationContext context = new Classpathxmlapplicationcontext ("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:
[HTML]View PlainCopy
- <? 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: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/>--
- <Bean class="Org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
- <Bean id= "personservice" class="Com.myapp.core.annotation.init.PersonService">
- < name="message" value="123"> </Property>
- </Bean>
- </Beans>
The output of the above test can also be obtained.
Spring implements operations prior to initializing and destroying beans in three different ways