本文转自:https://www.cnblogs.com/yql1986/p/4084888.html
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 args 14 */ 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 @Override 17 public void afterPropertiesSet() throws Exception { 18 System.out.println(">>>>>>> 执行了 afterPropertiesSet 方法"); 19 20 } 21 22 }
执行的结果如下图所示。可以看到当执行完User默认的构造函数之后,就会调用该类实现afterPropertiesSet方法