-
Notifications
You must be signed in to change notification settings - Fork 40.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Child context created with SpringApplicationBuilder runs parents runners #38647
Comments
Thanks for the report. Unfortunately, I'm not sure that I've understood the arrangement that you have described. Are you using the |
Yes, I'm trying to create child context in parents runner implementation. And it worked well on previous versions. |
Thanks very much. I understand things now. You can work around the problem by only allowing the parent runner to run once: package com.reproduce.parent;
import java.util.concurrent.atomic.AtomicBoolean;
import org.springframework.beans.BeansException;
import org.springframework.boot.Banner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;
import com.reproduce.child.ChildMainClass;
@Component
public class ParentRunner implements CommandLineRunner, ApplicationContextAware {
private final AtomicBoolean run = new AtomicBoolean();
private ApplicationContext applicationContext;
@Override
public void run(String... args) throws Exception {
if (run.compareAndSet(false, true)) {
System.out.println("Parent runner");
new SpringApplicationBuilder(ChildMainClass.class)
.parent((ConfigurableApplicationContext) applicationContext)
.bannerMode(Banner.Mode.OFF)
.run();
}
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
} |
After #37905 fix, child context created with SpringApplicationBuilder runs parents runners.
Child context creation code:
Code above is executed from parents CommandLineRunner and leads to infinite context recursion. Parents runner creates child context, which invokes parents runner and so on.
Looks like problem is in using
context.getBeanProvider(Runner.class)
instead ofcontext.getBeansOfType(ApplicationRunner.class)
inSpringApplication::callRunners
The text was updated successfully, but these errors were encountered: