0

Spring Data KeyValue looks great for quickly knocking up a mock microservice. How can I bootstrap it with data?

I tried adding stuff to the KeyValueAdapter, but by specifying the bean myself, I've ended up with Repositorys that have been wired with a different KeyValueAdapter, and so don't have the data available.

@Configuration
@EnableMapRepositories
public class PersistenceConfig
{
    @Bean
    public KeyValueAdapter keyValueAdapter() {
        KeyValueAdapter adapter = new MapKeyValueAdapter(ConcurrentHashMap.class);
        Account testAccount = new Account();
        testAccount.id = "dj-asc-test";
        testAccount.givenName = "Gertrude";
        adapter.put(testAccount.id, testAccount, "accounts");
        Account read = (Account) adapter.get("dj-asc-test", "accounts");
        Assert.notNull(read);
        return adapter;
    }
}
DeejUK
  • 12,891
  • 19
  • 89
  • 169

1 Answers1

1

I guess you'd rather use a repository as the adapter is basically in place to plug other Map implementations. Assume you have a PersonRepository like this:

interface PersonRepository extends CrudRepository<Person, Long> { … }

Then this should work:

@Configuration
@EnableMapRepositories
class Application {

  @Autowired PersonRepository repository;

  @PostConstruct
  public void init() {
    repository.save(new Person(…));
  }
}
Oliver Drotbohm
  • 80,157
  • 18
  • 225
  • 211