A few @SpringBootApplication annotations in the project

In my project created by SpringBoot,

I added 2 main classes with @SpringBootApplication.

Because if I use STS, I can choose one main application when starting debugging.

But I found that while SpringDemoApplication is complete, RabbitMQApplication also works.

Is this a specification? work properly?

Here is this sample to play 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); } } 

RabbitMQApplication

 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); } } } 
+5
source share
1 answer

The @SpringBootApplication is an abbreviated annotation for @Configuration , @EnableAutoConfiguration and @ComponentScan .

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

The default behavior of @ComponentScan is to search for the @Configuration and @Component within the same package and all subpackages of the annotated class. Since all your classes are in the same package, when you start any of them, Spring will find the others and treat them as @Configuration classes and register their beans, etc.

So this is the expected behavior when setting up your project. Put each @SpringBootApplication class in a separate @SpringBootApplication if you do not want this to happen for local testing. If at some point this goes beyond the scope of the demo, you probably want to develop a better setting (perhaps subprojects for each @SpringBootApplication ).

+9
source

Source: https://habr.com/ru/post/1235553/


All Articles