问题
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.
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.
Any ideas? Maybe spring-rabbitmq
modifies in some way context ?
回答1:
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.
So, during that @RabbitListener
method invocation you should just choose a proper DataSource
, for example from the:
@Autowired
Map<String, DataSource> dataSources;
What you describe in your question isn't a behavior of Spring Dependency Injection Container.
来源:https://stackoverflow.com/questions/43101776/context-of-class-with-listener-spring-rabbitmq-internals-and-injecting-beans