博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring bean 实现生命周期的三种解决方案
阅读量:4261 次
发布时间:2019-05-26

本文共 3334 字,大约阅读时间需要 11 分钟。

解决方案一:通过XML配置文件实现:(标签bean的属性init-method和destroy-method)

beans.xml:

 

SimpleBean.java:

 

 

package org.spring.tutorial;public class SimpleBean {	public SimpleBean() {		System.out.println("SimpleBean called");	}		public void init() {		System.out.println("init called");	}		public void destroy() {		System.out.println("destroy called");	}	}

 

MainApp.java:

 

package org.spring.tutorial;import org.springframework.context.support.AbstractApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainApp {	public static void main(String[] args) {		AbstractApplicationContext context 		= new ClassPathXmlApplicationContext("beans.xml"); // 加载指定XML文件的定义		context.registerShutdownHook();	// 注册关闭钩子	}}

运行之:

 

 

SimpleBean calledinit called五月 19, 2013 5:59:18 下午 org.springframework.context.support.AbstractApplicationContext doCloseINFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@1286c71: startup date [Sun May 19 17:59:18 CST 2013]; root of context hierarchydestroy called

解决方案二:实现Spring的InitializingBean和DisposableBean。

 

SimpleBean.java:

 

package org.spring.tutorial;import org.springframework.beans.factory.DisposableBean;import org.springframework.beans.factory.InitializingBean;public class SimpleBean implements InitializingBean,DisposableBean {	public SimpleBean() {		System.out.println("SimpleBean called");	}	@Override	public void destroy() throws Exception {		System.out.println("destroy called");	}	@Override	public void afterPropertiesSet() throws Exception {		System.out.println("afterPropertiesSet called");	}		}

beans.xml:

 

 

运行结果:

 

 

SimpleBean calledafterPropertiesSet called五月 19, 2013 6:03:44 下午 org.springframework.context.support.AbstractApplicationContext doCloseINFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@e77ca4: startup date [Sun May 19 18:03:44 CST 2013]; root of context hierarchydestroy called

解决方案三:利用javax.annotation包的注解PostConstruct和PreDestroy。

 

SimpleBean.java:

 

package org.spring.tutorial;import javax.annotation.PostConstruct;import javax.annotation.PreDestroy;public class SimpleBean {	public SimpleBean() {		System.out.println("SimpleBean called");	}	@PreDestroy	public void destroy() throws Exception {		System.out.println("destroy called");	}	@PostConstruct	public void afterPropertiesSet() throws Exception {		System.out.println("afterPropertiesSet called");	}		}

beans.xml:

 

 

运行结果:

 

 

SimpleBean called五月 19, 2013 6:06:39 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletonsINFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@431425: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,bean,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchyafterPropertiesSet calleddestroy called

总结:可以利用接口(InitializingBean,DisposableBean),注解(@PreDestroy,@PostConstruct)和XML配置文件(bean的属性init-method,destroy-method)来实现bean的生命周期。可以根据个人的喜好选择不同的方案。

转载地址:http://xrxei.baihongyu.com/

你可能感兴趣的文章
JavaScript中使用offset时遇到的bug
查看>>
java基础入门(一)
查看>>
Java基础入门(二)
查看>>
Java基础入门(三)
查看>>
Java基础入门(四)
查看>>
Java基础入门(十)
查看>>
Java基础入门(完结篇)
查看>>
Java进阶之面向对象(一)——继承
查看>>
Java进阶之自定义ArrayList&斗地主发牌案例
查看>>
JavaWeb之filter&listener&文件上传
查看>>
JavaWeb之Ajax&json
查看>>
BUFG,IBUFG,BUFGP,IBUFGDS等含义以及使用
查看>>
转载:在 Windows 10 下遇到移动硬盘不自动分配盘符的问题
查看>>
DDR2 SSTL_18标准
查看>>
DDR3的DQS_p/n信号电平摆幅变化不一致现象
查看>>
北大旁听生中的历史名人
查看>>
大唐凌烟阁开国廿四将
查看>>
Access数据库出现"Selected collating sequence not supported by the operating system."错误
查看>>
逻辑思维测试题
查看>>
如何用Easy CHM制作CHM格式电子书(帮助文档)
查看>>