首页 > 解决方案 > 即使在新部署之后,Spring Boot 也会保留属性

问题描述

目前,我正在通过 HTTP 将 Android 应用程序的应用程序崩溃日志发送到我的服务器(acra),我的服务器将它们保存在如下属性中:

@RestController
public class EndlessBlowReportController {
    
    public int counter;

    @Autowired
    public static final Properties defaultProperties = new Properties();

    @PostMapping("/add_report")
    public void addReport(@RequestBody String report) {
        try {
            JSONObject jsonObject = new JSONObject(report);
            defaultProperties.put(counter, report);
            counter++;

        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }

    @GetMapping("/get_reports")
    public List<String> getReports() {
        List<String> reports = new ArrayList<>();
        try {
            for(int i=0;i<defaultProperties.size();i++) {
            reports.add((String)defaultProperties.get(i));
            }
        } catch (Exception ex) {
        }
        return reports;
    }
}

在我部署新版本的服务器之前它工作正常。

即使在部署之后,我如何才能保留我的属性?

标签: javaspringspring-boot

解决方案


这些属性仅存储在内存中,不会持久保存到任何永久存储中,例如文件或数据库。我的建议是不要将此信息存储在属性中,而是将其存储在数据库中,或者作为文件存储在文件存储中。

例如,如果您使用文件解决方案,您可以在启动期间加载文件并在每次获得新报告时更新文件。通过这样做,您将保留信息,并且每次重新启动服务器时它都不会消失。

我希望你觉得这个答案有帮助。

祝你好运!


推荐阅读