考慮下面的類:如何在春天讀取文件並將其分配給ArrayList?
public class Store {
private final ArrayList<String> store;
public ArrayList<String> getStore() {
return store;
}
public Store(ArrayList<String> store){
this.store = store;
}
}
我有一個名爲input.txt
我有標註有@RestController
正常控制器的文本文件,如下所示:
@RestController
@RequestMapping(value="/test")
public class Controller {
.
.
.
}
我需要做的以下操作:
- 閱讀input.txt中使用
Files.readAllLines(path,cs)
(JDK從1.7) - 將返回值(
List<String>
)到Store.store
- 我想使用Spring註解一路(我正在寫一個彈簧啓動應用程序)
- 我需要Store作爲一個Singleton bean。
- 商店需要在自舉應用程序的過程中進行初始化。
這個問題可能太模糊了,但我完全不知道如何使它更具體。
P.S.
我是Spring的新手。
我有幾個問題應該讓你的問題更加具體化...... 1)你是否試圖製作一個RESTful Web服務?如果是這樣,這個文件是否需要傳遞給服務器,它是在服務器上,還是在用戶的本地機器上? 3)當你說arrayList存儲,這是一個商店名稱列表?你可以期待什麼格式的input.txt? –
Steve
https://stackoverflow.com/questions/1363310/auto-wiring-a-list-using-util-schema-gives-nosuchbeandefinitionexception and https://www.mkyong.com/spring/spring-value-import-a -list-from-properties-file/ – StanislavL
是的,我正在嘗試製作一個RESTful WS。 和input.txt在服務器上是UTF-8格式。 –