发现当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
销毁了~