13

In my project created by SpringBoot,

I have added 2 main classes with @SpringBootApplication.

Because if I use STS I can choose one main application when start to debug.

But I found that while SpringDemoApplication is up ,RabbitMQApplication is also running.

Is this specification ? working appropriately?

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

2 Answers 2

23

The @SpringBootApplication annotation is a shortcut 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 look for @Configuration and @Component classes within the same package and all sub-packages of the annotated class. Since all your classes are in the same package, when you start any one of them Spring will find the others and treat them like @Configuration classes, and register their beans, etc.

So yes, this is expected behavior given your project setup. Put each @SpringBootApplication class in a separate subpackage if you don't want this to happen for local testing. If this moves beyond a demo at some point you'll probably want to come up with a better setup (subprojects for each @SpringBootApplication perhaps).

2
  • 1
    I have 3 kinds of scripts which i want to run as periodic jobs. How do i structure them in the same project so that i can run one independently at a time?
    – Adi
    Sep 22, 2018 at 19:14
  • @Adi You may want to ask that as a standalone question, but you could essentially do what this answer recommends and have multiple classes with main methods and then specify the class name at run-time, or you could pass in an argument to a single main method to tell it which script to run.
    – nerdherd
    Sep 25, 2018 at 18:14
2

I recently faced the same scenario here and I solved with a simple solution.

My projected uses Maven and is configured with sub-modules like this:

my-parent
  |__ my-main (depends on my-other module)
  |__ my-other

Each module has its own main App class annotated with @SpringBootApplication. The problem is that both classes reside in the same package even though they are in different modules.

Once I start MyMainApp it also starts MyOtherApp. To avoid this I just had to do the following.

In the my-main module I have:

@SpringBootApplication
public class MyMainApp ... { ... }

and in the my-other module I have:

@SpringBootApplication
@ConditionalOnProperty(name = "my.other.active", havingValue = "true", matchIfMissing = false)
public class MyOtherApp ... { ... }

with application.properties with:

my.other.active=true

It works as expected.

1
  • Hey danieldestro, I tried your method and it works as expected, but then when I run a unit test for the app , the application is not loaded if @ConditionalOnProperty annotation is added
    – Elad Cohen
    Jul 12, 2022 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.