0

Spring Data KeyValue看起来非常适合快速构建模拟微服务。如何使用数据引导它?

我尝试向 中添加东西KeyValueAdapter,但是通过自己指定 bean,我最终得到了s 与不同Repository的 s 连接,因此没有可用的数据。 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;
    }
}
4

1 回答 1

1

我猜您宁愿使用存储库,因为适配器基本上已就位以插入其他Map实现。假设你有PersonRepository这样的:

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

那么这应该工作:

@Configuration
@EnableMapRepositories
class Application {

  @Autowired PersonRepository repository;

  @PostConstruct
  public void init() {
    repository.save(new Person(…));
  }
}
于 2016-02-09T17:45:17.053 回答