spring的InitializingBean的 afterPropertiesSet 方法 和 init-method配置的 区别联系
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"); } }
SHAPE /* MERGEFORMAT
装配bean的合作者
|
查看bean是否实现InitializingBean接口
|
调用afterPropertiesSet方法
|
package research.spring.beanfactory.ch4;publicclass LifeCycleBean{publicvoid init(){System.out.println("LifeCycleBean.init...");} }
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>
//……//在一个bean的合作者设备完成后,执行一个bean的初始化方法。protectedvoid 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-methodif(mergedBeanDefinition!=null&&mergedBeanDefinition.getInitMethodName() !=null) {//调用invokeCustomInitMethod方法来执行init-method定义的方法invokeCustomInitMethod(beanName, bean, mergedBeanDefinition.getInitMethodName());} }//执行一个bean定义的init-method方法protectedvoid 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) {thrownew NoSuchMethodException("Couldn‘t find an init method named ‘"+ initMethodName +"‘ on bean with name ‘"+ beanName +"‘");}//判断方法是否是publicif (!Modifier.isPublic(initMethod.getModifiers())) {//设置accessible为true,可以访问private方法。 initMethod.setAccessible(true);}try {//反射执行这个方法initMethod.invoke(bean, (Object[]) null);}catch (InvocationTargetException ex) {throw ex.getTargetException();} }//………..
spring的InitializingBean的 afterPropertiesSet 方法 和 init-method配置的 区别联系,古老的榕树,5-wow.com
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。