InitializingBean接口只包括afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候会执行该方法
import org.springframework.beans.factory.InitializingBean;
public class TestBean implements InitializingBean{
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("this is afterPropertiesSet");
}
public void test(){
System.out.println("this is a test");
}
}
配置文件中配置:
<bean id="testBean" class="com.TestBean" ></bean>
写一个测试的main方法:
public class Main {
public static void main(String[] args){
ApplicationContext context = new FileSystemXmlApplicationContext("/src/main/java/com/beans.xml");
}
}
后台会输出:this is afterPropertiesSet
如果要执行test()方法要怎么办呢?
<bean id="testBean" class="com.TestBean" init-method="test"></bean>
在配置中加入init-method="test"指定要执行的方法,结果后台会输出:
this is afterPropertiesSet
this is a test
继承InitializingBean接口后实现afterPropertiesSet()方法运行程序afterPropertiesSet()方法是一定先执行的
to be continue