问题描述
我正在使用 spring-boot
和 spring-rabbitmq
软件包.我有一些配置类和代表客户端的类.该客户端类具有一个用 @RabbitListener
注释的侦听器.使用我的配置和客户端,一切都很好-它确实可以工作.
I am using spring-boot
and spring-rabbitmq
package. I have some config class and class representing client. This client class has one listener annotated with @RabbitListener
. Everything is fine with my config and client - it does work.
但是,我需要了解有关此客户端类内部的一些详细信息.在上下文中是否有特殊设置?我希望能够注入三个可用数据源之一(三个bean).我的意思是,我希望能够基于消息的第一个符号(由于使用自定义转换器,我可以假定此符号发生了),才可以注入/使用实际的数据源bean.
However, I need to know some details about internals of this client class. Is something special set in a context ? I would like to be able to inject one of three available datasources (three beans). I mean that I would like to be able based on the first symbol of message (thanks to custom converter I can assume that this symbol happens) that actual datasource bean will be injected/used.
有什么想法吗?也许 spring-rabbitmq
会以某种方式修改上下文?
Any ideas? Maybe spring-rabbitmq
modifies in some way context ?
推荐答案
具有 @RabbitListener
的类必须在应用程序上下文中声明为bean(无论哪种方式-都不属于问题范围).并且必须为 singleton
-仅创建一次对象.因此,所有注入都必须在其实例化/初始化期间完成.
The class with @RabbitListener
must be declared as a bean in application context (either way - out of scope of the question). And it must be singleton
- created only once object. Therefore all the injections must be done during its instantiation/initialization.
因此,在该 @RabbitListener
方法调用期间,您应该只选择一个正确的 DataSource
,例如:
So, during that @RabbitListener
method invocation you should just choose a proper DataSource
, for example from the:
@Autowired
Map<String, DataSource> dataSources;
您在问题中描述的不是Spring Dependency Injection Container的行为.
What you describe in your question isn't a behavior of Spring Dependency Injection Container.
这篇关于具有侦听器的类的上下文-spring-rabbitmq,内部结构和基于消息包含的注入bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!