1 package org.test.InitializingBean; 2 3 import org.springframework.context.support.ClassPathxmlapplicationContext; 4 5 /** 6 * 測試 spring的 InitializingBean 接口 7 * @author Administrator 8 * 9 */10 public class InitializingBeanTest {11 12 /**13 * @param args14 */15 public static void main(String[] args) {16 17 ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml", 18 InitializingBeanTest.class);19 ctx.start();20 21 }22 23 }
1 package org.test.InitializingBean; 2 3 import org.springframework.beans.factory.InitializingBean; 4 5 /** 6 * @see {@link org.springframework.beans.factory.InitializingBean#afterPropertiesSet} 7 * @author Administrator 8 * 9 */10 public class User implements InitializingBean {11 12 public User() {13 System.out.println(">>>>>>> User 默認的構造函數執行了");14 }15 16 @Override17 public void afterPropertiesSet() throws Exception {18 System.out.println(">>>>>>> 執行了 afterPropertiesSet 方法");19 20 }21 22 }
執行的結果如下圖所示。可以看到當執行完User默認的構造函數之后,就會調用該類實現afterPropertiesSet方法
新聞熱點
疑難解答