Spring boot项目,项目启动会自动跑该代码
@Component
public class MemoryConfig {
public Map<String,String> data = new HashMap<>();
@Value("${Sourcepath.config}")
String Sourcepath;
@Bean
public void addMemory() throws Exception {
//
String[] codes = {"xQCC001","xQCC002","xQCC003","xQCC004"};
for(String code : codes) {
List<String> fileNames = getList(Sourcepath + File.separator + code);
for(String fileName : fileNames ) {
StringBuilder value = new StringBuilder();
String path = Sourcepath + File.separator + code + File.separator + fileName;
File jsonFile = new File(path);
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(jsonFile), "UTF-8"));
String str = null;
while((str = br.readLine()) != null){
value.append(str);
}
data.put(fileName, value.toString());
}
}
}
public List<String> getList(String path) throws Exception{
List<String> result = new ArrayList<>();
File fileList = new File(path);
for(File file1:fileList.listFiles()) {
result.add(file1.getName());
}
return result;
}
}
这是一个Springboot组件,启动时自动执行代码,从配置路径加载文件名,并读取对应JSON文件内容,存储到内存中。通过`@Value`注解获取配置路径,使用`BufferedReader`读取文件,将内容存储到Map中。
1145

被折叠的 条评论
为什么被折叠?



