Search code examples
spring-bootglobalpojo

How to initiate a global method that can be accessed by all controllers in Spring Boot


I would like to initiate a simple POJO that generates an array of random Strings when initiated (or when the Spring Boot application starts). This array of Strings has to be shared across every controller in the Spring Boot application, and it cannot be different for different controllers. The class and methods (of this shared POJO) are internal to a Spring Boot application and are accessed by calling the getters in the controller methods (only).

Furthermore, I would like to avoid using the application.properties. The best solution would be java-only (no database such as H2 or offloading the POJO onto a file). Also, using the sessions will not help.

Something like this would help: http://www.masterspringboot.com/security/authentication/securing-spring-boot-with-in-memory-basic-authentication

How can I accomplish such a design ?

My idea is to simply use a micro-service and launch it separately, but I would like to confirm if there is something else I can do within the single Spring Boot application.


Solution

  • To answer my own question, the solution was to use the in-memory provided by the mapDB, and generate all the data during the spring-boot initiating itself...

    https://mvnrepository.com/artifact/org.mapdb/mapdb

    The source code example can be found using the search engines...