当前位置: 首页 > 面试题库 >

何时调用SpringBeans销毁方法?

周弘毅
2023-03-14
问题内容

我已经在bean的“销毁方法”中放入了sysout语句。当我运行示例代码时,sysout没有得到输出。这是否意味着销毁方法未得到调用?

测试类别:

  package spring.test;

  import org.springframework.context.ApplicationContext;
  import org.springframework.context.support.ClassPathXmlApplicationContext;

  public class InitTest {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("InitTestContext.xml");
        InitTestBean bean = (InitTestBean)ctx.getBean("InitTestBean");
        bean.display();
    }
  }

The Bean

  package spring.test;

  public class InitTestBean {
    private String prop1;
    private String prop2;

    public InitTestBean(String prop1, String prop2) {
        System.out.println("Instantiating InitTestBean");
        this.prop1 = prop1;
        this.prop2 = prop2;
    }

    public void setProp1(String prop1) {
        System.out.println("In setProp1");
        this.prop1 = prop1;
    }

    public void setProp2(String prop2) {
        System.out.println("In setProp2");
        this.prop2 = prop2;
    }

    public String getProp1() {
        return prop1;
    }

    public String getProp2() {
        return prop2;
    }

    public void display() {
        System.out.println("Prop1 is " + prop1);
        System.out.println("Prop2 is " + prop2);
    }

    public void initialize(){
        System.out.println("In initialize");
        this.prop1 = "init-prop1";
        this.prop2 = "init-prop2";
    }

    public void teardown() {
        System.out.println("In teardown");
        this.prop1 = null;
        this.prop2 = null;
    }
  }

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="InitTestBean" class="spring.test.InitTestBean" init-method="initialize" destroy-method="teardown">
        <constructor-arg value="Prop1" />
        <constructor-arg value="Prop2" />
        <property name="prop1" value="setProp1"/>
        <property name="prop2" value="setProp2"/>
    </bean>

</beans>

问题答案:

你的示例不起作用,因为你没有关闭appcontext,只是让程序终止。

调用close()上下文,你将看到被调用的bean destroy-method。



 类似资料:
  • 我是一个蟒蛇初学者。试着做一个新的按钮来关闭窗口。我得到了错误消息: 异常在Tkinter回调Traceback(最近调用最后):文件/系统/库/框架/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py,第1536行,在调用返回self.func(*args)文件tk_cp_successful.py,第138行,在按钮推送s

  • 问题内容: 我在hapijs中使用jwt插件和策略。 我可以在登录用户时创建jwt令牌,并通过’jwt’策略使用同一令牌对其他API进行身份验证。 我将令牌设置为cookie,其中是令牌名称。另外,我没有将这些令牌保存在数据库中。 但是,注销时如何销毁jwt令牌? 请提出一种方法。 问题答案: JWT存储在浏览器中,因此删除令牌以删除客户端的cookie 如果您还需要在令牌到期之前从服务器端使令牌

  • 销毁 Destroy 在不需要使用iScoll的时候调用iScroll实例的公共方法destroy()可以释放一些内存。 myScroll.destroy(); myScroll = null;

  • 问题内容: 据我所知(很少),给出了两种方法: 然后: 其他更好的方法?我在这里劈头发吗? 问题答案: 您正在寻找。 但是要考虑到 您不能显式销毁对象。 它会保留在那里,但是如果您取消设置对象,并且脚本将PHP推入内存限制,则不需要的对象将被垃圾回收。我会选择(而不是将其设置为null),因为它似乎具有更好的性能(未经测试,但已记录在PHP官方手册的注释中)。 也就是说,请记住,PHP总是在页面被

  • 问题内容: 我正在使用Cookies模块来设置cookie。这是我的代码: 但是在文档中,我还没有找到如何 销毁 该Cookie的方法。 任何建议,将不胜感激。 问题答案: 无法根据HTTP规范删除cookie。为了有效地“删除” cookie,您可以将过期日期设置为过去的某个日期。本质上,这将为您带来以下收益(根据Cookies模块文档): 或根据HTTP规范: 两者都应该起作用。您可以替换与一

  • 问题内容: 我需要能够动态加载/卸载角度应用程序而不会引起内存泄漏。在jQuery中,您可以执行相应的销毁代码,事件处理程序未绑定等。 我一直无法在有角度的文档中找到任何内容,提及启动应用程序后可能会拆除应用程序的可能性。 我的第一次尝试是像这样破坏rootScope: 但这似乎不起作用,而且我不确定即使清除了注入器和服务也将如何清理。 应该怎么做? 问题答案: 2013年3月10日更新: 我发现