Spring Data KeyValue对于快速关闭模拟微服务看起来很不错。如何引导数据?

我尝试将内容添加到KeyValueAdapter中,但是我自己指定了bean,最后得到的Repository已与其他KeyValueAdapter连接,因此没有可用的数据。

@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;
    }
}

最佳答案

我猜您宁愿使用存储库,因为基本上可以使用适配器来插入其他Map实现。假设您有一个PersonRepository像这样:

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


然后这应该工作:

@Configuration
@EnableMapRepositories
class Application {

  @Autowired PersonRepository repository;

  @PostConstruct
  public void init() {
    repository.save(new Person(…));
  }
}

10-08 07:22