I'm wondering how others are using the repository populators.
I've looked into the code and the 'default' ResourceReaderRepositoryPopulator executes which will overwrite existing data...
I was thinking of extending the AbstractRepositoryPopulatorFactoryBean but this one is really tied to using Resources.
And hence the protected abstract ResourceReader getResourceReader(); which need to be implemented.
I'm basically looking for a Code-based RepositoryPopulator which code be done easily by implementing the RepositoryPopulator.
The populate method will in this case call directly the repository and do 'stuff'. E.g. checking if records exists, and *only* if not create them.
But the problem to what class assign this populator then.
I can create a e.g. a SimpleRepositoryPopulatorFactoryBean which is just a copy of AbstractRepositoryPopulatorFactoryBean without Resource related things. It would accept one ore more RepositoryPopulator which populate method would be called.
What do you think about this.
Note that I'm currently using similar code to populate my repositories:
But when I read about the RepositoryPopulator in Spring Data Commons I thought it would be much cleaner to migrate my custom class and implement the RepositoryPopulator. Much cleaner code, as the ApplicationListener<ContextRefreshedEvent> is off course very generic.Code:public class BootStrap implements ApplicationListener<ContextRefreshedEvent> { private static Log LOG = LogFactory.getLog(BootStrap.class); @Inject private UserRepository userRepository; @Override public void onApplicationEvent(ContextRefreshedEvent event) { LOG.info("Bootstrapping application..."); if (userRepository.count() == 0) { LOG.info("User repository is empty, creating default admin user..."); User admin = new User(..); userRepository.saveAndFlush(admin); } } }


Reply With Quote