我正在使用
applicationContext.getAutowireCapableBeanFactory().initializeBean(bean, name);
在运行时注册bean。实际上,这个bean可以被认为是现有单例的子bean。
如果bean实现ApplicationListener,我会在日志中收到警告,因为没有底层bean定义来指示该bean应该被视为单例。当然,它没有收到应用程序事件,就像警告说的那样。
Inner bean 'name' 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.
如何在运行时将bean注册为singleton,因此我避免了bean post processor警告,以及应用程序事件是否有效?
(参见AbstractApplicationContext:1413)