我有一個Spring類,在那裏我使用構造函數自動裝配服務,加上相同的構造函數我是初始化相同類的其他字段。春天的構造函數自動裝配和初始化其他字段
@Component
class Converter {
private TestService testService;
private Interger otherFields;
@Autowired
public Converter(TestService testService) {
this.testService = testService;
this.otherFields = new Integer(10);
}
}
我的功能工作正常,但它是一個很好的做法?會@Autowired
標註有otherFields
intialization過程
沒有,爲什麼呢?它只是表明'TestService'類型的依賴關係需要傳遞給構造函數,因爲它仍然只是對構造函數的調用。 –
@ M.Deinum,謝謝我有同樣的想法,只是我需要確認 –