InitializingBean接口爲bean提供了初始化方法的方式,它只有afterPropertiesSet方法,凡是繼承該接口的類,在初始化bean的時候會執行該方法。java
測試程序以下:spring
import org.springframework.beans.factory.InitializingBean; public class TestInitializingBean implements InitializingBean{ @Override public void afterPropertiesSet() throws Exception { System.out.println("TestInitializingBean "); } }
配置文件以下:ide
<bean id="testInitializingBean" class="com.TestInitializingBean" ></bean>
Main主程序以下:測試
public class Main { public static void main(String[] args){ ApplicationContext context = new FileSystemXmlApplicationContext("/src/main/java/com/beans.xml"); } }
打印以下結果:spa
TestInitializingBean
這說明在spring初始化bean的時候,若是bean實現了InitializingBean接口,會自動調用afterPropertiesSet方法。code