Hi.
We are working on a refactoring a couple of big spring boot applications with multiple circular dependencies. We are also trying to run one of our Spring Boot apps as a graal image. It is based on Spring Cloud Functions.
We've read the docs and understood that circular dependencies is not recommended, nope we don't like them either, hence the ongoing refactoring work.
We have created a small spring boot app which demos the issue with the "spring.main.allow-circular-references = true" configuration which works if we run the app as a "vanilla spring boot app". It does not work when we try to include it in the native build configuration (api/pom.xml) when building and then running the built graal native image.
Here is the url to the repo https://github.com/nirre7/spring-boot-graal-native-image-with-circular-dependencies
Thanks in advance Niclas
Comment From: nosan
Hi @nirre7,
I reviewed your application, and it indeed doesn't work with field injection. However, if you modify it slightly as follows:
@Service
public class CircularService1 {
private CircularService2 circularService2;
@Autowired
public void setCircularService2(@Lazy CircularService2 circularService2) {
this.circularService2 = circularService2;
}
...
}
@Service
public class CircularService2 {
private final CircularService1 circularService1;
public CircularService2(CircularService1 circularService1) {
this.circularService1 = circularService1;
}
....
}
Your application will start
2025-01-28T21:17:04.483+02:00 INFO 49030 --- [demo] [ main] com.example.api.DemoApplication : Starting AOT-processed DemoApplication using Java 23.0.1 with PID 49030 (/Users/dmytronosan/IdeaProjects/spring-boot-graal-native-image-with-circular-dependencies/api/target/api started by dmytronosan in /Users/dmytronosan/IdeaProjects/spring-boot-graal-native-image-with-circular-dependencies) 2025-01-28T21:17:04.483+02:00 INFO 49030 --- [demo] [ main] com.example.api.DemoApplication : No active profile set, falling back to 1 default profile: "default" 2025-01-28T21:17:04.492+02:00 INFO 49030 --- [demo] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http) 2025-01-28T21:17:04.493+02:00 INFO 49030 --- [demo] [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2025-01-28T21:17:04.493+02:00 INFO 49030 --- [demo] [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.34] 2025-01-28T21:17:04.499+02:00 INFO 49030 --- [demo] [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2025-01-28T21:17:04.499+02:00 INFO 49030 --- [demo] [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 16 ms Hello from CircularService1! 2025-01-28T21:17:04.510+02:00 INFO 49030 --- [demo] [ main] o.s.c.f.web.mvc.FunctionHandlerMapping : FunctionCatalog: org.springframework.cloud.function.context.catalog.BeanFactoryAwareFunctionRegistry@72ae8c80 2025-01-28T21:17:04.513+02:00 INFO 49030 --- [demo] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '/' 2025-01-28T21:17:04.513+02:00 INFO 49030 --- [demo] [ main] com.example.api.DemoApplication : Started DemoApplication in 0.043 seconds (process running for 0.058)