0

我在用

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 注册为单例,从而避免 bean 后处理器警告,并让应用程序事件正常工作?

(参见 AbstractApplicationContext:1413)

4

0 回答 0