如果我想创建一个使用 Spring Data JPA 的库,如何限制可见性?

If I want to create a library that uses Spring Data JPA, how can I limit the visibility?

提问人:Jakkins 提问时间:11/17/2023 更新时间:11/17/2023 访问量:24

问:

问题

我需要创建一个库,但我找不到一种方法来限制存储库的可见性,同时允许将其导入服务。 这使用 Spring Data JPA。 我不能使用公共修饰符,因为如果我使用它,那么当用户导入库时将能够看到存储库。

项目包树

my.example.project
  .repository
  .model
    .entity
  .service

项目类树

my.example.project
  .repository
    MyRepo
  .model
    .entity
      MyEntity
  .service
    MyService

实现

实体

@Entity
@Table(name = "my_entity")
public class MyEntity{
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column
  private long id;

  @Column
  private String name;
}

回购

package my.example.project.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import my.example.project.entity.MyEntity;

/*
 * If the repo is not public then won't be visible to the service.
 * I cannot either use private, or protected.
 * I cannot use public because if I use it then when the user will 
 * import the library will be able to see the repo.
 */
@Repository
interface MyRepo extends JpaRepository<MyEntity, Long> {}

服务

package my.example.project.service;

@Service
public class MyService {

  private final MyRepo myRepo; // gives error

  public WlService() {}

}
java spring-data

评论


答: 暂无答案