如何bootstrapSpring数据KeyValue?
How to bootstrap Spring Data KeyValue?
Spring Data KeyValue 看起来非常适合快速构建模拟微服务。我怎样才能 bootstrap 它与数据?
我尝试向 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(…));
}
}
Spring Data KeyValue 看起来非常适合快速构建模拟微服务。我怎样才能 bootstrap 它与数据?
我尝试向 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(…));
}
}