Spring Boot - the best way to read data from database on startup

2496 views spring-boot
3

I would like to read data to List or Map from database on startup. Which is the best way to do it? The Spring Boot version is 5.

Is the below solution is good?

@Component
public class ApplicationStartup 
implements ApplicationListener<ApplicationReadyEvent> {

  /**
   * This event is executed as late as conceivably possible to indicate that 
   * the application is ready to service requests.
   */
  @Override
  public void onApplicationEvent(final ApplicationReadyEvent event) {

    // here your code ...

    return;
  }

} 

I'd like to storage data on static class, but I have doubt that is the best solution.

answered question

1 Answer

1

I don't quite understand what is your motive for doing so but for doing so you can create a bean using @Component and in that bean create a method with annotation @PostConstruct. you can do whatever you want in this method.

posted this

Have an answer?

JD

Please login first before posting an answer.