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. }

spring bean中构造函数,afterPropertiesSet和init-method的执行顺序的更多相关文章

  1. Spring bean中的properties元素内的name 和 ref都代表什么意思啊?

    <bean id="userAction" class="com.neusoft.gmsbs.gms.user.action.UserAction" sc ...

  2. spring bean中的properties元素内的ref和value的区别;* 和 ** 的区别

    spring bean中的properties元素内的ref和value的区别 至于使用哪个是依据你所用的属性类型决定的. <bean id="sqlSessionFactory&qu ...

  3. spring 基于XML的申明式AspectJ通知的执行顺序

    spring 基于XML的申明式AspectJ通知的执行顺序 关于各种通知的执行顺序,结论:与配置文件中的申明顺序有关 1. XML文件配置说明 图片来源:<Java EE企业级应用开发教程&g ...

  4. Javascript中页面加载完成后优先执行顺序

    Javascript中页面加载完成后优先执行顺序 document优先于windowwindow优先于element //document加载完成执行方法体 document.addEventList ...

  5. spring bean中scope="prototype“的作用

    今天写代码时,遇到个问题,问题大概如下:在写一个新增模块,当各文本框等输入值后,提交存入数据库,跳到其它页面,当再次进入该新增页面时,上次输入的数据还存在. 经过检查发现是,spring配置文件中,配 ...

  6. 如何在静态方法或非Spring Bean中注入Spring Bean

           在项目中有时需要根据需要在自己new一个对象,或者在某些util方法或属性中获取Spring Bean对象,从而完成某些工作,但是由于自己new的对象和util方法并不是受Spring所 ...

  7. 【Spring】Spring bean中id和name的差异

    id和name都是spring 容器中中bean 的唯一标识符. id: 一个bean的唯一标识 , 命名格式必须符合XML ID属性的命名规范 name: 可以用特殊字符,并且一个bean可以用多个 ...

  8. Spring bean 实现初始化、销毁方法的方式及顺序

    Spring 允许 Bean 在初始化完成后以及销毁前执行特定的操作,常用方法有三种: 使用注解,在指定方法上加上@PostConstruct或@PreDestroy注解来制定该方法是在初始化之后还是 ...

  9. Spring filter和拦截器(Interceptor)的区别和执行顺序

    转载自:http://listenup.iteye.com/blog/1559553 1.Filter过滤器只过滤jsp文件不过滤action请求解决方案 解决办法:在web.xml中将filter的 ...

随机推荐

  1. node版本管理nvm使用

    nvm:Node Version Manager,用来管理node版本,可以在一台机器上来回切换node版本,比较方便. win下建议使用 nvm-windows nodist linux下直接使用n ...

  2. window.open()被浏览器拦截问题汇总

    一.问题描述 最近在做项目的时候碰到了使用window.open被浏览器拦截的情况,虽然在自己的环境可以对页面进行放行,但是对用户来说,不能要求用户都来通过拦截.何况当出现拦截时,很多用户根本不知道发 ...

  3. [javaSE] JDBC的批处理

    向数据库发送多条sql语句 create database batch use batch create table batch_table( id int primary key auto_incr ...

  4. redis(1)简介

    一.nosql简介 RDBMS(关系型数据库)提供的结构化编程,让数据建模以及应用程序编程变得非常简单,带来了非常高的经济效益,并且学习成本也比较低.但在当今数据大爆炸时代,每时每刻都会海量的数据产生 ...

  5. Java基础(9)——数组

    难点儿的已经过去啦,现在又开始基础了哈~ 之前讲变量的时候,变量是一个个的呀~,那我要搞一串变量该啷个办呢?Java给我们出了个好东西叫数组(*^▽^*) 数组呢,就是将变量一组一组的存起来,这个也是 ...

  6. 虚拟机下centos时间不正确的方便解决方法

    就是用NTP了,通过外部的服务同步时间. ntpdate us.pool.ntp.org | logger -t NTP 如果没有ntpdate ,可以使用 yum install ntpdate 进 ...

  7. hdu 4628 Pieces 状态压缩dp

    Pieces Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total S ...

  8. JS Event 鼠标拖拽事件

    <!DOCTYPE html><html> <head>        <meta charset="UTF-8">         ...

  9. eayui grid 每一页的行号都是从1开始

    问题背景: easyui 需要显示行号的时候,我们只需要设置  rownumbers: true, 但是 不管是在哪一页,行号都是从1开始,不能连续 我们在分页的 onSelectPage 函数里去执 ...

  10. Python基础-小程序练习(跳出多层循环,购物车,多级菜单,用户登录)

    一. 从第3层循环直接跳出所有循环 break_flag = False count = 0 while break_flag == False: print("-第一层") wh ...