spring的InitializingBean介绍
http://blog.csdn.net/hhdem/article/details/1802701
InitializingBean Spirng的InitializingBean为bean提供了定义初始化方法的方式。InitializingBean是一个接口,它仅仅包含一个方法:afterPropertiesSet()。Bean实现这个接口,在afterPropertiesSet()中编写初始化代码:
package research.spring.beanfactory.ch4; import org.springframework.beans.factory.InitializingBean; publicclass LifeCycleBean implements InitializingBean{ publicvoid afterPropertiesSet() throws Exception { System.out.println("LifeCycleBean initializing..."); } }在xml配置文件中并不需要对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">bean>beans>
package research.spring.beanfactory.ch4; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; publicclass LifeCycleTest { publicstaticvoid main(String[] args) { XmlBeanFactory factory=new XmlBeanFactory(new ClassPathResource( "research/spring/beanfactory/ch4/context.xml")); factory.getBean("lifeBean"); } }
SHAPE /* MERGEFORMAT
| 装配bean的合作者 |
| 查看bean是否实现InitializingBean接口 |
| 调用afterPropertiesSet方法 |
init-method Spring虽然可以通过InitializingBean完成一个bean初始化后对这个bean的回调,但是这种方式要求bean实现 InitializingBean接口。一但bean实现了InitializingBean接口,那么这个bean的代码就和Spring耦合到一起了。通常情况下我不鼓励bean直接实现InitializingBean,可以使用Spring提供的init-method的功能来执行一个bean 子定义的初始化方法。 写一个java class,这个类不实现任何Spring的接口。定义一个没有参数的方法init()。
package research.spring.beanfactory.ch4; publicclass LifeCycleBean{ publicvoid 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>
//…… //在一个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(); } } //………..
如果一个bean被定义为非单例的,那么afterPropertiesSet和init-method在bean的每一个实例被创建时都会执行。单例 bean的afterPropertiesSet和init-method只在bean第一次被实例时调用一次。大多数情况下 afterPropertiesSet和init-method都应用在单例的bean上。
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
