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:
<repository:jackson-populator location="classpath:data.json" />
How would the above be expressed in a java @Configuration class?
This post suggests using the FactoryBean directly: https://stackoverflow.com/a/13566712/1746274
I tried that and the closest I got was the following but it's not quite right.
@Bean(name="repositoryPopulator")
public RepositoryPopulator getRespositoryPopulator() throws Exception {
final JacksonRepositoryPopulatorFactoryBean factory = new JacksonRepositoryPopulatorFactoryBean();
factory.getObject().setResourceLocation("classpath:test-data.json");
factory.afterPropertiesSet();
return factory.getObject();
}
The above results in a FactoryBeanNotInitializedException
with the message JacksonRepositoryPopulatorFactoryBean does not support circular references
.
Any ideas?
It's straight-forward actually:
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 anApplicationListener
and thus will listen to theContextRefreshedEvent
and trigger population when theApplicationContext
is fully initialized.