简体   繁体   English

无法在 MessageListener 类 AMQP 中自动装配 bean

[英]Cannot autowire bean in MessageListener Class AMQP

Ok.好的。 So i have a rabbit configuration class with some constants and I try to add a service to my listenercontainer listener.所以我有一个带有一些常量的兔子配置类,我尝试向我的 listenercontainer 侦听器添加一个服务。

@Configuration
public class RabbitConfig {

@Bean
public ConnectionFactory connectionFactory() {
    CachingConnectionFactory connectionFactory = new CachingConnectionFactory(HOST);
    connectionFactory.setPort(CONN_PORT);
    connectionFactory.setUsername(USERNAME);
    connectionFactory.setPassword(PASSWORD);
    return connectionFactory;
}

@Bean
public AmqpAdmin amqpAdmin() {
    return new RabbitAdmin(connectionFactory());
}

@Bean
public RabbitTemplate rabbitTemplate() {
    RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory());
    rabbitTemplate.setReplyQueue(replyQueue());
    rabbitTemplate.setCorrelationKey(UUID.randomUUID().toString());
    return rabbitTemplate;
}

@Bean
public Queue replyQueue() {
    return new Queue(REPLY_QUEUE_NAME);
}

@Bean
public SimpleMessageListenerContainer messageListenerContainer() {

    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory());
    container.setQueueNames(QUEUE_NAME);
    container.setMessageListener(messageListener());
    return container;
}

@Bean
public MessageListener messageListener(){
    return new RabbitListener();
}

} }

I am trying to inject into the messagelistener which is created in the last lines a service from my project.我正在尝试将我的项目中的服务注入到在最后一行中创建的消息侦听器中。 This triggers an error of cannot autowire field as if the field is not managed by Spring.这会触发无法自动装配字段的错误,就好像该字段不是由 Spring 管理的一样。 I did some research and I verified my component scan package and it's set to all the project, I have annotated the rabbitlistener with @Component so I can't really find the mistake or why Spring cannot autowire the field in my listener class.我做了一些研究,并验证了我的组件扫描包并将其设置为所有项目,我用 @Component 注释了 rabbitlistener,所以我无法真正找到错误或为什么 Spring 无法在我的侦听器类中自动装配该字段。 Here is the code.这是代码。

@Component
public class RabbitListener implements MessageListener {
Logger logger = LoggerFactory.getLogger(this.getClass());

@Autowired
ImagesService imagesService;


@Override
public void onMessage(Message message) {
//processing message
}

Any ideas please?请问有什么想法吗?

Would be better if you'd share the full StackTrace, but I suggest you do something like this:如果你能分享完整的 StackTrace 会更好,但我建议你做这样的事情:

  1. Add @ComponentScan for your @Configuration class and specify there those packages where are your RabbitListener and ImagesService classes为你的@Configuration类添加@ComponentScan并在那里指定你的RabbitListenerImagesService类所在的那些包

  2. Mark the last two with @Component (Yes, I see that on your RabbitListener , but it isn't clear where your ImagesService and how it is going around it)@Component标记最后两个(是的,我在你的RabbitListener上看到了,但不清楚你的ImagesService在哪里以及它是如何绕过它的)

  3. Than @Autowire RabbitListener to that RabbitConfig instead of @Bean for it.@Autowire RabbitListener到那个RabbitConfig而不是@Bean

And be careful with the @Component and @Bean mix: you end up with two bean, if you have @ComponentScan for that package, of course.并注意与@Component@Bean混合:你最终有两个豆,如果你有@ComponentScan该软件包,当然。

Okay, u need to @Autowire the RabbitListener bean.好的,你需要@Autowire RabbitListener bean。 Since the RabbitListener is a bean that need to be managed by the IOC, since is declared @Component , hence @runtime the RabbitListener is not in the context, hence autowire it in the config class like so由于RabbitListener是一个需要由 IOC 管理的 bean,因为声明了@Component ,因此 @runtime RabbitListener不在上下文中,因此像这样在配置类中自动装配它

@Configuration
public class RabbitConfig {

@Bean
public ConnectionFactory connectionFactory() {
    CachingConnectionFactory connectionFactory = new CachingConnectionFactory(HOST);
    connectionFactory.setPort(CONN_PORT);
    connectionFactory.setUsername(USERNAME);
    connectionFactory.setPassword(PASSWORD);
    return connectionFactory;
}

@Bean
public AmqpAdmin amqpAdmin() {
    return new RabbitAdmin(connectionFactory());
}

@Bean
public RabbitTemplate rabbitTemplate() {
    RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory());
    rabbitTemplate.setReplyQueue(replyQueue());
    rabbitTemplate.setCorrelationKey(UUID.randomUUID().toString());
    return rabbitTemplate;
}

@Bean
public Queue replyQueue() {
    return new Queue(REPLY_QUEUE_NAME);
}

@Bean
public SimpleMessageListenerContainer messageListenerContainer() {

    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory());
    container.setQueueNames(QUEUE_NAME);
    container.setMessageListener(rabbitListener); // reference the autowired RabbitListener on this line
    return container;
}

@Autowire
private RabbitListener rabbitListener;
}

That should resolve this error.那应该可以解决这个错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM