2017-04-06 142 views
1

我一直在Intellij IDEA內開發一個彈簧引導應用程序。它現在已經完成,我即將把它發送給其他用戶。僅在運行java -jar時發生循環依賴關係,而不是在使用spring-boot時運行:運行

我用mvn clean install構建它,並嘗試用java -jar target/my-app.jar啓動內置的jar文件。

令我吃驚的失敗與一個異常(難讀,但至少斬幾行)

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? 

我也得到了一些ASCII藝術上的依賴關係

*************************** 
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 
└─────┘ 

所以我嘗試運行它與mvn spring-boot:run,它的工作原理!

我以爲java -jarmvn spring-boot:run運行的印象是一樣的。我錯過了什麼?

我想我可以修復循環依賴,但現在困擾我的是爲什麼這兩個選手不同。

謝謝。

+0

你可能也有'mvn spring-boot:run'的問題,但你的配置允許循環引用(默認行爲),而使用'java -jar'則有東西禁止它。 –

+0

如何控制該行爲? –

+0

搜索谷歌這種類型的錯誤導致這個問題[使用\\EnableGlobalAuthentication或\\EnableAutoConfiguration類使用方法安全導致豆週期](https://github.com/spring-projects/spring-boot/issues/2578 )。可能與您的問題有關。你使用的是spring boot 1.2.2嗎? :) –

回答

0

您必須使用maven-shade-plugin或類似的東西打包獨立使用的所有依賴關係。但是你確定你想單獨運行這個jar,而不是將它建立爲war,並將其部署到像Apache Tomcat這樣的應用服務器上?

+0

它與'spring-boot-maven-plugin'使用「類似的東西」。我相信。這個jar將被打包到docker容器中,我不想在我的docker映像中使用應用程序服務器。 –