以后面试问到Bean的生命周期再也不怕了!
看了这么久的Spring源码,想必对Spring的生命周期已经有了一定的了解,这次将之前零散的生命周期处理的事情贯穿起来,看过之后,一定对bean的生命周期有更深入的理解
实例化策略类:
InstantiationStrategy
实例化具体方法:
AbstractAutowireCapableBeanFactory.createBeanInstance(String beanName, RootBeanDefinition mbd, Object[] args)
设置Aware方法顺序:
BeanPostProcessor.postProcessBeforeInitialization
ApplicationContextAwareProcessor也会设置Aware:
调用afterpropertiesSet方法:位于AbstractAutowireCapableBeanFactory.invokeInitMethods(String beanName, final Object bean, RootBeanDefinition mbd)方法中
protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) {
// 设置Aware
if (System.getSecurityManager() != null) {
AccessController.doPrivileged(new PrivilegedAction
DefaultListableBeanFactory.preInstantiateSingletons方法,其在所有的bean都实例化完成之后调用
@Override
public void preInstantiateSingletons() throws BeansException {
if (this.logger.isDebugEnabled()) {
this.logger.debug("Pre-instantiating singletons in " + this);
}
// Iterate over a copy to allow for init methods which in turn register new bean definitions.
// While this may not be part of the regular factory bootstrap, it does otherwise work fine.
List beanNames = new ArrayList(this.beanDefinitionNames);
// Trigger initialization of all non-lazy singleton beans...
// 触发实例化所有的非懒加载的单例
for (String beanName : beanNames) {
...
}
// Trigger post-initialization callback for all applicable beans...
// 触发应用bean的post-initialization回调,也就是afterSingletonsInstantiated方法
for (String beanName : beanNames) {
Object singletonInstance = getSingleton(beanName);
if (singletonInstance instanceof SmartInitializingSingleton) {
final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
if (System.getSecurityManager() != null) {
AccessController.doPrivileged(new PrivilegedAction
判断bean是否为SmartLifecycle并且autoStartup。
位于:
DefaultLifecycleProcessor.onRefresh
AbstractAutowireCapableBeanFactory.doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args)
public class HelloWorld implements SmartInitializingSingleton,SmartLifecycle,InitializingBean,
DisposableBean,MyInterface,BeanNameAware,ApplicationContextAware
{
private final Log logger = LogFactory.getLog(getClass());
private boolean isRunning;
public HelloWorld() {
System.out.println("实例化");
}
public void sayHello(){
System.out.println("hello World");
}
public void afterSingletonsInstantiated() {
System.out.println("SmartInitializingSingleton afterSingletonsInstantiated");
}
public void start() {
isRunning = true;
System.out.println("LifeCycle start");
}
public void stop() {
System.out.println("LifeCycle stop");
}
public boolean isRunning() {
return isRunning;
}
public boolean isAutoStartup() {
return true;
}
public void stop(Runnable callback) {
System.out.println("LifeScycle stop");
callback.run();
}
public int getPhase() {
return 0;
}
public void afterPropertiesSet() throws Exception {
System.out.println("afterproperties set");
}
public void destroy() throws Exception {
System.out.println("destroy");
}
public void my(String str) {
System.out.println(str);
}
public void setBeanName(String name) {
System.out.println("set bean Name aware");
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("set Application Aware");
}
}
//MyInterface接口
public interface MyInterface {
void my(String str);
}
//app.xml
//SpringApp
public class SpringApp {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("app.xml");
HelloWorld hello = (HelloWorld) applicationContext.getBean("hello");
hello.sayHello();
applicationContext.close();
}
}
运行结果:
可对照源代码自行验证生命周期。