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. javase(4)_数组

    一.数组概述 数组可以看成是多个相同类型数据组合,对这些数据的统一管理. 数组变量属于引用类型,数组也可以看成对象,数组中的每个元素相当于该对象的成员变量. 数组中的元素可以是任意类型,包括基本类型和 ...

  2. javaEE(7)_自定义标签&JSTL标签(JSP Standard Tag Library)

    一.自定义标签简介 1.自定义标签主要用于移除Jsp页面中的java代码,jsp禁止出现一行java脚本. 2.使用自定义标签移除jsp页面中的java代码,只需要完成以下两个步骤: •编写一个实现T ...

  3. 安装ruby开发环境

    如何快速正确的安装 Ruby, Rails 运行环境 对于新入门的开发者,如何安装 Ruby, Ruby Gems 和 Rails 的运行环境可能会是个问题,本页主要介绍如何用一条靠谱的路子快速安装 ...

  4. 将find过滤添加到数组

    array=($(find . -name "*.txt"))for i in "${array[@]}"; do echo $i; done

  5. Controller View 模式

    参考:https://blog.andrewray.me/the-reactjs-controller-view-pattern/ Flux参考:http://www.cnblogs.com/hell ...

  6. 《嵌入式linux应用程序开发标准教程》笔记——8.进程间通信

    , 8.1 概述 linux里使用较多的进程间通信方式: 管道,pipe和fifo,管道pipe没有实体文件,只能用于具有亲缘关系的进程间通信:有名管道 named pipe,也叫fifo,还允许无亲 ...

  7. (转) 苹果所有常用证书,appID,Provisioning Profiles配置说明及制作图文教程(精)

    原文地址:http://blog.csdn.net/holydancer/article/details/9219333 概述: 苹果的证书繁锁复杂,制作管理相当麻烦,今天决定重置一个游戏项目中的所有 ...

  8. django的rest framework框架——认证、权限、节流控制

    一.登录认证示例 模拟用户登录,获取token,当用户访问订单或用户中心时,判断用户携带正确的token,则允许查看订单和用户信息,否则抛出异常: from django.conf.urls impo ...

  9. js 百度云搜索框

    // ==UserScript==// @name 百度云插件+APIKey// @namespace // @version 5.0.2.1// @description 在百度云网盘的页面添加一个 ...

  10. arrive 和reach 的区别

    例如:He arrived yesterday. 没宾语的话就用arrive了reach作抵达讲时是及物动词,后面要宾语的 分清arrive和reach的区别arrive是不及物动词,后面不能直接加地 ...