Spring boot preloading data from database in bean

10,173

Solution 1

A Cache would solve your requirement elegantly.

Ideally you would have a Service which has a method which returns the skills.

This method could looke like this:

import org.springframework.cache.annotation.Cacheable;

@Cacheable(value = "skills", key = "#root.methodName")
public List<Skill> getAllSkills() {
    return ... your skills ...;
}

To enable Caching in Spring Boot, add the @EnableCaching annotation to your configurations class, and add a Bean to configure it:

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;

@Bean
public CacheManager cacheManager() {
    return new ConcurrentMapCacheManager("skills", "othercachename");
}

This way, the method getAllSkills is just executed once, the first time it's called, and afterwards the values are returned from the cache-manager without even calling the method.

Solution 2

You can listen for the ApplicationReadyEvent, and then call the method (from @yglodt's answer) to initialise your cache.

Example code:

@Component
public class MyEventsListener {

    @Autowired
    SkillsService skillsService;

    @EventListener
    public void onApplicationReady(ApplicationReadyEvent ready) {
        skillsService.getAllSkills();
    }
}

Also remember that if you call getAllSkills() from within the SkillsService bean itself, you will not hit the cache, because the method is only advised when it is called on an injected proxy of the class.

If you're deploying the application as an executable jar (rather than a war file), then the simplest solution is to invoke the code that you want to run at start-up from your main method:

public class Application {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
        SkillsService skillsService = context.getBean(SkillsService.class);
        skillsService.getAllSkills();
    }

}
Share:
10,173

Related videos on Youtube

AndreasGloeckner
Author by

AndreasGloeckner

Updated on June 26, 2022

Comments

  • AndreasGloeckner
    AndreasGloeckner almost 2 years

    I'm asking for elements that have a recursive relationship in one table:

    child id | parent id

    1 | null

    2 | 1

    3 | 1

    4 | 2

    And so on...To ask for this whole datastructure it takes about 10 secondsAt the moment I can't redesign this table because it cost too much time (for more information: Spring Repository performance issues with recursive ORM Class)

    Now, I am thinking about preloading all data during spring startup in a bean, so that the client "communicates" with the bean and I update the data in bean and database. I think it doesn't matter how long this startup takes, but it matters how long the user has to wait for its answer.

    So far, I didn't manage it to preload it. I was trying to create a bean like this:

    public class AppConfig extends WebMvcConfigurerAdapter {
    ...
    @Autowired
    SkillDAO skillDAO
    ...
    @Bean(name="allSkills")
    public List<Skill> allSkills(){
        return skillDAO.findBySkill(null);
    }
    ...
    

    It doesn't work cause I get an error:

    Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'dataSource': Requested bean is currently in creation: Is there an unresolvable circular reference?

    By the way, I create all my beans in AppConfig. When I delete this "allSkills" bean, it works again.

    UPDATE

    @Component
    public class MyListener {
    
    @Autowired
    private SkillDAO skillDAO;
    
    @EventListener
    public void onApplicationReady(ApplicationReadyEvent ready) {
        System.out.println("++++++++++++++ HALLO +++++++++++++");
        skillDAO.findBySkill(null);
    }
    }
    

    ...

    @Bean(name="skillBean")
    public MyListener myListener() {
        return new MyListener();
    }
    
  • AndreasGloeckner
    AndreasGloeckner about 8 years
    Where do I put the "getAllSkills"-method? I can't use it in my SkillDAO-Interface. Eclipse says "The annotation @Cacheable is disallowed for this location". @Repository public interface SkillDAO extends CrudRepository<Skill, Integer>, CustomSkillDAO { @Cacheable(value = "skills", key = "#root.methodName") public List<Skill> findBySkill(Skill skill);
  • AndreasGloeckner
    AndreasGloeckner about 8 years
    Ok, I imported the javax.persistence.Cacheable instead of org.springframework.cache.annotation.Cacheable. Now I can annotate the method. ... Wow, when I invoked the method onces, the next call takes just 3 seconds instead of 13 seconds. Can I tell spring to invoke method once during startup or something? Is this cache stored at clientside or serverside?