gpt4 book ai didi

spring-boot - 项目中多个@SpringBootApplication注解

转载 作者:行者123 更新时间:2023-12-03 06:14:44 37 4
gpt4 key购买 nike

在我的SpringBoot创建的项目中,

我使用@SpringBootApplication添加了2个主要类。

因为如果我使用STS,我可以在开始调试时选择一个主应用程序。

但是我发现当 SpringDemoApplication 启动时,RabbitMQApplication 也在运行。

这是规范吗?工作正常吗?

这是要重现的示例 https://github.com/MariMurotani/SpringDemo/tree/6_rabbitMQ

enter image description here

SpringDemoApplication

package demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class SpringDemoApplication {

public static void main(String[] args) {

SpringApplication application = new SpringApplication(SpringDemoApplication.class);
ApplicationContext context = application.run(args);

}
}

RabbitMQ应用程序

package demo;

import java.util.Date;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

import demo.configs.Const;
import demo.dto.Mail;

@SpringBootApplication
public class RabbitMQApplication implements CommandLineRunner {

@Autowired
ApplicationContext context;


@Autowired
RabbitTemplate rabbitTemplate;

@Bean
Queue queue() {
return new Queue(Const.RabbitMQMessageQue, false);
}

@Bean
TopicExchange exchange() {
return new TopicExchange("spring-boot-exchange");
}

@Bean
Binding binding(Queue queue, TopicExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(Const.RabbitMQMessageQue);
}

@Bean
SimpleMessageListenerContainer container(ConnectionFactory connectionFactory) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueueNames(Const.RabbitMQMessageQue);
//container.setMessageListener(listenerAdapter);
return container;
}

/*
For asyncronized receiving

@Bean
Receiver receiver() {
return new Receiver();
}

@Bean
MessageListenerAdapter listenerAdapter(Receiver receiver) {
return new MessageListenerAdapter(receiver, "receiveMessage");
}*/

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

@Override
public void run(String... args) throws Exception {
System.out.println("Waiting five seconds...");

while(0 < 1){
for(int i = 0 ; i < 5 ; i++){
String object = (String)rabbitTemplate.receiveAndConvert(Const.RabbitMQMessageQue);
if(object != null){
try{
System.out.println(new Date().toGMTString() + ": " + object);
ObjectMapper mapper = new ObjectMapper();
Mail mail = mapper.readValue(object, Mail.class);
System.out.println(mail.getToAddress() + " , " + mail.getStrContent());
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
Thread.sleep(10000);
}
}
}

最佳答案

@SpringBootApplication 注解是 @Configuration@EnableAutoConfiguration@ComponentScan 的快捷注解。

http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-using-springbootapplication-annotation.html

@ComponentScan 的默认行为是在同一包及其所有子包中查找 @Configuration@Component 类带注释的类。由于所有类都在同一个包中,因此当您启动其中任何一个类时,Spring 都会找到其他类并将它们视为 @Configuration 类,并注册它们的 bean 等。

所以,是的,考虑到您的项目设置,这是预期的行为。如果您不希望本地测试发生这种情况,请将每个 @SpringBootApplication 类放入单独的子包中。如果这超出了演示的范围,您可能会想要提出一个更好的设置(也许每个 @SpringBootApplication 的子项目)。

关于spring-boot - 项目中多个@SpringBootApplication注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33612130/

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