- 浏览: 967732 次
文章分类
最新评论
-
18335864773:
很多公司项目 都在使用pageoffice 来操作word,e ...
用java生成word文档 -
Gozs_cs_dn:
请问下博主, 怎样将sitemesh3.xsd绑定 sitem ...
SiteMesh3配置 -
Rose_06:
springside4.0quick-start.bat报错原因 -
ilemma:
我也是刚参见工作啊,经理让自学这个,有些东西不太懂,能不能发个 ...
Apache Shiro在Web中的应用 -
shanbangyou:
你废了
程序员上班打酱油的方法
private ContextLoader contextLoader;
/**
* Initialize the root web application context.
*/
public void contextInitialized(ServletContextEvent event) {
this.contextLoader = createContextLoader();
this.contextLoader.initWebApplicationContext(event.getServletContext());
}
/**
* Create the ContextLoader to use. Can be overridden in subclasses.
* @return the new ContextLoader
*/
protected ContextLoader createContextLoader() {
return new ContextLoader();
}
/**
* Return the ContextLoader used by this listener.
*/
public ContextLoader getContextLoader() {
return contextLoader;
}
/**
* Close the root web application context.
*/
public void contextDestroyed(ServletContextEvent event) {
this.contextLoader.closeWebApplicationContext(event.getServletContext());
}
}
servletContext.log("Closing root WebApplicationContext");
Object wac = servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
if (wac instanceof ConfigurableApplicationContext) {
((ConfigurableApplicationContext) wac).close();
}
}
AbstractApplicationContext.Close的代码 :
logger.info("Closing application context [" + getDisplayName() + "]");
// Destroy all cached singletons in this context,
// invoking DisposableBean.destroy and/or "destroy-method".
getBeanFactory().destroySingletons();
// publish corresponding event
publishEvent(new ContextClosedEvent(this));
}
最终还是调用到了getBeanFactory().destroySingletons(); 看来,没有容器,我们还是需要自己来搞定这个方法的调用的 !
- 作者: hpq852 2004年12月28日, 星期二 18:07
举个例子,ContextLoaderListener的源代码,
我们知道,如果要在tomcat里面使用spring的话需要这个Listener(或者ContextLoaderServlet)
public class ContextLoaderListener implements ServletContextListener {
private ContextLoader contextLoader;
/**
* Initialize the root web application context.
*/
public void contextInitialized(ServletContextEvent event) {
this.contextLoader = createContextLoader();
this.contextLoader.initWebApplicationContext(event.getServletContext());
}/**
* Create the ContextLoader to use. Can be overridden in subclasses.
* @return the new ContextLoader
*/
protected ContextLoader createContextLoader() {
return new ContextLoader();
}/**
* Return the ContextLoader used by this listener.
*/
public ContextLoader getContextLoader() {
return contextLoader;
}/**
* Close the root web application context.
*/
public void contextDestroyed(ServletContextEvent event) {
this.contextLoader.closeWebApplicationContext(event.getServletContext());
}}
当tomcat关闭的时候会自动调用contextDestroyed(ServletContextEvent event)这个方法。在看一下contextLoader的closeWebApplicationContext方法:
public void closeWebApplicationContext(ServletContext servletContext) throws ApplicationContextException {
servletContext.log("Closing root WebApplicationContext");
Object wac = servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
if (wac instanceof ConfigurableApplicationContext) {
((ConfigurableApplicationContext) wac).close();
}
}
就应该明白文档里面是什么意思了,AbstractApplicationContext.Close这个方法是要你自己调用的,在程序要结束的时候保证调用这个close方法,在这里的话就是由Listener来保证tomcat退出的时候调用close方法。
AbstractApplicationContext.Close的代码
public void close() {
logger.info("Closing application context [" + getDisplayName() + "]");
// Destroy all cached singletons in this context,
// invoking DisposableBean.destroy and/or "destroy-method".
getBeanFactory().destroySingletons();// publish corresponding event
publishEvent(new ContextClosedEvent(this));
}
其实就是调用context里面的beanFactory的destroySingletons()方法了,这个没什么好说的。我的意思就是,容器本身不知道什么时候要shutdown了,这个消息是要靠外部(程序员)来提供的。
de3light
发表评论
-
SpringMVC使用@ResponseBody中文乱码
2012-03-31 11:45 3069Spring3.0 MVC @ResponseBody 的 ... -
spring mvc 3 上传文件
2012-03-30 16:31 1239spring mvc 支持web应用程序的文件上传功能,是由 ... -
解决 spring mvc 3.0 结合 hibernate3.2 使用<tx:annotation-driven>声明式事务无法提交的问题
2011-10-29 09:23 111241、问题复现 spring 3.0 + hiber ... -
基于Spring框架的Shiro配置
2011-10-26 15:39 3113一、在web.xml中添加shiro过滤器 < ... -
spring3 混合使用 XML 与注解(Annotation)进行 Bean 的配置
2011-10-21 17:34 3634设计 @Configuration 和 @Bean 的初衷,并 ... -
springMVC最佳实践-基于注解的Controller
2011-10-13 23:09 894在spring 2.5发布之后,基于注解的Controller ... -
springmvc3-hibernate-crud例子
2011-10-12 23:45 626http://java.dzone.com/articles/ ... -
ExtJS, Spring MVC 3 and Hibernate 3.5: CRUD例子
2011-10-12 23:45 1278http://loianegroner.com/2010/09 ... -
spring mvc3 annotation例子
2011-10-12 23:43 1838服务端数据到客户端1.返回页面,Controller中方法返回 ... -
Spring Web MVC框架学习笔记
2011-10-12 21:22 2198spring-framework-reference中文版摘录 ... -
Mastering Spring MVC 3中文版
2011-10-11 23:00 3173Mastering Spring MVC 3中文版 精通Sp ... -
基于Myeclipse9.1的spring3.1MVC开发搭建
2011-10-01 14:27 1991(1)配置基础装备。每个符合Java EE规范的web应用程 ... -
Spring Security 设置session
2011-04-02 15:26 3879使用SecurityContextHolder来偷窥登入帐号密 ... -
springsecurity的URL过滤和全局过滤
2011-03-31 15:55 4760springsecurity的URL过滤和全局过滤 最近有个需 ... -
权限控制:spring 3.0 security配置例子
2011-03-31 15:21 1891我几年前自己写过一个 ... -
SpringSide 3 中的 Struts 2
2011-03-03 18:01 872在SpringSide 3 中,使用的MVC框架是Struts ... -
springside 中 报 org.hibernate.MappingException: Unknown entity: XXX
2011-03-03 17:25 1217org.hibernate.MappingException: ... -
springside 迁移sqlserver数据库报错
2011-03-02 16:55 798报错信息: java.lang.AbstractMethodE ... -
使用SpringSide 3.1.4.3开发Web项目的全过程(上)
2011-03-01 17:41 1093http://www.blogjava.net/youxia/ ... -
MultiActionController绑定command
2007-06-03 08:56 2517dlpower: 一般只见到用SimpleFormContro ...
相关推荐
2)default-destroy-method="方法名" 定义在此配置文件中的bean都会在执行指定的destroy方法。 3)default-lazy-init ="false|true" 定义在此配置文件中的bean都会延迟加载。 ....................
Spring IOC容器可以管理Bean的生命周期,允许在Bean生命周期的特定点执行定制的任务。 Spring IOC容器对Bean的生命周期...在 Bean 的声明里设置 init-method 和 destroy-method 属性, 为 Bean 指定初始化和销毁方法。
spring Ioc容器配置 ... destroy-method="close"> <value>org.gjt.mm.mysql.Driver <value>jdbc:mysql://localhost:3306/demo <value>root <value>root </bean>
资源释放:<bean destroy-method=""/>仅对单例对象有效 (2)IoC概念 Inversion of Control 控制反转或控制转移 Don't Call Me,We will call you! 控制权:对象的创建和调用关系的控制. (3)DI概念 Dependecy ...
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
EWA讲义 Hacettepe大学企业Web体系结构讲座的幻灯片和示例应用程序 应用清单: AjaxApp:基于Eclipse的Web项目,使用jQuery演示...spring-bean-lifecycle:使用init-method,destroy-method,InitializingBean,Disp
Spring IOC Bean标签属性介绍 0.Bean标签属性介绍 1.0 新建一个Maven工程 1.1 pom.xml ...1.9 init-method和destroy-method 1.9.1 实体类JavaBean User加自定义的初始化方法和销毁方法 1.9.3 加了lazy
<bean id="cachingClient" class="com.dmx.cache.caching.impl.CachingXMemcachedClient" init-method="init" destroy-method="destroy"> ${XMemcached_isflushAll}" /> ...
资源释放:<bean destroy-method=""/>仅对单例对象有效 (2)IoC概念 Inversion of Control 控制反转或控制转移 Don't Call Me,We will call you! 控制权:对象的创建和调用关系的控制. (3)DI概念 Dependecy ...
<bean id="dataSourceA" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown"> <bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown"> ...
资源释放:<bean destroy-method=""/>仅对单例对象有效 (2)IoC概念 Inversion of Control 控制反转或控制转移 Don't Call Me,We will call you! 控制权:对象的创建和调用关系的控制. (3)DI概念 Dependecy ...
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> ...
为了定义一个 bean 的安装和卸载,我们可以使用 init-method 和 destroy-method 参数简单的声明一下 ,其中 init-method 属性指定了一个方法,该方法在 bean 的实例化阶段会立即被调用;同样地,destro
destroy-method API BeanFactory:使用这个工厂创建对象的方式都是懒加载,在调用的时候再创建 ClassPathXmlApplicationContext:使用这个工厂创建对象,他会根据scope智能判断是否懒加载,如果是单例则创建容器时...
JSP Spring中Druid连接池配置 jdbc.properties url=jdbc:postgresql://***.... <bean id=dataSource class=com.alibaba.druid.pool.DruidDataSource init-method=init destroy-method=close> <!-- 基本属性 url、u
(二) init-method destroy-method 不要和prototype一起用(了解) 15 第六课:annotation方式Spring 16 一、 开始使用annotation配置Spring 16 二、 @Autowired、@Qualifier 16 (一) @Autowired 16 (二) @Qualifier ...
<bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource" destroy-method="close"> <value>org.gjt.mm.mysql.Driver <value>jdbc:mysql://localhost/manytomany?useUnicode=true&...
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/> ...