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已经不是第一次创建的那个了,所以上传图 ...
随机推荐
- ecshop去官方化的修改
1:如何修改网站"欢迎光临本店" 回答:languages\zh_cn\common.php文件中, $_LANG['welcome'] = '欢迎光临本店';将他修改成你需要的字 ...
- 修改cas登陆页面-服务器端
原文地址:http://www.cnblogs.com/liveandevil/archive/2013/03/06/2946341.html 1.cas统一认证的登陆页面位于:cas目录/WEB-I ...
- UIAlertViewController+TextField 输入框
if (IOS8) { UIAlertController *alertController=[UIAlertController alertControllerWithTitle:CustomLoc ...
- CentOS 6.5安装TortoiseSVN svn client
TortoiseSVN: TortoiseSVN 是 Subversion 版本号控制系统的一个免费开源client,能够超越时间的管理文件和文件夹. 文件保存在中央版本号库,除了能记住文件和文件夹的 ...
- Codeforces Round #324 (Div. 2) C. Marina and Vasya 贪心
C. Marina and Vasya Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/584/pr ...
- 基于S7-200的PLC对里程轮(增量式码盘)解码的应用
解码模块为JC-11:工业增量式码盘 解码模块,接口简单,易于使用. 应用Step7-MicroWIN编程软件,为S7-200PLC设计本编码盘的应用程序.由于编码盘输出的脉冲信号频 ...
- iOS音频篇:AVPlayer的缓存实现
授权转载,作者:明仔Su(简书) 在上一篇文章<使用AVPlayer播放网络音乐>介绍了AVPlayer的基本使用,下面介绍如何通过AVAssetResourceLoader实现AVPla ...
- 面试题总结之JAVA
JAVA 1. what is thread safe? 线程安全就是说多线程访问同一代码,不会产生不确定的结果.编写线程安全的代码是低依靠线程同步.线程安全: 在多线程中使用时,不用自已做同步处理线 ...
- java使用Thumbnailator操作图片
Thumbnailator 是一个用来生成图像缩略图.裁切.旋转.添加水印等操作的 Java 类库,通过很简单的代码即可生成图片缩略图,也可直接对一整个目录的图片生成缩略图. Thumbnailato ...
- window.external.notify() 与 UglifyJS 压缩优化冲突
近期研究了一下 UglifyJs 对 JS 代码的压缩,发现 UglifyJS 压缩后,无法调用 window.external.notify() 方法,JS 代码如下: function MyNot ...