Read CSV and assign it to service SpringBoot

I recently started exploring backend and SpringBoot and came across a big problem in my small project. I wanted to create a RestController in which I have a service that reads a singular .csv file and creates a List of objects (Id and conversionRate). However, I can’t assign List’s values to service, since I’m not creating a service object. Should I create a database and pull values and create objects from there?

@Service
public class CurrencyService {
    private List<Currency> currencies = CSVReader.readCSVFile(); //Doesn't work since readCSVFile() is static
}

Answer

Assuming you want to read the CSV file at startup and keep it in memory, you can use @PostConstruct annotation to do just that:

@Service
public class CurrencyService {
    private List<Currency> currencies;

    @PostConstruct
    fun init() {
        currencies = CSVReader.readCSVFile();
    }
}

Reference documentation: