Spring boot: load text file lines at Bean creation time

To access a resource file from Spring, you can’t just refer to it to its path(“src/main/resources/words.txt”) as when packaged into the JAR, the path will likely change (depending on your config).

The simplest thing to do is placing the file inside the resource directory, and refer to it as “classpath:words.txt”

Caveat: you cannot transform the Spring resource into a java.io.File; it’d fail when packaged as the file structure does not exist in the JAR file. In the example below I access the file by getting the…