http://blog.csdn.net/super_ccc/article/details/50728529

1.xml文件

  1. <bean id="aaa" class="com.dingwang.Test.Aaa" init-method="init">
  2. <constructor-arg name="name" value="ddd"></constructor-arg>
  3. </bean>

2.java文件

  1. public Aaa(String name) {
  2. LOGGER.warn("--------------------Aaa-----------------Aaa");
  3. this.setName(name);
  4. }
  5. public void init() {
  6. LOGGER.warn("--------------------Aaa-----------------init");
  7. }
  8. /*
  9. * (non-Javadoc)
  10. * @see
  11. * org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
  12. */
  13. @Override
  14. public void afterPropertiesSet() throws Exception {
  15. LOGGER.warn("--------------------Aaa-----------------afterPropertiesSet");
  16. }

3.执行日志

  1. 10:44:54.116 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'aaa'
  2. 10:44:54.157 [main] WARN  com.dingwang.Test.Aaa - --------------------Aaa-----------------Aaa
  3. 10:44:54.159 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'aaa' to allow for resolving potential circular references
  4. 10:44:54.171 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'aaa'
  5. 10:44:54.172 [main] WARN  com.dingwang.Test.Aaa - --------------------Aaa-----------------afterPropertiesSet
  6. 10:44:54.172 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Invoking init method  'init' on bean with name 'aaa'
  7. 10:44:54.172 [main] WARN  com.dingwang.Test.Aaa - --------------------Aaa-----------------init
  8. 10:44:54.173 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'aaa'

4.结论

执行顺序:构造函数>afterPropertiesSet>init-method

Person类

  1. public class Person {
  2. private int i = 0;
  3. public Person(){
  4. System.out.println("实例化一个对象");
  5. }
  6. public void init(){
  7. System.out.println("调用初始化方法....");
  8. }
  9. public void destory222(){
  10. System.out.println("调用销毁化方法....");
  11. }
  12. }

applicationContext.xml

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

测试类:

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

如果用注解方式配置:

applicationContext.xml

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

Person类

    1. public class Person {
    2. private int i = 0;
    3. public Person(){
    4. System.out.println("实例化一个对象");
    5. }
    6. @PostConstruct //初始化方法的注解方式  等同与init-method=init
    7. public void init(){
    8. System.out.println("调用初始化方法....");
    9. }
    10. @PreDestroy //销毁方法的注解方式  等同于destory-method=destory222
    11. public void destory(){
    12. System.out.println("调用销毁化方法....");
    13. }
    14. }

最新文章

  1. Thrift-0.9.2编译安装
  2. Qt Creator pro 文件 导入vs2013碰到的问题
  3. 关于如何在cenos7.0上实现mysql数据库远程连接
  4. iOS 关于UIWindow的理解
  5. 改变select组件的option选中状态的快捷方法
  6. xtrabackup之Innobackupex增量备份及恢复
  7. 开发框架XUtils
  8. Php模板引擎Smarty安装和配置
  9. 第2阶段——编写uboot之启动内核和制作Makefile(2)
  10. ThinkPHP配置文件的加载
  11. [一个脑洞] Candy?&#39;s 不饱和度
  12. github远程仓库初始化配置
  13. 线上Django项目python2到3升级日记
  14. Vue.js组件间通信方式总结
  15. echarts简单的折线图
  16. python 进程锁 生产者消费者模型 队列 (进程其他方法,守护进程,数据共享,进程隔离验证)
  17. django之admin管理工具
  18. 初级PM要做什么
  19. android开发(48) Android Snackbar 的使用
  20. 【索引失效】什么情况下会引起MySQL索引失效

热门文章

  1. CSS学习(二)
  2. Windows Server: 将虚拟机迁移到 Azure (以阿里云为例)
  3. final 、finalize和finally的区别
  4. Git错误解决(windows版本下的Git Shell)
  5. setInterval 与 clearInterval详解
  6. MXNet 分布式环境部署
  7. ES6学习笔记(一)
  8. Android错题集
  9. Android学习——ListView的缓存机制
  10. 浅谈App原生开发、混合开发及HTML5开发的优劣