org.springframework.beans.factory包下有一个接口是InitializingBean 只有一个方法:

/**
  * Invoked by a BeanFactory after it has set all bean properties supplied
  * (and satisfied BeanFactoryAware and ApplicationContextAware).
  * <p>This method allows the bean instance to perform initialization only
  * possible when all bean properties have been set and to throw an
  * exception in the event of misconfiguration.
  * @throws Exception in the event of misconfiguration (such
  * as failure to set an essential property) or if initialization fails.
  */
 void afterPropertiesSet() throws Exception;

这个方法将在所有的属性被初始化后调用。

但是会在init前调用。

但是主要的是如果是延迟加载的话,则马上执行。

所以可以在类上加上注解:

import org.springframework.context.annotation.Lazy;

@Lazy(false)

这样spring容器初始化的时候afterPropertiesSet就会被调用。

1:spring为bean提供了两种初始化bean的方式,实现InitializingBean接口,实现afterPropertiesSet方法,或者在配置文件中同过init-method指定,两种方式可以同时使用

2:实现InitializingBean接口是直接调用afterPropertiesSet方法,比通过反射调用init-method指定的方法效率相对来说要高点。但是init-method方式消除了对spring的依赖

由结果可看出,在spring初始化bean的时候,如果该bean是实现了InitializingBean接口,并且同时在配置文件中指定了init-method,系统则是先调用afterPropertiesSet方法,然后在调用init-method中指定的方法。

3:如果调用afterPropertiesSet方法时出错,则不调用init-method指定的方法。

InitializingBean的更多相关文章

  1. spring中InitializingBean接口使用理解

    InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候会执行该方法. 测试程序如下: imp ...

  2. spring InitializingBean接口

    最近工作需要得到sping中的每个事物需要执行的sql,称机会简单研究了一下spring的事务,项目中管理事务比较简单,用TransactionTemplate,就直接以TransactionTemp ...

  3. 用spring的InitializingBean作初始化

    org.springframework.beans.factory包下有一个接口是InitializingBean 只有一个方法: /**  * Invoked by a BeanFactory af ...

  4. spring中的DisposableBean和InitializingBean,ApplicationContextAware的用法

    在spring容器初始化bean和销毁bean的以前的操作有很多种, 目前我知道的有:在xml中定义的时候用init-method和destory-method,还有一种就是定义bean的时候实现Di ...

  5. Spring InitializingBean和init-method

    原文转自:http://blog.csdn.net/shaozheng1006/article/details/6916940 InitializingBean     Spirng的Initiali ...

  6. InitializingBean afterPropertiesSet

    package org.test.InitializingBean; import org.springframework.context.support.ClassPathXmlApplicatio ...

  7. InitializingBean和init-method

    [spring的InitializingBean的 afterPropertiesSet 方法 和 init-method配置的 区别联系] InitializingBean Spring的Initi ...

  8. Spring InitializingBean and DisposableBean example

    In Spring, InitializingBean and DisposableBean are two marker interfaces, a useful way for Spring to ...

  9. Spring中Bean的生命中期与InitializingBean和DisposableBean接口

    Spring提供了一些标志接口,用来改变BeanFactory中的bean的行为.它们包括InitializingBean和DisposableBean.实现这些接口将会导致BeanFactory调用 ...

  10. 启动就加载(三)initializingbean实现afterPropertiesSet方法

    TransactionTemplate,就直接以TransactionTemplate为入口开始学习. TransactionTemplate的源码如下: public class Transacti ...

随机推荐

  1. 行内元素的padding和margin是否无效

    html中元素分为三种:块级元素.行内元素(也叫内联元素),内联块级元素. 常用块级元素:<div>.<p>.<h1>...<h6>.<ol> ...

  2. UNIX 进程间通讯(IPC)概念(Posix,System V IPC)

     IPC(Inter-Process Communication,进程间通讯)可以有三种信息共享方式(随文件系统,随内核,随共享内存).(当然这里虽然说是进程间通讯,其实也是可以和线程相通的). 相对 ...

  3. JAVA遍历map元素

    第一种: Map map = new HashMap(); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { Ma ...

  4. 如何正确入门Windows系统下驱动开发领域?

    [作者]猪头三个人网站 :http://www.x86asm.com/ [序言]很多人都对驱动开发有兴趣,但往往找不到正确的学习方式.当然这跟驱动开发的本土化资料少有关系.大多学的驱动开发资料都以英文 ...

  5. ios之UIButoon

    第一.UIButton的定义 UIButton *button=[[UIButton buttonWithType:(UIButtonType); 能够定义的button类型有以下6种, typede ...

  6. jsDate()

    var myTime=new Date();//myTime的数据类型为(typeof) object //下面得到的都为number 类型 getFullYear();年 四位数字返回年份. get ...

  7. 一段式fsm

    //1-paragraph method to decribe FSM //Describe state transition, state output, state input condition ...

  8. perl学习 之:my local our

    范围声明 和全局声明类似,词法范围声明也是在编译时起作用的.和全局声明不同的是,词法范围声明的作用范围是从声明开始到闭合范围的最里层(块,文件,或者 eval--以先到者为准).这也是为什么我们称它为 ...

  9. LeetCode(90) Subsets II

    题目 Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...

  10. angular中几种加载css的方法

    1.Style URLs in Metadata We can load styles from external CSS files by adding a styleUrls attribute ...