1.xml文件
<bean id="aaa" class="com.dingwang.Test.Aaa" init-method="init">
<constructor-arg name="name" value="ddd"></constructor-arg>
</bean>2.java文件
public Aaa(String name) {
LOGGER.warn("--------------------Aaa-----------------Aaa");
this.setName(name);
}
public void init() {
LOGGER.warn("--------------------Aaa-----------------init");
}
/*
* (non-Javadoc)
* @see
* org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
@Override
public void afterPropertiesSet() throws Exception {
LOGGER.warn("--------------------Aaa-----------------afterPropertiesSet");
}3.执行日志
10:44:54.116 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'aaa'
10:44:54.157 [main] WARN com.dingwang.Test.Aaa - --------------------Aaa-----------------Aaa
10:44:54.159 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'aaa' to allow for resolving potential circular references
10:44:54.171 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'aaa'
10:44:54.172 [main] WARN com.dingwang.Test.Aaa - --------------------Aaa-----------------afterPropertiesSet
10:44:54.172 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Invoking init method 'init' on bean with name 'aaa'
10:44:54.172 [main] WARN com.dingwang.Test.Aaa - --------------------Aaa-----------------init
10:44:54.173 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'aaa'4.结论
执行顺序:构造函数>afterPropertiesSet>init-method
本文详细解析了Spring框架中Bean的创建过程及生命周期方法调用顺序,重点介绍了构造函数、afterPropertiesSet方法及自定义初始化方法的执行时机。
903

被折叠的 条评论
为什么被折叠?



