06_init()和destroy()方法
【工程截图】

【HelloWorld.java】
package com.HigginCui;
public class HelloWorld {
public HelloWorld(){
System.out.println("Create Object...");
}
public void init(){
System.out.println("init method...");
}
public void hello(){
System.out.println("hello world...");
}
public void destroy(){
System.out.println("destroy method...");
}
}
【applicationContext.xml】
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!--
init-method:初始化方法
destroy-method:销毁方法
-->
<bean id="helloWorld"
class="com.HigginCui.HelloWorld"
init-method="init"
destroy-method="destroy">
</bean>
</beans>
【testHelloWorld.java】
package com.HigginCui.test; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.HigginCui.HelloWorld; public class testHelloWorld {
@Test
public void test(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld helloWorld=(HelloWorld) context.getBean("helloWorld");
helloWorld.hello(); //关闭Spring容器(为了执行destroy方法,一般spring不关闭)
ClassPathXmlApplicationContext applicationContext=(ClassPathXmlApplicationContext) context;
applicationContext.close();
}
}
【运行结果】
2016-6-10 15:23:34 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@26ee7a14: display name [org.springframework.context.support.ClassPathXmlApplicationContext@26ee7a14]; startup date [Fri Jun 10 15:23:34 CST 2016]; root of context hierarchy
2016-6-10 15:23:34 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
2016-6-10 15:23:34 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@26ee7a14]: org.springframework.beans.factory.support.DefaultListableBeanFactory@2b5575e0
2016-6-10 15:23:34 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2b5575e0: defining beans [helloWorld]; root of factory hierarchy
Create Object...
init method...
hello world...
2016-6-10 15:23:34 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@26ee7a14: display name [org.springframework.context.support.ClassPathXmlApplicationContext@26ee7a14]; startup date [Fri Jun 10 15:23:34 CST 2016]; root of context hierarchy
2016-6-10 15:23:34 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
信息: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2b5575e0: defining beans [helloWorld]; root of factory hierarchy
destroy method...
【小结】
从运行结果可以看出,先运行HelloWorld()构造方法,然后运行init()初始化方法,然后再运行调用的hello()的方法,最后关闭spring容器时运行destroy()销毁方法。
06_init()和destroy()方法的更多相关文章
- JavaEE开发之Spring中Bean的作用域、Init和Destroy方法以及Spring-EL表达式
上篇博客我们聊了<JavaEE开发之Spring中的依赖注入以及AOP>,本篇博客我们就来聊一下Spring框架中的Bean的作用域以及Bean的Init和Destroy方法,然后在聊一下 ...
- laravel中delete()方法和destroy()方法的区别
delete()方法是实例方法,需要查询到相应的数据并通过模型实例调用 destroy()方法可以直接调用,通过索引删除记录 举个栗子: /*delete()方法删除*/ //先查找记录 $blog ...
- ServletContext结合Servlet接口中的init()方法和destroy()方法的运用----网站计数器
我们一般知道Servlet接口中的init()方法在tomcat启动时调用,destroy()方法在tomcat关闭时调用.那么这两个方法到底在实际开发中有什么作用呢?这就是这个随笔主要讲的内容. 思 ...
- 【Spring实战】—— 4 Spring中bean的init和destroy方法讲解
本篇文章主要介绍了在spring中通过配置init-method和destroy-method方法来实现Bean的初始化和销毁时附加的操作. 在java中,我们并不需要去管理内存或者变量,而在C或C+ ...
- cocos creator destroy方法
node.destroy(),Node.destroyAllChildren并不会立即销毁,实际销毁操作会延迟到当前帧渲染前执行. 这段话可能不明白,但是在Node.destroyAllChildre ...
- httpservlet在创建实例对象时候默认调用有参数的init方法 destroy()方法 service方法, 父类的init方法给子类实例一个config对象
- 代码 | 自适应大邻域搜索系列之(3) - Destroy和Repair方法代码实现解析
前言 上一篇文章中我们具体解剖了ALNS类的具体代码实现过程,不过也留下了很多大坑.接下来的文章基本都是"填坑"了,把各个模块一一展现解析给大家.不过碍于文章篇幅等原因呢,也不会每 ...
- 解决 jquery dialog 弹框destroy销毁方法不能把弹出元素设置成初始状态
在使用jquery ui中的dialog弹出窗口的时候遇到一个问题,就是页面弹出窗口关闭后希望表单元素能回到初始状态 例如文本框输入内容后关闭dialog后里面的内容清除,使用了destroy方法也不 ...
- angularjs中ckeditor的destroy问题
项目中,在切换了页面的tab页后会发现上传图片的操作报错,检查后发现问题根源是切换了tab页重新加载页面时ckeditor又会创建一次,这个时候的ckeditor已经不是第一次创建的那个了,所以上传图 ...
随机推荐
- iOS 正则表达式小结
#pragma mark - 正则第一种表示方式-利用NSPredicate(谓词)匹配// NSString *email = @"15078357696@163.com" ...
- ZZTHX-Androidannotations框架联想
我们首先来看一段代码: 在android开发中findViewById是最常用的一个方法,用来实例化页面上的控件,基本上每个控件都需要调用一次的,加入我们页面上有100个需要使用,那么findView ...
- 删除浏览器浏览器删除cookie方法
上班之余抽点时光出来写写博文,希望对新接触的朋友有帮助.今天在这里和大家一起学习一下删除浏览器 文章目录导航 适用范围及演示工具 什么是cookie? cookie有什么作用? ie6/ie7/ie8 ...
- 从零开始学android开发-字符如何转换整形 string 转化为int
int i = Integer.parseInt(string);
- 2015北京网络赛 G题 Boxes bfs
Boxes Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/contest/acmicpc2015beijingonl ...
- spring关于“transactionAttributes”的相关配置
spring关于"transactionAttributes"的相关配置 <bean id="baseTransactionProxy" class=&q ...
- 声明:function FileSetAttr ( const FileName : string
对文件和文件夹都有效 FileSetAttr('D:\Administrator\Desktop\patcher\Win32\Release\config\element\update',faHid ...
- 杭电 2602 Bone Collector
Bone Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- IPVS
http://kb.linuxvirtualserver.org/wiki/IPVS_FULLNAT_and_SYNPROXY
- Mysql导出表结构及表数据 mysqldump用法
几个常用用例: 1.导出整个数据库 mysqldump -u 用户名 -p 数据库名 > 导出的文件名 mysqldump -u wcnc -p smgp_apps_wcnc > ...