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 方法 |
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
