我成功地自動裝配了一個這樣的 bean 串列。
@Autowire
private final List<SpecificType> beans;
我怎樣才能使它不可修改?更具體地說,如何使用不可修改的串列自動裝配?
uj5u.com熱心網友回復:
您可以使用建構式注入并執行以下操作:
@Component
public class ComponentClass {
private final List<SpecificType> beans;
public ComponentClass(List<SpecificType> beans) {
this.beans = Collections.unmodifiableList(beans);
}
}
uj5u.com熱心網友回復:
而不是使用這個:
@Autowire
private final List<SpecificType> beans;
使用類建構式接收串列并使其不可變。
@Component
public YourClass{
private final List<SpecificType> beans;
public YourClass(List<SpecificType> beans){
this.bean = Collections.unmodifiableList(beans);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/433087.html
上一篇:根據欄位的值隱藏表單選項卡
