一、首先我们看一下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