How to declare a respository populator bean using java config instead of XML?

前端 未结 1 1882
遥遥无期
遥遥无期 2021-01-04 01:58

I am working on a Spring based project that is (so-far) completely XML-free, except now I\'ve hit a wall with the Spring JPA repository populator:



        
相关标签:
1条回答
  • 2021-01-04 02:06

    It's straight-forward actually:

    @Configuration
    class ApplicationConfig {
    
      @Bean
      public JacksonRepositoryPopulatorFactoryBean repositoryPopulator() {
    
        Resource sourceData = new ClassPathResource("test-data.json");
    
        JacksonRepositoryPopulatorFactoryBean factory = new JacksonRepositoryPopulatorFactoryBean();
        // Set a custom ObjectMapper if Jackson customization is needed
        factory.setObjectMapper(…);
        factory.setResources(new Resource[] { sourceData });
        return factory;
      }
    }
    

    By returning the FactoryBean itself, Spring will take care of invoking all the necessarry callback interfaces (i.e. setApplicationContext(…), setBeanClassLoader(…) etc.). The factory bean is an ApplicationListener and thus will listen to the ContextRefreshedEvent and trigger population when the ApplicationContext is fully initialized.

    0 讨论(0)
提交回复
热议问题