InitializingBean
package org.springframework.beans.factory; /** * Interface to be implemented by beans that need to react once all their * properties have been set by a BeanFactory: for example, to perform custom * initialization, or merely to check that all mandatory properties have been set. * * <p>An alternative to implementing InitializingBean is specifying a custom * init-method, for example in an XML bean definition. * For a list of all bean lifecycle methods, see the BeanFactory javadocs. * * @author Rod Johnson * @see BeanNameAware * @see BeanFactoryAware * @see BeanFactory * @see org.springframework.beans.factory.support.RootBeanDefinition#getInitMethodName * @see org.springframework.context.ApplicationContextAware */ public interface InitializingBean { /** * Invoked by a BeanFactory after it has set all bean properties supplied * (and satisfied BeanFactoryAware and ApplicationContextAware). * <p>This method allows the bean instance to perform initialization only * possible when all bean properties have been set and to throw an * exception in the event of misconfiguration. * @throws Exception in the event of misconfiguration (such * as failure to set an essential property) or if initialization fails. */ void afterPropertiesSet() throws Exception; }
package research.spring.beanfactory.ch4; import org.springframework.beans.factory.InitializingBean; public class LifeCycleBean implements InitializingBean{ public void afterPropertiesSet() throws Exception { System.out.println("LifeCycleBean initializing..."); } }
<xml version="1.0" encoding="UTF-8"?> DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean name="lifeBean" class="research.spring.beanfactory.ch4.LifeCycleBean"> </bean> </beans>
package research.spring.beanfactory.ch4; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; public class LifeCycleTest { public static void main(String[] args) { XmlBeanFactory factory=new XmlBeanFactory(new ClassPathResource("research/spring/beanfactory/ch4/context.xml")); factory.getBean("lifeBean"); } }运行上面的程序我们会看到:“LifeCycleBean initializing...”,这说明bean的afterPropertiesSet已经被Spring调用了。
init-method
package research.spring.beanfactory.ch4; public class LifeCycleBean{ public void init(){ System.out.println("LifeCycleBean.init..."); } }
在Spring中配置这个bean:
xml version="1.0" encoding="UTF-8"?> DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean name="lifeBean" class="research.spring.beanfactory.ch4.LifeCycleBean" init-method="init"> </bean> </beans>
final protected void init() throws Exception{
System.out.println("init method...");
if(true) throw new Exception("init exception");
//…… //在一个bean的合作者设备完成后,执行一个bean的初始化方法。 protected void invokeInitMethods(String beanName, Object bean, RootBeanDefinition mergedBeanDefinition) throws Throwable { //判断bean是否实现了InitializingBean接口 if (bean instanceof InitializingBean) { if (logger.isDebugEnabled()) { logger.debug("Invoking afterPropertiesSet() on bean with name '" + beanName + "'"); } //调用afterPropertiesSet方法 ((InitializingBean) bean).afterPropertiesSet(); } //判断bean是否定义了init-method if(mergedBeanDefinition!=null&&mergedBeanDefinition.getInitMethodName() != null) { //调用invokeCustomInitMethod方法来执行init-method定义的方法 invokeCustomInitMethod(beanName, bean, mergedBeanDefinition.getInitMethodName()); } } //执行一个bean定义的init-method方法 protected void invokeCustomInitMethod(String beanName, Object bean, String initMethodName) throws Throwable { if (logger.isDebugEnabled()) { logger.debug("Invoking custom init method '" + initMethodName + "' on bean with name '" + beanName + "'"); } //使用方法名,反射Method对象 Method initMethod = BeanUtils.findMethod(bean.getClass(), initMethodName, null); if (initMethod == null) { throw new NoSuchMethodException("Couldn't find an init method named '" + initMethodName + "' on bean with name '" + beanName + "'"); } //判断方法是否是public if (!Modifier.isPublic(initMethod.getModifiers())) { //设置accessible为true,可以访问private方法。 initMethod.setAccessible(true); } try { //反射执行这个方法 initMethod.invoke(bean, (Object[]) null); } catch (InvocationTargetException ex) { throw ex.getTargetException(); } } //………..
装配 bean 的合作者 |
查看 bean 是否实现 InitializingBean 接口 |
调用 afterPropertiesSet 方法 |