Context of class with listener - spring-rabbitmq, internals and injecting beans based on contain of message

依然范特西╮ 提交于 2019-12-25 09:47:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!