spring之InitializingBean接口的使用
//记录几个spring的接口以及使用 //从名字可以看出来这个接口主要用于初始化bean用。在bean被初始化后就会执行afterPropertiesSet方法。 public interface InitializingBean { void afterPropertiesSet() throws Exception; } //其实现原理如下该方法在AbstractAutowireCapableBeanFactory中 protected void invokeInitMethods(String beanName, final Object bean, RootBeanDefinition mbd) throws Throwable { //判断是否实现了InitializingBean接口 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>() { @Override 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(); if (initMethodName != null && !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) && !mbd.isExternallyManagedInitMethod(initMethodName)) { invokeCustomInitMethod(beanName, bean, mbd); } } } /** 所以我们在写框架的时候如果要进行一些初始化工作就可以实现InitializingBean接口。 */
相关推荐
横云断岭 2020-06-11
横云断岭 2020-05-26
咻pur慢 2020-04-29
白净垃圾桶 2020-05-28
杜鲁门 2020-11-05
与卿画眉共浮生 2020-10-14
lukezhong 2020-10-14
tangxiong0 2020-09-03
YangHuiLiang 2020-08-06
Sweetdream 2020-08-03
编程点滴 2020-07-29
smalllove 2020-07-27
iconhot 2020-07-05
XGQ 2020-07-04
MicroBoy 2020-07-04
itjavashuai 2020-07-04
zmysna 2020-07-04
willluckysmile 2020-06-29
CoderBoy 2020-06-28