在這篇文章之后,我配置了
(1) 一個尤里卡服務器,
(2) 作為 (1) 的客戶的收費服務,
(3) 從 (2) 讀取資料的通行費儀表板。
一切順利,直到我使用 @LoadBalanced 進行了一些更改(也許這不完全是由于負載平衡器,但我會在下面發布相關的代碼塊)
在(2)中的 bootstrap.properties 中,公共端點名稱是
spring.application.name = demo-tollrate-service
在(3)中的DashboardController.java中,可以看到HTTP地址改為上面的應用名
// import some libraries
@Controller
public class DashboardController {
@Autowired
private RestTemplate restTemplate;
@LoadBalanced
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
@RequestMapping("/dashboard")
public String GetTollRate(@RequestParam int stationId, Model m) {
TollRate tr = restTemplate.getForObject("http://demo-tollrate-service/tollrate/" stationId, TollRate.class);
m.addAttribute("rate", tr.getCurrentRate());
return "dashboard";
}
}
這篇文章中的 pom.xml 大致相同
運行此應用程式時,出現以下錯誤。
***************************
APPLICATION FAILED TO START
***************************
Description:
The dependencies of some of the beans in the application context form a cycle:
┌──->──┐
| dashboardController (field private org.springframework.web.client.RestTemplate com.example.demo.DashboardController.restTemplate)
└──<-──┘
Action:
Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.
任何人都可以在這里幫助我嗎?謝謝你。
uj5u.com熱心網友回復:
它有一個回圈依賴,DashboardController 依賴于 RestTemplate,但 RestTemplate 是在 DashboardController 內部定義的。
Java 版本沒有任何區別,但可能是 Spring 版本。新的 Spring 版本默認不允許回圈參考。您可以更改在 application.properties 檔案中添加一個屬性,在這里您可以找到更多詳細資訊:
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.6-Release-Notes#circular-references-prohibited-by-default
但是我推薦重構,沒有充分的理由在控制器內部定義 RestTemplate,最好在 @Configuration 類中進行。
uj5u.com熱心網友回復:
Spring 101:您不能在控制器類中定義 bean。將RestRemplate定義移動到@Configuration類或應用程式主類。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/391914.html
