spring2.0以后,有5个实例进程:singleton , prototype,request,session.gloubsession.
问题:如果使用默认实例,调用初始化方法,和摧毁方法是,使用close();方法,可以打印出初始化方法,摧毁方法的内容。但如果使用prototype实例时,不会打印出摧毁方法,why?

解决方案 »

  1.   

    AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    context.close();
    这些是代码//配置文件
    <bean id="userEntity" scope="prototype" class="com.xiaoxiao.entity.UserEntity" init-method="init" destroy-method="destory">
    </bean>//调用方法
    public class UserEntity implements Serializable {
    public void init(){
    System.out.println("初始化方法");
    }
    public void destory(){
    System.out.println("销毁方法");
    }

    }
      

  2.   

    没懂到你什么意思,你是想建一个监听器么?那你的UserEntity必须继承相关Listener
      

  3.   

    spring的说明中有这样一句话,我大致翻译了一下:“对于prototype作用域的bean,有一点非常重要,那就是Spring不能对一个 prototype bean的整个生命周期负责:容器在初始化、配置、装饰或者是装配完一个prototype实例后,将它交给客户端,随后就对该prototype实例不闻不问了。不管何种作用域,容器都会调用所有对象的初始化生命周期回调方法,而对prototype而言,任何配置好的析构生命周期回调方法都将不会被调用。清除prototype作用域的对象并释放任何prototype bean所持有的昂贵资源,都是客户端代码的职责.”
      

  4.   

    这么一说,有点感觉了,prototype只能被实例,自己不能被摧毁,要摧毁要客户端进行操作,所以即使调用摧毁方法也没有作用