A, InitializingBean Interface
public interface InitializingBean {
void afterPropertiesSet() throws Exception;
}
public interface DisposableBean {
void destroy() throws Exception;
}
Second, an overview
Not difficult to find the name of the interface, InitializingBean role is to perform customized operations after bean initialization.
Spring container Bean is a life cycle, allowing Spring Bean initialization is completed before a specific action Bean destruction methods are commonly used in the set, and the following three:
- After initialization by implementing customized InitializingBean / DisposableBean Interface / destruction before the operation of the method;
- After init-method by <bean> element / destroy-method property specifies the initialization / destruction before the operation of the method call;
- Plus @PostConstruct or @PreDestroy comment on the specified method to develop this method is called before or destroyed after initialization.
三、InitializingBean vs init-method
- bean spring provides two ways to initialize bean achieve InitializingBean interface, afterPropertiesSet method, or by the init-method specified in the configuration file, two methods can be used simultaneously
- InitializingBean implement direct call interface is a afterPropertiesSet method, reflection efficiency than by the method calls init-method specified to be relatively high. But the init-method approach eliminates the dependency on spring
- AfterPropertiesSet first call, then perform the init-method approach was an error if you call afterPropertiesSet method, the method specified by init-method is not called.
Four, @ PostConstruct
And debug class found by the call stack InitDestroyAnnotationBeanPostProcessor
, wherein the core of the method, i.e. @PostConstruct
the method call entry:
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
LifecycleMetadata metadata = findLifecycleMetadata(bean.getClass());
try {
metadata.invokeInitMethods(bean, beanName);
}
catch (InvocationTargetException ex) {
throw new BeanCreationException(beanName, "Invocation of init method failed", ex.getTargetException());
}
catch (Throwable ex) {
throw new BeanCreationException(beanName, "Failed to invoke init method", ex);
}
return bean;
}
From the name, we can get some information - this is a BeanPostProcessor. Think of what? In Talk Spring container's life cycle , the mentioned BeanPostProcessor of postProcessBeforeInitialization Bean is in the life cycle to be called before afterPropertiesSet and init-method. Also by tracking, @PostConstruct
call also through the ways and means of emission mechanism.
to sum up
- spring bean initialization execution order of: constructor ->
@PostConstruct
Method annotation ->afterPropertiesSet
Method ->init-method
specified method. Specific examples can refer to afterPropertiesSet
By way of calling interface (a little higher efficiency),@PostConstruct
andinit-method
it is reflected by calling mechanism
init-method example
// 通过<bean>的init-method属性指定的初始化方法
public void myInit() {
System.out.println("【init-method】调用<bean>的init-method属性指定的初始化方法");
}
// 通过<bean>的destroy-method属性指定的初始化方法
public void myDestory() {
System.out.println("【destroy-method】调用<bean>的destroy-method属性指定的初始化方法");
}
<bean id="person" class="com.ooo.life.Person" init-method="myInit"
destroy-method="myDestory" scope="singleton" p:name="张三" p:address="广州"
p:phone="159000" />