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是否實(shí)現(xiàn)InitializingBean接口 |
調(diào)用afterPropertiesSet方法 |
package research.spring.beanfactory.ch4;public class LifeCycleBean{public void 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>
final protected void init() throws Exception{
System.out.println("init method...");
if(true) throw new Exception("init exception");
//……//在一個(gè)bean的合作者設(shè)備完成后,執(zhí)行一個(gè)bean的初始化方法。protected void invokeInitMethods(String beanName, Object bean, RootBeanDefinition mergedBeanDefinition)
throws Throwable {//判斷bean是否實(shí)現(xiàn)了InitializingBean接口if (bean instanceof InitializingBean) {if (logger.isDebugEnabled()) {logger.debug("Invoking afterPropertiesSet() on bean with name ‘" + beanName + "‘");}//調(diào)用afterPropertiesSet方法((InitializingBean) bean).afterPropertiesSet();}//判斷bean是否定義了init-methodif(mergedBeanDefinition!=null&&mergedBeanDefinition.getInitMethodName() != null) {//調(diào)用invokeCustomInitMethod方法來(lái)執(zhí)行init-method定義的方法invokeCustomInitMethod(beanName, bean, mergedBeanDefinition.getInitMethodName());}}//執(zhí)行一個(gè)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對(duì)象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 + "‘");}//判斷方法是否是publicif (!Modifier.isPublic(initMethod.getModifiers())) {//設(shè)置accessible為true,可以訪問(wèn)private方法。 initMethod.setAccessible(true);}try {//反射執(zhí)行這個(gè)方法initMethod.invoke(bean, (Object[]) null);}catch (InvocationTargetException ex) {throw ex.getTargetException();}}//………..
聯(lián)系客服