關於在spring 容器初始化 bean 和銷燬前所作的操做定義方式有三種:java
第一種:經過@PostConstruct 和 @PreDestroy 方法 實現初始化和銷燬bean以前進行的操做web
第二種是:經過 在xml中定義init-method 和 destory-method方法spring
第三種是: 經過bean實現InitializingBean和 DisposableBean接口app
下面演示經過 @PostConstruct 和 @PreDestory編輯器
1:定義相關的實現類:post
- 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:定義相關的配置文件:
- <?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:annotation-config />
-
- <bean id="personService" class="com.myapp.core.annotation.init.PersonService">
- <property name="message" value="123"></property>
- </bean>
-
- </beans>
其中<context:annotation-config />告訴spring 容器採用註解配置:掃描註解配置;
測試類:測試
- 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();
- }
-
- }
測試結果:
I'm init method using @PostConstrut....123
I'm destory method using @PreDestroy.....123
this
其中也能夠經過申明加載org.springframework.context.annotation.CommonAnnotationBeanPostProcessorspa
類來告訴Spring容器採用的 經常使用 註解配置的方式:
只須要修改配置文件爲:
- <?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">
-
-
-
-
- <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
- <bean id="personService" class="com.myapp.core.annotation.init.PersonService">
- <property name="message" value="123"></property>
- </bean>
-
-
-
- </beans>
一樣能夠獲得以上測試的輸出結果。