8

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.

Andreas Wederbrand
  • 38,065
  • 11
  • 68
  • 78
  • You may have the issue too with `mvn spring-boot:run` but your configuration allow circular reference (default behaviour) whereas with `java -jar` something forbids it. – Nicolas Labrot Apr 06 '17 at 14:56
  • How can I control that behaviour? – Andreas Wederbrand Apr 06 '17 at 16:27
  • Searching google for this type of error lead to this issue [Using \@EnableGlobalAuthentication or \@EnableAutoConfiguration on Classes Using Method Security causes Bean Cycle](https://github.com/spring-projects/spring-boot/issues/2578). May relate to your issue. Are you using spring boot 1.2.2? :) – Nicolas Labrot Apr 06 '17 at 20:31
  • It's spring 1.5.2 – Andreas Wederbrand Apr 07 '17 at 06:36
  • You can disallow circular reference and launch `mvn spring-boot:run` using an [`ApplicationContextInitializer`](http://stackoverflow.com/questions/35217354/how-to-add-custom-applicationcontextinitializer-to-a-spring-boot-application) and check if you get the same exception than with `mvn spring-boot:run`. To be honest I'm not convinced. – Nicolas Labrot Apr 07 '17 at 06:59
  • I am having a very similar issue with Spring Boot 1.5.13. Did you find a solution? – Wim Deblauwe Jun 21 '18 at 07:30
  • 1
    In my case, it was due to having a class annotated with `@Component` _and_ having it declared explictly via `@Bean`. – Wim Deblauwe Jun 21 '18 at 08:28
  • @NicolasLabrot Could you share any documentation about the fact that the default configuration allow circular references ? I am facing the same issue and I really don't understand how it can happen with `java -jar` command and not when I use `mvn spring-boot:run`. I'm also trying to reproduce a minimal example to show the issue but I can't, it fails into cyclic dependency whenever I use both packaging and boot methods. – LG_ Jan 04 '19 at 14:38

2 Answers2

2

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

Alexey Stepanov
  • 561
  • 6
  • 15
1

[Adding as an answer as couldn't comment]

Not exactly sure why java -jar and mvn spring-boot:run runners are different. Adding @Lazy worked for java -jar runner.

A.java

@Autowired
@Lazy
//setter injection
public void setB(B b) {
    this.b = b;
}

B.java

// constructor injection
@Autowired
public B(A a) {
  this.a = a;
}