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

关于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 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记