Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cyclic dependency only when running java -jar, not with spring-boot:run

I've been developing a spring-boot application inside Intellij IDEA for a while. It's now complete and I'm about to send it of to other users.

I build it with mvn clean install and try to start the built -jar file with java -jar target/my-app.jar.

To my surprise it fails with an exception (hard to read but at least chopped up into several lines)

Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'userController':
Unsatisfied dependency expressed through field 'userClient';
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name '***.client.userclient.UserClient':
FactoryBean threw exception on object creation;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$EnableWebMvcConfiguration':
Unsatisfied dependency expressed through method 'setConfigurers' parameter 0;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'webMvcConfig':
Unsatisfied dependency expressed through field 'authenticationInterceptor';
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'authenticationInterceptor':
Unsatisfied dependency expressed through field 'authenticationClient';
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name '***.client.authenticationclient.AuthenticationClient':
FactoryBean threw exception on object creation;
nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException:
Error creating bean with name 'mvcResourceUrlProvider':
Requested bean is currently in creation: Is there an unresolvable circular reference?

I also get some ascii art over the dependencies

***************************
APPLICATION FAILED TO START
***************************

Description:

The dependencies of some of the beans in the application context form a cycle:

|  userController (field ****.client.userclient.UserClient ****.controller.user.UserController.userClient)
↑     ↓
|  ****.client.userclient.UserClient
↑     ↓
|  org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$EnableWebMvcConfiguration
↑     ↓
|  webMvcConfig (field ****.AuthenticationInterceptor ****.WebMvcConfig.authenticationInterceptor)
↑     ↓
|  authenticationInterceptor (field ****.client.authenticationclient.AuthenticationClient ****.AuthenticationInterceptor.authenticationClient)
↑     ↓
|  ****.client.authenticationclient.AuthenticationClient
↑     ↓
|  mvcResourceUrlProvider
└─────┘

So I try to run it with mvn spring-boot:run and it works!

I was under the impression that running with java -jar and mvn spring-boot:run would be the same. What am I missing?

I guess I could fix the cyclic dependencies but what bothers me now is why these two runners differ.

Thanks.

like image 437
Andreas Wederbrand Avatar asked Apr 06 '17 12:04

Andreas Wederbrand


People also ask

How is cyclic dependency detected?

Analyze cyclic dependenciesFrom the main menu, select Code | Analyze Code | Cyclic Dependencies. In the Specify Cyclic Dependency Analysis Scope dialog, select the scope of files that you want to analyze. Select the Include test sources option if you want to analyze your test code together with the production code.

What dependencies do I need for Spring boot?

Dependencies, inherited from the spring-boot-dependencies POM. Sensible resource filtering. Sensible plugin configuration.


1 Answers

It's a problem in spring, see
https://github.com/spring-projects/spring-boot/issues/6045
https://github.com/spring-projects/spring-framework/issues/18879

Temporary crutch solution
set in application.properties or different way this:
spring.main.lazy-initialization=true

like image 173
Alexey Stepanov Avatar answered Sep 27 '22 17:09

Alexey Stepanov