gpt4 book ai didi

java - 使用 Spring Boot 和 ApacheMQ 发送和使用消息时出现运行时错误

转载 作者:行者123 更新时间:2023-11-30 07:37:25 27 4
gpt4 key购买 nike

M 使用 Spring Boot 和 ActiveMQ 来发送和接收消息。请帮忙
我的 MAVEN 项目中有 3 个类

    1. SampleActiveMQApplication 

package hello;

import javax.jms.Queue;

import org.apache.activemq.command.ActiveMQQueue;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.EnableJms;

@SpringBootApplication
@EnableJms
public class SampleActiveMQApplication {

@Bean
public Queue queue()
{
return new ActiveMQQueue("welcome.queue");
}

public static void main(String[] args) {
SpringApplication.run(SampleActiveMQApplication.class, args);
}

}

M 使用 Spring Boot 和 ActiveMQ 来发送和接收消息。请帮忙
我的 MAVEN 项目中有 3 个类

    2.  Consumer

package hello;


import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class Consumer {

@JmsListener(destination = "welcome.queue")
public void receiveQueue(String text) {
System.out.println(text);
}

}

M 使用 Spring Boot 和 ActiveMQ 来发送和接收消息。请帮忙
我的 MAVEN 项目中有 3 个类

    3. Producer

package hello;


import javax.jms.Queue;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

@Component
public class Producer implements CommandLineRunner {

@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;

@Autowired
private Queue queue;

public void run(String... args) throws Exception {
send("Sample message");
System.out.println("Message was sent to the Queue");
}

public void send(String msg) {
this.jmsMessagingTemplate.convertAndSend(this.queue, msg);
}

}

M 收到错误 java.lang.NoClassDefFoundError: org/springframework/context/event/GenericApplicationListener 类未找到错误

  • pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>ApacheMQBoot</groupId>
    <artifactId>ApacheMQBoot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
    <!-- Spring version -->
    <spring-framework.version>4.1.0.RELEASE</spring-framework.version>
    <!-- ActiveMQ version -->
    <activemq.version>5.10.0</activemq.version>
    </properties>


    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.2.RELEASE</version>
    </parent>

    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>


    <!-- Spring aritifacts -->

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>3.2.2.RELEASE</version>
    </dependency>

    <!-- <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring-framework.version}</version>
    </dependency> -->


    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jms</artifactId>
    <version>${spring-framework.version}</version>
    </dependency>

    <!-- ActiveMQ Artifacts -->
    <dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-spring</artifactId>
    <version>${activemq.version}</version>
    </dependency>
    </dependencies>

    <!-- Using JDK 1.7 for compiling -->
    <build>
    <plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.5.1</version>
    <configuration>
    <source>1.8</source>
    <target>1.8</target>
    </configuration>
    </plugin>
    </plugins>
    </build>
    </project>
  • 我得到的错误是dis

    ////

    Exception in thread "main" java.lang.IllegalArgumentException: Cannot instantiate interface org.springframework.context.ApplicationListener : org.springframework.boot.logging.ClasspathLoggingApplicationListener
    at org.springframework.boot.SpringApplication.createSpringFactoriesInstances(SpringApplication.java:414)
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:394)
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:385)
    at org.springframework.boot.SpringApplication.initialize(SpringApplication.java:263)
    at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:237)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180)
    at hello.SampleActiveMQApplication.main(SampleActiveMQApplication.java:22)
    Caused by: java.lang.NoClassDefFoundError: org/springframework/context/event/GenericApplicationListener
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at org.springframework.util.ClassUtils.forName(ClassUtils.java:250)
    at org.springframework.boot.SpringApplication.createSpringFactoriesInstances(SpringApplication.java:407)
    ... 7 more
    Caused by: java.lang.ClassNotFoundException: **org.springframework.context.event.GenericApplicationListener**
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 21 more

    最佳答案

    类/接口(interface) org.springframework.context.event.GenericApplicationListener 从 spring 4.2 开始可用。您必须更改为较新版本的 spring。

    请参阅javadoc

    关于java - 使用 Spring Boot 和 ApacheMQ 发送和使用消息时出现运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35196169/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com