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(…));
}
}