java - RabbitMQ MessageConverter 在运行 Spring Boot 应用程序时出现错误

标签 java spring maven spring-boot rabbitmq

我正在尝试运行一个 Spring Boot 应用程序 (1.4.7.RELEASE),它将向 RabbitMQ 队列吐出消息。我的构建工作成功,但是当我尝试通过 mvn clean spring-boot:run 运行应用程序时,我在文件 ProduceMessage.java

中收到以下错误
@PropertySource("classpath:application.properties")
@Component
@ContextConfiguration("classpath:META-INF/spring/rabbitmq-producer.xml")
public class ProduceMessage {

    private static final Logger logger = LoggerFactory.getLogger(ProduceMessage.class.getName());

    @Autowired
    private RabbitTemplate myEventTemplate;

    @Autowired
    private MessageConverter ctdMessageConverter;

    @Value("${fieldChangedEvent.MainQueue}")
    private String mainQ;

    /*
     * (non-Javadoc)
     * 
     * @see com.ge.predix.dispatcherqproducer.api.produceFieldChangedEvent#
     * produceFieldChangedEvent(com.ge.dsp.pm.solution.service.fieldchanged.
     * FieldChangedEvent)
     */
    public boolean produceStringMessage(String data) {

        logger.debug("In produceStringMessage......");

        MessageProperties prop = new MessageProperties();
        prop.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);

        Message msg = ctdMessageConverter.toMessage(data, prop);

        logger.debug("publishing string to ......= " + mainQ);
        myEventTemplate.convertAndSend(mainQ, msg);

        return true;
    }
}
<小时/>

错误:

2017-10-04 11:06:08.830[0;39m [32m INFO[0;39m [35m62162[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.apache.catalina.core.StandardService [0;39m [2m:[0;39m Stopping service [Tomcat] [2m2017-10-04 11:06:08.849[0;39m [32m INFO[0;39m [35m62162[0;39m [2m---[0;39m [2m[ main][0;39m [36mutoConfigurationReportLoggingInitializer[0;39m [2m:[0;39m

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.

[2m2017-10-04 11:06:08.944[0;39m [31mERROR[0;39m [35m62162[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.b.d.LoggingFailureAnalysisReporter [0;39m [2m:[0;39m

*************************** APPLICATION FAILED TO START *************************** Description: Field ctdMessageConverter in com.ge.power.tcs.producer.ProduceMessage required a bean of type 'org.springframework.amqp.support.converter.MessageConverter' that could not be found.

Action:

Consider defining a bean of type 'org.springframework.amqp.support.converter.MessageConverter' in your configuration.

<小时/>

rabbitmq-生产者.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:oxm="http://www.springframework.org/schema/oxm"
    xsi:schemaLocation="http://www.springframework.org/schema/rabbit
           http://www.springframework.org/schema/rabbit/spring-rabbit.xsd
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm.xsd">
    <bean id="ctdMessageConverter"
        class="org.springframework.amqp.support.converter.ContentTypeDelegatingMessageConverter">
        <property name="delegates">
            <map>
                <entry key="text/plain" value-ref="simpleMessageConverter" />
            </map>
        </property>
    </bean> 
    <bean id="simpleMessageConverter"
        class="org.springframework.amqp.support.converter.SimpleMessageConverter" />          
 </beans>

最佳答案

当我们在 Spring Boot 的主 Application 类中提到 @ImportResource("classpath:META-INF/spring/rabbitmq- Producer.xml") 而不是 @ 时,问题就解决了ContextConfiguration("classpath:META-INF/spring/rabbitmq- Producer.xml") 包含 bean 实例化

关于java - RabbitMQ MessageConverter 在运行 Spring Boot 应用程序时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46569822/

相关文章:

java - 如何在组件初始化之前在Spring Boot测试中加载h2数据

java - Spring Controller 用于表单处理

javac对包的识别

java - 优化单词列表中的字符串检查 (Java)

java - 使用 Spring 实现基于 token 的授权的最佳方法是什么?

java - 为什么我在 ScalaIDE 中运行代码时会得到 `java.lang.NoClassDefFoundError: scala/Function1`?

maven - springboot 应用程序未获取 Kubernetes configmap 名称

java - 如何停止 swt 上的按键事件传播

java - 通过复制目录安装 JRE 是否会影响类数据共享 (CDS)?

java - 无法使用java API连接到orientdb数据库