Two Spring projects to boot using @SpringBootApplication

I have a data project and a user interface project. Both projects are Spring boot applications. Both projects have the same root package (com.myorg) with the main class annotated with @SpringBootApplication .

The main class of the data project:

 package com.myorg; @SpringBootApplication public class DataApplication { public static void main(String[] args) { SpringApplication.run(DataApplication.class, args); } } 

The main class of the interface project:

 package com.myorg; @SpringBootApplication public class UiApplication { public static void main(String[] args) { SpringApplication.run(UiApplication .class, args); } } 

The user interface design depends on the data design through the following Gradle dependency:

 dependencies { compile('com.myorg:data:1.0') } 

If I run the user interface application, it starts without problems. However, if I run an integration test in a user interface application, for example:

 package com.myorg @RunWith(SpringRunner.class) @SpringBootTest public class UiIntTest { @Test public void contextLoads() { } } 

The following initialization error occurred:

java.lang.IllegalStateException: Found multiple @SpringBootConfiguration annotated classes

In the main data project class, if I replaced @SpringBootApplication with

 @Configuration @EnableAutoConfiguration @ComponentScan({ "com.myorg" }) 

When I try to run integration tests, I get the following initialization error:

 java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test 

For example, if I try to run:

 package com.myorg @RunWith(SpringRunner.class) @SpringBootTest public class DataIntTest { @Test public void contextLoads() { } } 

How to set up data projects and user interfaces?

+5
source share
2 answers

You need to specify which Spring Boot Main class to use with @SpringBootTest :

 @SpringBootTest(classes = YourUiSpringBootApp.class) 
+11
source

You should not have two SpringApplication annotations in one package.

Package one.

 twoapps.one; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication(scanBasePackageClasses = {One.class}) @EnableAutoConfiguration public class One extends SpringApplication { } 

Package two.

 twoapps.two; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication(scanBasePackageClasses = {Two.class}) @EnableAutoConfiguration public class Two extends SpringApplication { } 

Root Package and Launcher

 package twoapps; import org.springframework.boot.SpringApplication; import twoapps.one.One; import twoapps.two.Two; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Application { public static void main(String[] args) throws Exception { new Thread(() -> SpringApplication.run(One.class, args(args, "--spring.profiles.active=one"))).start(); new Thread(() -> SpringApplication.run(Two.class, args(args, "--spring.profiles.active=two"))).start(); } private static String[] args(String[] args, String s) { List<String> collect = Arrays.stream(args).collect(Collectors.toList()); collect.add(s); String[] strings = collect.toArray(new String[]{}); return strings; } } 

This is a terrible idea. do not do it. It is much better to have two different projects and a common project.

+1
source

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


All Articles