我試圖在填充物體類的屬性之前在我的物體類中撰寫一個方法。為此,我需要呼叫休息服務。我喜歡使用 Autowired 將我的 resttemplate 帶入我的 Entity 類。那有可能嗎,如果是的話,我們怎么能做到這一點。
@Entity
@Table(name = "IC_ORDER")
public class ICOrder implements Serializable {
@Id
@GenericGenerator(name = "UUIDGenerator", strategy = "uuid2")
@GeneratedValue(generator = "UUIDGenerator")
@Type(type = "uuid-char")
UUID id;
@Transient
@Autowired
private RestTemplate restTemplate;
@SneakyThrows
int start(Context context,String userId) {
//restTemplate.postForEntity // Trying to call a rest service here . But getting
restTemplate as null , Want to avoid new Resttemplate here.
}
這是 Config 類中的代碼
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
uj5u.com熱心網友回復:
物體屬于領域層。最好讓它們盡可能不依賴任何外部框架或庫的東西RestTemplate。此外,讓域層直接與外部系統互動對我來說并不合適。它們應該在更高級別的層中完成,例如服務層。所以最好有一個單獨的服務類OrderService來做這樣的邏輯。然后,您可以輕松地將其RestTemplate注入此服務類。
如果您堅持在域模型中執行此操作,請查看此內容以了解如何使用@Configurable和 AspectJ 執行此操作。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/497848.html
上一篇:嘗試使用JPA時獲取“java.lang.reflect.InaccessibleObjectException:無法使欄位私有int(變數)(包)可訪問”
