【问题标题】:How to bootstrap Spring Data KeyValue?如何引导 Spring Data KeyValue?
【发布时间】:2016-02-09 17:29:18
【问题描述】:

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

我尝试向KeyValueAdapter 添加东西,但通过自己指定 bean,我最终得到了 Repositorys,它与 不同 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;
    }
}

【问题讨论】:

    标签: java spring-data spring-data-keyvalue


    【解决方案1】:

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

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

    那么这应该可以工作:

    @Configuration
    @EnableMapRepositories
    class Application {
    
      @Autowired PersonRepository repository;
    
      @PostConstruct
      public void init() {
        repository.save(new Person(…));
      }
    }
    

    【讨论】:

    • 谢谢 - 我想我误解了适配器的作用。
    猜你喜欢
    • 2022-07-30
    • 2011-12-19
    • 2013-12-10
    • 2016-11-25
    • 1970-01-01
    • 2022-11-10
    • 2016-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多