'SpringApplicationBuilder parent webapp and child without webapp doesn't work

I have a spring boot app with a parent application context and a child application context with some legacy code. I would like to have a webapp in the parent application context, and have the child context be a non webapp.

I have my config and properties in application.yml, that gets loaded in the main class

@ComponentScan(...)
@PropertySource(value = "classpath:application.yml", factory = YamlPropertySourceFactory.class)
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class MyApplication(
    public static void main(String[] args) {
        new SpringApplicationBuilder(MyApplication.class)
                    .profiles("Prod")
                    .web(WebApplicationType.SERVLET)
                    .listeners(new MainAppListener())  // logging for some of the events
                    .child(LegacyServiceConfig.class)
                    .web(WebApplicationType.NONE)
                    .listeners(new LegacyAppListener())  // logging for some of the events
                    .run(args);
    }
}

What I've tried so far:

  1. I don't have any web application type config in my application.yml (spring.main.web-application-type). I hoped that the configuration I've specified thru code, in the above snippet would have worked, but it doesn't. Neither of the application contexts start as a webapp, effectively ignoring the config web(WebApplicationType.SERVLET) in the above snippet.
  2. I tried putting spring.main.web-application-type=servlet in my application.yaml. But that seems to apply this config to both parent and child application context. And both of them try to start as webapps, which is not what I want.

How do I get this to work? Any pointers?



Solution 1:[1]

As per my knowledge in spring-boot start from main only and the main class is provided by Maven, because I had worked on Maven project. All the properties in the project like JDBC connection, port change you can do it in .properties(extension) type file. And this file can also be in format of .yml also. These file is totally different from main class.

If you make any changes in main or add any extra annotation in main then it will throw an error.

So keep every properties in .properties or .yml file and all configuration component scan in pom.xml file you can have two .properties file also

But main class should be simple like this:

@SpringBootApplication()
public class SpringBootProjectApplication {

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

}

Hope this will help.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mark Rotteveel