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已经不是第一次创建的那个了,所以上传图 ...
随机推荐
- VB操作CAD
Dim xlapp As Excel.Application Dim xlbook As Excel.Workbook Dim sheet As Excel ...
- Central Authentication Service
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- Android下得到已安装Application信息
在上一篇blog中,谈到如何利用APK archive文件得到相应信息.(当时发现例如ProcessName,DataDir等信息,其实是无法得到的). 当前咱们看看如何通过系统取得已经安装的Appl ...
- [WebGL入门]二,開始WebGL之前,先了解一下canvas
年2月)HTML5依旧处于草案阶段. HTML5支持网页端的多媒体功能和画布功能,追加了非常多全新的更合理的Tag标签.各个浏览器也都在逐渐的完好这些新的特性. Canvas对象表示一个 HTML画布 ...
- 解决Windows2008Server上PLSQL登录时报ORA-12557
公司的Oracle服务端是安装在一台Linux服务器上,版本号为11.1.0.7.0.我们开发的系统部署在Windows 2008 Server(x64),因为偶尔需要调用Oracle数据库,所以最开 ...
- Android ADB启动失败 ADB server out of date
调试Android程序时总提示ADB未运行,转到命令行方式下启动ADB, adb kille-server停止ADB, 然后再运行adb start-server 随后提示: adb server i ...
- Oracle database server 安装tips
需要手动解压第二个包的文件合并到第一个包的相同目录中. 以12c为例,需要把 winx64_12102_SE2_database_1of2.zip和winx64_12102_SE2_database_ ...
- python--字符工厂函数dict()
字符工厂函数str() class str(object): """ str(object='') -> str str(bytes_or_buffer[, enc ...
- 对setTimeout()第一个参数是字串的深入理解以及eval函数的理解
<script language="javascript" type="text/javascript"> var a=1; setTimeout( ...
- 关于Eclipse平台的使用和开发第一个SWT程序
IBM把投入巨资开发的Eclipse作为一个开源项目捐献给了开源组织Eclipse.org Eclipse出色而有独创的平台特性,现在仍由IBM子公司OTI(主要从事Eclipse开发的人)继续领导着 ...