spring-boot-starter-amqp+application.yml的代码案例
时间: 2023-07-30 09:09:18 浏览: 82
好的,这里是一个使用`spring-boot-starter-amqp`和`application.yml`的代码案例:
在`pom.xml`文件中添加以下依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
```
在`application.yml`文件中添加以下配置:
```yml
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
```
这里配置了RabbitMQ的连接信息,包括主机名、端口、用户名和密码。
在代码中可以通过`@EnableRabbit`注解开启RabbitMQ支持,然后使用`@RabbitListener`注解指定消息监听方法。
例如:
```java
@SpringBootApplication
@EnableRabbit
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@RabbitListener(queues = "myqueue")
public void processMessage(String content) {
System.out.println("Received message: " + content);
}
}
```
这个例子中,我们使用`@RabbitListener`注解指定了一个名为`myqueue`的队列,当消息到达这个队列时,`processMessage`方法将被调用,打印出接收到的消息内容。
当然,还有其他更复杂的用法,比如发送消息、配置交换器、队列等等。你可以参考Spring Boot官方文档和RabbitMQ官方文档了解更多信息。