我知道錯誤是自我解釋的,但是當我從構造函數中將rest模板的設置移除到@Autowired @Qualifier(「myRestTemplate」 )私人RestTemplate restTemplate,它的工作原理。在同一類中創建Bean的Spring Autowire會導致:請求的bean當前正在創建中錯誤*
只是想知道如何在構造函數中做到這一點,如果相同的類有我試圖自動裝配的bean定義?
org.springframework.beans.factory.BeanCurrentlyInCreationException: 錯誤創建名爲 'XXX' 豆:當前請求的bean是在 創造:是否有一個無法解決的循環引用?在常規
@Component
註解的類
@Component
public class xxx {
private RestTemplate restTemplate;
@Autowired
public xxx(@Qualifier("myRestTemplate") RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@Bean(name="myRestTemplate")
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
應該不是問題。上述實現應該按照原樣工作,只需從構造函數中刪除'@ Autowired'即可。 –
@KrishnaKuntala這不會工作,因爲我沒有默認的構造函數,所以我需要Autowired這個構造函數。 – Abhijeet
明白了你的觀點。你爲什麼要在同一個類中創建'@ Bean'?你可以在'ApplicationInitializer'(註解爲@ @ Configuration')的類中初始化它嗎? –