Person类:

public class Person {  
    private int i = 0;  
  
    public Person(){  
        System.out.println("实例化一个对象");  
    }  
      
    public void init(){  
        System.out.println("调用初始化方法....");  
    }  
      
    public void destory222(){  
        System.out.println("调用销毁化方法....");  
    }  
}  

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        xsi:schemaLocation="http://www.springframework.org/schema/beans   
                            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
    <!-- 配置初始化方法和销毁方法,但是如果要销毁方法生效scope="singleton" -->                  
    <bean id="person" class="com.xxc.initAndDestory.domain.Person" scope="singleton" lazy-init="false" init-method="init" destroy-method="destory"></bean>  
</beans>  

测试类:

public class Test {  
    public static void main(String[] args) {  
        //如果要调用销毁方法必须用子类来声明,而不是ApplicationContext,因为ApplicationContext没有close()  
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("com/xxc/initAndDestory/applicationContext.xml");  
        Person p1 = (Person)ac.getBean("person");  
        Person p2 = (Person)ac.getBean("person");  
        ac.close();  
    }  
}  

 如果用注解方式配置: 

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
       xmlns:context="http://www.springframework.org/schema/context"   
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       xsi:schemaLocation="http://www.springframework.org/schema/beans     
                            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd    
                            http://www.springframework.org/schema/context     
                            http://www.springframework.org/schema/context/spring-context-2.5.xsd">    
                              
    <context:annotation-config/>   
                              
    <!-- scope默认是 prototype:getBean()一次创建一个实例-->  
    <bean id="person" class="com.xxc.initAndDestory.domain.Person"></bean>  
</beans>  

Person类:

public class Person {  
    private int i = 0;  
  
    public Person(){  
        System.out.println("实例化一个对象");  
    }  
    @PostConstruct //初始化方法的注解方式  等同与init-method=init  
    public void init(){  
        System.out.println("调用初始化方法....");  
    }  
    @PreDestroy //销毁方法的注解方式  等同于destory-method=destory222  
    public void destory(){  
        System.out.println("调用销毁化方法....");  
    }  
}  

Spring配置文件中bean标签中init-method和destroy-method和用注解方式配置的更多相关文章

  1. 【Spring源码解读】bean标签中的属性(二)你可能还不够了解的 abstract 属性和 parent 属性

    abstract 属性说明 abstract 在java的语义里是代表抽象的意思,用来说明被修饰的类是抽象类.在Spring中bean标签里的 abstract 的含义其实也差不多,表示当前bean是 ...

  2. 【Spring源码解读】bean标签中的属性

    说明 今天在阅读Spring源码的时候,发现在加载xml中的bean时,解析了很多标签,其中有常用的如:scope.autowire.lazy-init.init-method.destroy-met ...

  3. Spring中bean标签的属性和值:

    Spring中bean标签的属性和值: <bean name="user" class="com.pojo.User" init-method=" ...

  4. java代码中init method和destroy method的三种使用方式

    在java的实际开发过程中,我们可能常常需要使用到init method和destroy method,比如初始化一个对象(bean)后立即初始化(加载)一些数据,在销毁一个对象之前进行垃圾回收等等. ...

  5. 关于django中input标签中file类型以及开路由

    0825自我总结 关于django中input标签中file类型 1.input提交图片实时展示 <img src="/static/img/default.png" wid ...

  6. HTML中Meta标签中http-equiv属性小结

    HTML中Meta标签中http-equiv的用法: <meta http-equiv="这里是参数" content="这里是参数值"> 1.Ex ...

  7. HTML中Meta标签中http-equiv属性

    HTML中Meta标签中http-equiv的用法: <meta http-equiv="这里是参数" content="这里是参数值"> 1.Ex ...

  8. spring(二、bean生命周期、用到的设计模式、常用注解)

    spring(二.bean生命周期.用到的设计模式.常用注解) Spring作为当前Java最流行.最强大的轻量级框架,受到了程序员的热烈欢迎.准确的了解Spring Bean的生命周期是非常必要的. ...

  9. 跟着刚哥学习Spring框架--通过注解方式配置Bean(四)

    组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Resposit ...

随机推荐

  1. SpringBoot多数据库连接(mysql+oracle)

    出于业务需求,有时我们需要在spring boot web应用程序中配置多个数据源并连接到多个数据库. 使用过Spring Boot框架的小伙伴们,想必都发现了Spring Boot对JPA提供了非常 ...

  2. B站学习的回顾总结

    视频地址 https://www.bilibili.com/video/av50680998/ 1.MVC 和MVVM有什么区别? MVC 是后端开发的概念: Model   view  contro ...

  3. 第37课 智能指针分析(指针特征操作符( -> 、 *)重载)

    1. 永恒的话题:内存泄漏 (1)动态申请堆空间,用完后不归还 (2)C++语言中没有垃圾回收的机制 (3)指针无法控制所指堆空间的生命周期------------指针是变量,可以指向内存堆空间,但是 ...

  4. PHP 表单和用户输入讲解

    PHP 表单和用户输入 PHP 中的 $_GET 和 $_POST 变量用于检索表单中的信息,比如用户输入. PHP 表单处理 有一点很重要的事情值得注意,当处理 HTML 表单时,PHP 能把来自 ...

  5. Python File read() 方法

    概述 read() 方法用于从文件读取指定的字节数,如果未给定或为负则读取所有.高佣联盟 www.cgewang.com 语法 read() 方法语法如下: fileObject.read(); 参数 ...

  6. 打印java系统的信息

    System.getProperties() 确定当前系统属性 Properties.list() 将获取到的信息打印到流中 public class Userone { public static ...

  7. CAS(乐观锁)以及ABA问题

    https://blog.csdn.net/wwd0501/article/details/88663621独占锁是一种悲观锁,synchronized就是一种独占锁:它假设最坏的情况,并且只有在确保 ...

  8. property补充

    property补充 # class Foo: # @property # def AAA(self): # print('get的时候运行我啊') # # @AAA.setter # def AAA ...

  9. OpenCV计算机视觉编程攻略(第三版)源码

    去年买了这本OpenCV的书,感觉还不错,但是书上没有给出下载源码的地方,在网上找了下,还好找到了,现在分享给大家: 链接: https://pan.baidu.com/s/1IqAay1IZ8g-h ...

  10. Java代码编写规范(转载)

    编码规范 1 前言为确保系统源程序可读性,从而增强系统可维护性,java编程人员应具有基本类似的编程风格,兹制定下述Java编程规范,以规范系统Java部分编程.系统继承的其它资源中的源程序也应按此规 ...