Spring中的初始化InitializingBean接口和DisposableBean接口

     最近在看关于redis整合的代码的时候,配置jedis的扩展操作RedisTemplate类,其中有属性设置jedis连接,忍不住好奇,看了看RedisTemplate的实现。发现RedisTemplate类继承了RedisAccessor,而RedisAccessor提供了redis库的连接方法,还实现了InitializingBean。InitializingBean有什么用?为什么要实现这个接口?

  度娘了一下,原来InitializingBean提供了afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候会执行该方法。也就是说只有类继承InitializingBean,我们就可以在afterPropertiesSet方法中添加一下自己的操作,一旦bean初始化,就会自动执行afterPropertiesSet方法。比如,判断redis连接是不是能取到。

 实现InitializingBean接口与在配置文件中指定init-method有什么不同?

     查看spring的加载bean的源码类(AbstractAutowireCapableBeanFactory)可看出,AbstractAutowireCapableBeanFactory类中的invokeInitMethods实现

 

protected void invokeInitMethods(String beanName, final Object bean, RootBeanDefinition mbd) throws Throwable {
<span style="white-space:pre">	</span>//判断该bean是否实现了实现了InitializingBean接口,如果实现了InitializingBean接口,调用bean的afterPropertiesSet方法
        boolean isInitializingBean = (bean instanceof InitializingBean);
        if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
            if (logger.isDebugEnabled()) {
                logger.debug("Invoking afterPropertiesSet() on bean with name '" + beanName + "'");
            }
            
            if (System.getSecurityManager() != null) {
                try {
                    AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
                        public Object run() throws Exception {
                            //直接调用afterPropertiesSet
                            ((InitializingBean) bean).afterPropertiesSet();
                            return null;
                        }
                    },getAccessControlContext());
                } catch (PrivilegedActionException pae) {
                    throw pae.getException();
                }
            }                
            else {
                //直接调用afterPropertiesSet
                ((InitializingBean) bean).afterPropertiesSet();
            }
        }
        if (mbd != null) {
            String initMethodName = mbd.getInitMethodName();
            //判断是否指定了init-method方法,如果指定了init-method方法,则再调用制定的init-method
            if (initMethodName != null && !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
                    !mbd.isExternallyManagedInitMethod(initMethodName)) {
                    //进一步查看该方法的源码,可以发现init-method方法中指定的方法是通过反射实现
                invokeCustomInitMethod(beanName, bean, mbd);
            }
        }
    }

 

总结:

1、spring提供了两种初始化bean的方式,实现InitializingBean接口中的afterPropertiesSet方法,或者在配置文件中设定init-method。
2、实现InitializingBean接口是直接调用afterPropertiesSet方法,而init-method是通过反射来实现,效率较低,但是init-method方式消除了对spring的依赖。
3、InitializingBean接口实现先于init-method方法,如果调用afterPropertiesSet方法时出错,则不调用init-method指定的方法。

同样,对于DisposableBean接口,它也只提供一个方法destroy()。如果实现了DisposebleBean接口,那么Spring将自动调用bean中的Destory方法进行销毁,与在配置文件中配置destory-method的情况与上面相同。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值