Spring bean 实现生命周期的三种解决方案
2013-05-19 20:29
471 查看
解决方案一:通过XML配置文件实现:(标签bean的属性init-method和destroy-method)
beans.xml:
SimpleBean.java:
MainApp.java:
运行之:
解决方案二:实现Spring的InitializingBean和DisposableBean。
SimpleBean.java:
beans.xml:
运行结果:
解决方案三:利用javax.annotation包的注解PostConstruct和PreDestroy。
SimpleBean.java:
beans.xml:
运行结果:
总结:可以利用接口(InitializingBean,DisposableBean),注解(@PreDestroy,@PostConstruct)和XML配置文件(bean的属性init-method,destroy-method)来实现bean的生命周期。可以根据个人的喜好选择不同的方案。
beans.xml:
<beans> <bean id="bean" class="org.spring.tutorial.SimpleBean" init-method="init" destroy-method="destroy" /> </beans>
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 called init called 五月 19, 2013 5:59:18 下午 org.springframework.context.support.AbstractApplicationContext doClose INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@1286c71: startup date [Sun May 19 17:59:18 CST 2013]; root of context hierarchy destroy 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:
<beans> <bean id="bean" class="org.spring.tutorial.SimpleBean" /> </beans>
运行结果:
SimpleBean called afterPropertiesSet called 五月 19, 2013 6:03:44 下午 org.springframework.context.support.AbstractApplicationContext doClose INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@e77ca4: startup date [Sun May 19 18:03:44 CST 2013]; root of context hierarchy destroy 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:
<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.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config /> <bean id="bean" class="org.spring.tutorial.SimpleBean" /> </beans>
运行结果:
SimpleBean called 五月 19, 2013 6:06:39 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons INFO: 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 hierarchy afterPropertiesSet called destroy called
总结:可以利用接口(InitializingBean,DisposableBean),注解(@PreDestroy,@PostConstruct)和XML配置文件(bean的属性init-method,destroy-method)来实现bean的生命周期。可以根据个人的喜好选择不同的方案。
相关文章推荐
- Spring bean 实现生命周期的三种解决方案
- Spring bean 实现生命周期的三种解决方案
- spring bean实现init/destory生命周期方法的三种方式
- Spring bean 实现生命周期的三种解决方案
- 详细理解servlet实现的三种方式和生命周期
- Java中实现文件上传下载的三种解决方案 之一
- Spring Bean 生命周期 三种表示方法
- C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志
- Java中实现文件上传下载的三种解决方案 之二
- Java中实现文件上传下载的三种解决方案之三
- Java中实现文件上传下载的三种解决方案
- jQuery实现动画循环的三种解决方案
- Java中实现文件上传下载的三种解决方案
- Java中实现文件上传下载的三种解决方案
- Windows下使用Apache与Tomcat实现负载均衡/反向代理的三种解决方案
- Java中实现文件上传下载的三种解决方案
- Java中实现文件上传下载的三种解决方案
- Servlet的生命周期和三种实现方式
- Java中实现文件上传下载的三种解决方案
- Java中实现文件上传下载的三种解决方案(推荐)