Spring-创建完bean之后自动回调里面的方法

目录

  1.实现SmartInitializingSingleton接口,具体原因解释如下:

 2.实现ApplicationListener接口:基于监听器解决问题,具体原因解释如下:


在公司统一配置中心客户端的程序代码中,遇到了这么一个问题:将所写的客户端程序打成jar后放到客户端里面,创建完Bean之后需要自动回调里面的方法来完成检测服务端是否重新发布配置的一个长轮询基于这个问题,给出两个解决方案::

  1.实现SmartInitializingSingleton接口,具体原因解释如下:

    for (String beanName : beanNames) {
			RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
			if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
				if (isFactoryBean(beanName)) {
					Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
					if (bean instanceof FactoryBean) {
						final FactoryBean<?> factory = (FactoryBean<?>) bean;
						boolean isEagerInit;
						if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
							isEagerInit = AccessController.doPrivileged((PrivilegedAction<Boolean>)
											((SmartFactoryBean<?>) factory)::isEagerInit,
									getAccessControlContext());
						}
						else {
							isEagerInit = (factory instanceof SmartFactoryBean &&
									((SmartFactoryBean<?>) factory).isEagerInit());
						}
						if (isEagerInit) {
							getBean(beanName);
						}
					}
				}
				else {
					getBean(beanName);
				}
			}
		}

        //通过上述步骤,已经把beanDefinitionMap中符合要求的bean定义创建完成
        //遍历所有的beanName
		for (String beanName : beanNames) {
            //所有的bean已经创建完成,会从一级缓存中获取对应的对象
			Object singletonInstance = getSingleton(beanName);
            //判断是否实现了SmartInitializingSingleton接口
			if (singletonInstance instanceof SmartInitializingSingleton) {
                //如果实现了这个接口,将实现类转为smartSingleton 
				final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
				if (System.getSecurityManager() != null) {
					AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
						smartSingleton.afterSingletonsInstantiated();
						return null;
					}, getAccessControlContext());
				}
				else {
                    //调用实现类里面的这个方法
					smartSingleton.afterSingletonsInstantiated();
				}
			}
		}

具体的实现代码:

 2.实现ApplicationListener接口:基于监听器解决问题,具体原因解释如下:

protected void publishEvent(Object event, @Nullable ResolvableType eventType) {
       /***
         **
          */
        else {

//ApplicationEventMulticaster这个类是我们所说的广播器,在spring事件中起到了非常关键的作用,
//发布者发布的事件会发送给EventMultiCaster, 而后由EventMultiCaster注册着所有的Listener,
//然后根据事件类型决定转发给那个Listener,接着唤醒Listener,执行里面的onApplicationEvent方法

            getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType);
        }

       /***
         **
          */

}



private void doInvokeListener(ApplicationListener listener, ApplicationEvent event) {
       //经过上述处理找到对应的监听器,调用onApplicationEvent方法
       try {
           listener.onApplicationEvent(event);
       }
}

具体代码实例

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值