iteye_13574 2009-01-21 11:37
浏览 955
已采纳

关于Spring2.5 @PreDestory注释,销毁bean时不执行

发现当scope="prototype" ,spring容器销毁对象的时候 @PreDestory 注释的方法不会执行,
scope="singleton" 的 @PreDestory 才会执行。。。
请问这是为什么啊? ~

[code="java"]
@Component("pService")
@Scope("prototype")
public class PersonServiceImpl implements PersonService {

@PostConstruct
public void init() {
    System.out.println("初始化咯~");
}

@PreDestroy  //这里spring容器销毁的时候并没有执行....
public void destory(){
    System.out.println("销毁了~");
}

}

[/code]

如果是prototype:
初始化咯~
Prototype !!!
Saving in PersonServiceImpl...null
Saving in PersonDaoImpl ...
2009-1-21 11:34:22 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@280387: display name [org.springframework.context.support.ClassPathXmlApplicationContext@280387]; startup date [Wed Jan 21 11:34:20 CST 2009]; root of context hierarchy
2009-1-21 11:34:22 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
信息: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2a3c6f: defining beans [pDao,pService,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor]; root of factory hierarchy

如果是singleton:
初始化咯~
Singleton !!!
Saving in PersonServiceImpl...null
Saving in PersonDaoImpl ...
2009-1-21 11:35:39 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@280387: display name [org.springframework.context.support.ClassPathXmlApplicationContext@280387]; startup date [Wed Jan 21 11:35:37 CST 2009]; root of context hierarchy
2009-1-21 11:35:39 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
信息: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2a3c6f: defining beans [pDao,pService,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor]; root of factory hierarchy
销毁了~

  • 写回答

1条回答 默认 最新

  • christ_1 2009-01-21 12:01
    关注

    [code="java"]@Scope("prototype") [/code]定义为prototype类型的实例创建之后spring就不在管理了,它只是做了new操作而已

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog