[英]How to split Repository and Repository implementation to different modules?
鄉親們!
我的應用程序中有兩個Maven模塊-域和持久性。
域具有域對象,服務和到外部端點的“數據提供程序”接口,例如持久性。 域包含業務邏輯,沒有外部依賴關系-他對持久性一無所知。
持久性取決於域。 它從域模塊實現“數據提供者”接口。 它可能是關系數據庫的實現,nosql的實現,文件的實現等。
例如,我在域中具有接口PersonRepository ,如下所示:
public interface PersonRepository {
List<Person> findAll();
List<Customer> findByLastName(String lastName);
}
我想用Spring Data JPA實現數據提供者接口。 我需要寫這樣的東西:
public interface PersonRepository extends CrudRepository<Person, Long> {
List<Person> findAll();
List<Person> findByLastName(String lastName);
}
但我不想將spring依賴項注入“核心域”。 我想保持我的域非常輕巧和獨立。
在持久性模塊中是否可以使用Spring Data實現PersonRepository?
Spring Data JPA接口可以擴展多個接口。 像下面這樣的東西應該起作用。
在您的域模塊中定義:
public interface PersonRepository {
List<Person> findAll();
List<Customer> findByLastName(String lastName);
}
在您的持久性模塊中:
@Reposistory
public interface SpringDataJPAPersonRepository extends CrudRepository<Person, Long>, PersonRepository {
// Any Spring Data JPA specific overrides e.g. @Query
}
無需通過Spring Data擴展現成的接口,您只需在自己的接口中復制方法即可。 通常,您只需在其上放置@Repository
批注,Spring Data即可完成其工作。 但這將重新引入依賴性。
因此,您可以做的是在Spring Configuration中自己調用JpaRepositoryFactory.getRepository
。 這樣的事情應該起作用:
@Bean
public PersonRepository(RepositoryFactorySupport factory) {
return factory.getRepository(PersonRepository.class);
}
這將在您的持久性模塊中,或者在第三個模塊中進行所有模塊的接線,因此依賴關系不應該成為問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.