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已经不是第一次创建的那个了,所以上传图 ...
随机推荐
- paip.云计算以及分布式计算的区别
paip.云计算以及分布式计算的区别 云计算的特点 1 网格计算 2 分布式计算 2 云计算以及网格计算以及分布式计算的区别 2 作者Attilax 艾龙, EMAIL:1466519819@qq ...
- 使用JavaScript实现分页效果
应公司需求,有时需要制作静态页面网站,而一旦涉及到文章的分页,实现起来非常麻烦,自己又刚入门,对js不是很熟悉,所以就拿来练练手. 首先页面结构如下,分别展示数据跟分页: <!DOCTYPE h ...
- Volley使用指南第一回(来自developer.android)
最近闲来想看看android网络方面的东西.google在2013年发布了一个叫做Volley的网络请求框架,我看了一下官网,居然在training里面就有教程.首先,英文的东西看着 还是挺不爽的,特 ...
- Using breakpad in cocos2d-x 3.2,dump信息收集
作者:HU 转载请注明,原文链接:http://www.cnblogs.com/xioapingguo/p/4037268.html 一.基本步骤 1.生成转换工具 2.把breakpad加入到项目 ...
- 在与SQL Server建立连接时出现与网络相关的或特定于实例的错误
向往前一样.学习牛腩新闻公布系统的视频,写程序,打开数据库.出现一个框框,具体内容例如以下: 数据库连接不上.全部的工作都要歇班,捣鼓了会儿,简单总结一下解决该问题的方法. 首先:第一步,程序--SQ ...
- RotatingDoughnut
RotatingDoughnut.zip
- WordPress搭建Personal Blog
早就想搭建一个专属于自己的博客了,用来记录自己生活.学习的点点滴滴.之所以选WordPress,主要是因为它可以支持Latex,而且特别喜欢其简约的风格. WordPress有个the famous ...
- 聊聊 iOS 开发中的协议
前言 何为协议,简单来说在OC中我们使用关键字@protocol可以声明一个协议,并在协议中添加多个属性.方法供于遵循者实现,从某个角度上来说,这是一种不同于category机制的category.在 ...
- iOS 开发中你是否遇到这些经验问题(二)
前言: 1.在Block中一起使用weakSelf与strongSelf的含义 我们都会声明一个弱引用在block中使用, 目的就是防止循环引用, 那么weakSelf与strongSelf一起使用目 ...
- Android_Menu_contextMenu
xml文件: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns ...