(转)Spring 的 init-method 和 destory-method

背景:今天在项目中看到spring中bean在初始化和注销时候的方法定义,之前没有用过这种方式,在此记录下,方便后期查看!

关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种:

第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作

第二种是:通过 在xml中定义init-method 和 destory-method方法

第三种是: 通过bean实现InitializingBean和 DisposableBean接口

1 通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作

项目结构:

(转)Spring 的 init-method 和 destory-method

定义相关实现类:

package com.lxw.study01;
/**
* @author lxw
* @describe
* @date 2017年7月25日 下午2:30:53
*/
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);
} }

定义相关配置文件annotation.xml:

<?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.lxw.study01.PersonService">
<property name="message" value="123"></property>
</bean> </beans>

其中<context:annotation-config />告诉spring 容器采用注解配置:扫描注解配置;

测试类:

package com.lxw.study01;
/**
* @author lxw
* @describe
* @date 2017年7月25日 下午2:29:09
*/
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("annotation.xml"); PersonService personService = (PersonService)context.getBean("personService"); personService.dostory();
} }

运行结果:

I'm  init  method  using  @PostConstrut....123
I'm destory method using @PreDestroy.....123

其中也可以通过申明加载org.springframework.context.annotation.CommonAnnotationBeanPostProcessor

类来告诉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"> <!-- <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">
<property name="message" value="123"></property>
</bean> </beans>

同样可以得到以上测试的输出结果。

2 通过 在xml中定义init-method 和 destory-method方法

项目结构:

(转)Spring 的 init-method 和 destory-method

在xml中配置 init-method和 destory-method方法,只是定义spring 容器在初始化bean 和容器销毁之前的所做的操作

基于xml的配置只是一种方式:

直接上xml中配置文件,定义SpringBeans.xml:

<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> <bean id="personService" class="com.lxw.study.PersonService"
scope="singleton" init-method="init" destroy-method="cleanUp" />
</beans>

ps:单例模式,表示spring容器中只存在一个bean,供程序调用

定义PersonService类:

package com.lxw.study;
/**
* @author lxw
* @describe
* @date 2017年7月25日 上午10:53:27
*/
public class PersonService {
private String message; public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} public void init(){
System.out.println("init");
}
// how validate the destory method is a question
public void cleanUp(){
System.out.println("cleanUp");
}
}

相应的测试类:

package com.lxw.study;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* @author lxw
* @describe
* @date 2017年7月25日 上午10:53:59
*/
public class MainTest {
public static void main(String[] args) { AbstractApplicationContext context =new ClassPathXmlApplicationContext("SpringBeans.xml"); PersonService person = (PersonService)context.getBean("personService"); person.setMessage("hello spring01"); PersonService person_new = (PersonService)context.getBean("personService");
person_new.setMessage("hello spring02"); System.out.println(person.getMessage());
System.out.println(person_new.getMessage());
context.registerShutdownHook(); }
}

测试结果:

init
hello spring02
hello spring02
cleanUp

ps:因为是单例模式,所以程序中修改的是同一个bean,(都输出hello spring02)。正因为是单例模式,所以程序中的初始化和销毁只执行一次。

可以看出 init 方法和 clean up方法都已经执行了。

context.registerShutdownHook(); 是一个钩子方法,当jvm关闭退出的时候会调用这个钩子方法,在设计模式之 模板模式中 通过在抽象类中定义这样的钩子方法由实现类进行实现,这里的实现类是AbstractApplicationContext,这是spring 容器优雅关闭的方法。

3 通过bean实现InitializingBean和 DisposableBean接口

项目结构:

(转)Spring 的 init-method 和 destory-method

定义相应类实现InitializingBean ,DisposableBean 接口

package com.lxw.study02;
/**
* @author lxw
* @describe
* @date 2017年7月25日 下午2:30:53
*/
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean; public class PersonService implements InitializingBean,DisposableBean{ private String message; public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} @Override
public void destroy() throws Exception {
// TODO Auto-generated method stub
System.out.println("I'm init method using implements InitializingBean interface...."+message); } @Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
System.out.println("I'm init method using implements DisposableBean interface...."+message); } }

定义相应的配置文件annotation02.xml:

<?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">
<property name="message" value="123"></property>
</bean>
--> <bean id="personService" class="com.lxw.study02.PersonService">
<property name="message" value="12345"></property>
</bean> </beans>

测试类:

package com.lxw.study02;
/**
* @author lxw
* @describe
* @date 2017年7月25日 下午2:29:09
*/
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainTest { public static void main(String[] args) { AbstractApplicationContext context = new ClassPathXmlApplicationContext("annotation02.xml"); PersonService personService = (PersonService)context.getBean("personService"); context.registerShutdownHook();
} }

测试结果:

I'm  init  method  using implements DisposableBean interface....12345
I'm init method using implements InitializingBean interface....12345
上一篇:(转)error while loading shared libraries:libmysqlclient.so.18 错误


下一篇:创建一个提供数据 API 的 Node.js 网站