ApplicationListenerDetector监听器判断demo,
分享于 点击 26691 次 点评:244
ApplicationListenerDetector监听器判断demo,
目录
- Bean实例化之后
- Bean销毁之前
Bean实例化之后
判断Bean是否是监听器,如果是监听器就将当前Bean加入监听器集合
public Object postProcessAfterInitialization(Object bean, String beanName) { if (bean instanceof ApplicationListener) { // potentially not detected as a listener by getBeanNamesForType retrieval Boolean flag = this.singletonNames.get(beanName); if (Boolean.TRUE.equals(flag)) { // singleton bean (top-level or inner): register on the fly this.applicationContext.addApplicationListener((ApplicationListener<?>) bean); } else if (Boolean.FALSE.equals(flag)) { if (logger.isWarnEnabled() && !this.applicationContext.containsBean(beanName)) { // inner bean with other scope - can't reliably process events logger.warn("Inner bean '" + beanName + "' implements ApplicationListener interface " + "but is not reachable for event multicasting by its containing ApplicationContext " + "because it does not have singleton scope. Only top-level listener beans are allowed " + "to be of non-singleton scope."); } this.singletonNames.remove(beanName); } } return bean; }
Bean销毁之前
如果当前Bean是监听器,就将当前Bean从监听器集合中移除
public void postProcessBeforeDestruction(Object bean, String beanName) { if (bean instanceof ApplicationListener) { try { ApplicationEventMulticaster multicaster = this.applicationContext.getApplicationEventMulticaster(); multicaster.removeApplicationListener((ApplicationListener<?>) bean); multicaster.removeApplicationListenerBean(beanName); } catch (IllegalStateException ex) { // ApplicationEventMulticaster not initialized yet - no need to remove a listener } } }
以上就是ApplicationListenerDetector监听器判断demo的详细内容,更多关于ApplicationListenerDetector监听器的资料请关注3672js教程其它相关文章!
您可能感兴趣的文章:- Spring事件监听机制观察者模式详解
- SpringBoot利用切面注解及反射实现事件监听功能
- Springboot整合zookeeper实现对节点的创建、监听与判断的案例详解
- 详解SpringBoot实现事件同步与异步监听
- SpringBoot中使用Redis Stream实现消息监听示例
用户点评