注解@PreDestory在scope是prototype的时候没有执行,如果scope为prototype则可以执行,请问这是什么原因?
<context:component-scan base-package="com.hiway.entity"></context:component-scan>
<bean name="service" class="com.hiway.entity.UserService" scope="prototype">
</bean>@Component("service")
public class UserService { private User u ; public User getU() {
return u;
} @Resource(name="u")
public void setU(User u) {
this.u = u;
}
@PostConstruct
public void init(){
System.out.println("post constructor");
}
@PreDestroy
public void destroy(){
System.out.println("pre destroy");
}
}
在销毁ClassPathXmlApplicatonContext的时候,没调用@PreDestory的方法,请问为什么?
<context:component-scan base-package="com.hiway.entity"></context:component-scan>
<bean name="service" class="com.hiway.entity.UserService" scope="prototype">
</bean>@Component("service")
public class UserService { private User u ; public User getU() {
return u;
} @Resource(name="u")
public void setU(User u) {
this.u = u;
}
@PostConstruct
public void init(){
System.out.println("post constructor");
}
@PreDestroy
public void destroy(){
System.out.println("pre destroy");
}
}
在销毁ClassPathXmlApplicatonContext的时候,没调用@PreDestory的方法,请问为什么?
context.destroy();
context.close();我是这样销毁的
没太明白你问什么,你是问@PreDestory在scope是singleton的时候没有执行吧?我推测是因为singleton并没有被销毁,非WEB 关闭CONTAINER请用
ctx.registerShutdownHook();