您的位置:首页 > 编程语言 > Java开发

Spring Bean扩展接口 -- InitializingBean源码分析

2017-01-09 18:34 435 查看
一、首先我们看一下InitializingBean接口的源码

package org.springframework.beans.factory;

public interface InitializingBean {

void afterPropertiesSet() throws Exception;

}

InitializingBean接口只要一个方法afterPropertiesSet,下面实例我们对afterPropertiesSet进行解释。

二、实例

@Component
public class OrderTest implements InitializingBean {

String name ;

public String getName() {
return name;
}
// 当容器启动后,先执行此处
public void setName(String name) {
System.out.println("this name is InitializingBean");
this.name = name;
}
// 当所有的属性都设置完成后,才会执行afterPropertiesSet方法
public void afterPropertiesSet() throws Exception {
System.out.println("exe afterPropertiesSet");
}

}

当容器启动后结果为:
this
name is InitializingBean

exe
afterPropertiesSet
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息