当前位置: 首页 > 知识库问答 >
问题:

直接注入JpaRepository

鲁峰
2023-03-14

我正在寻找一种拦截所有发送到所有已定义方法的请求的方法。

(定义=JpaRepository接口上的任何内容)。

因此,例如,当有人调用repo.findAll()时,我将能够在之前和之后运行通用代码。

(通用=所有实体的相同代码)。

所以我做的是创建一个泛型类并在JpaRepository中实现方法,然后拦截所有请求。

@Repository
public class BaseJpaRepository<T> implements JpaRepository<T, Long> {

    @Autowired
    private JpaRepository<T, Long> repository;

    @Override
    public List<T> findAll() {
        //run some code here
        List<T> res = repository.findAll();
        //run some code here
        return res;
    }
    // all other methods here...
}

这是注入服务的接口:

@Repository
public interface UserRepository extends JpaRepository<UserEntity, Long> {

}

这就是豆子

@Repository
public class UserRepositoryBean extends  BaseJpaRepository<User> implements JpaRepository<User, Long> {

}

问题是私有JpaRepository

我还尝试将它的显式类型注入到构造函数if < code > UserRepositoryBean 中,并将其传递给父级。但是它不满足。

@Repository
public class UserRepositoryBean extends  BaseJpaRepository<User> implements JpaRepository<User, Long> {

public UserRepositoryBean(JpaRepositry<User, Long> repo){super(repo);}

}

有办法拦截所有的Spring jpa方法吗?

谢啦

共有1个答案

羊柏
2023-03-14

首先,定义基本接口,所有自定义存储库都将从该接口继承

@NoRepositoryBean
interface BaseJpaRepository<T, ID> extends JpaRepository<T, ID> {
     // You can also declare any generic methods here, 
     // and override (intercept) them in BaseJpaRepositoryImpl as well
}

这也是实现的问题

@NoRepositoryBean
class BaseJpaRepositoryImpl<T, ID>
        extends SimpleJpaRepository<T, ID>
        implements BaseJpaRepository<T, ID> {

    public BaseJpaRepositoryImpl(JpaEntityInformation<T, ID> entityInformation, EntityManager em) {
        super(entityInformation, em);
    }

    // One of 'defined' methods inherited from SimpleJpaRepository (and in turn from JpaRepository)
    @Override
    public List<T> findAll() {
        //run some code here
        List<T> res = super.findAll();
        //run some code here
        return res;
    }

    // other 'defined' methods to intercept ...
}

然后,您的自定义存储库将像往常一样看起来,除了它现在派生自您的BaseJpa存储库界面,而不是Spring的Jpa存储库

@Repository
interface UserRepository extends BaseJpaRepository<User, Long> {
}

为了使它全部正常工作,让我们修改通常放置在某个@Configuration类或@SpringBootApplication类上的以下注释

@EnableJpaRepositories(
        basePackages = {"org.example.repositories"},
        repositoryBaseClass = BaseJpaRepositoryImpl.class
)

另一个可行的方法是使用Spring AOP。您也可以在这里查看类似的问题

 类似资料:
  • 我已经在我的本地机器中用Keycloak设置了一个web应用程序。由于Im使用Keycloak作为SSO实现,我希望在我的web应用程序中,无论何时单击注册按钮,用户都被引导到注册页面,而不是通过登录页面。 这是指向注册表单的示例URL,但是,它包含一个,它像会话ID一样随机生成。 https://site.test/auth/realms/custom/login-actions/authent

  • 直接下载并用 <script> 标签引入,Vue 会被注册为一个全局变量。 在开发环境下不要使用压缩版本,不然你就失去了所有常见错误相关的警告! 开发版本: 包含完整的警告和调试模式 生产版本: 删除了警告,30.67KB min+gzip CDN 我们推荐链接到一个你可以手动更新的指定版本号: <script src="https://cdn.jsdelivr.net/npm/vue@2.5.

  • 直接插入排序的基本思想是:将 n 个有序数存放在数组 a 中,要插入的数为 x,首先确定 x 插在数组中的位置 p,然后将 p 之后的元素都向后移一个位置,空出 a(p),将 x 放入 a(p),这样可实现插入 x 后仍然有序。 例 1 本例子通过直接插入的方法对上述例子中的 number 数组进行排序。创建一个 Test27 类文件,在 main() 方法中开始编码,具体实现代码如下: 在上述代

  • 问题内容: 如何直接写入linux framebuffer? 问题答案: 看看FBIOPUT_VSCREENINFO,ioctl和mmap (我有代码,但没有此电脑,对不起) 编辑:这应该让您开始

  • 直接插入排序是比较简单的排序算法,它主要是将我们要排序的序列分成两个部分,一个有序一个无序,然后将无序序列中的值依次插入到有序序列中。 这么讲如果不能理解,我们拿例子去讲,假如有序列8 7 2 1 5 9 3这么一个序列,如下表所示。我们要将这个序列进行升序排序,接下来演示插入排序的过程。 初始序列 8 7 2 1 5 9 3 第一次排序 从前往后排,将8作为我们的有序序列的第一个值,而8后面的值

  • 问题内容: 从非直接字节缓冲区获取/输入比从直接字节缓冲区获取/输入更快吗? 如果我必须从直接字节缓冲区读取/写入,最好先读取/写入线程本地字节数组,然后再用字节数组完全更新(用于写入)直接字节缓冲区吗? 问题答案: 从非直接字节缓冲区获取/输入比从直接字节缓冲区获取/输入更快吗? 如果将堆缓冲区与不使用本机字节顺序的直接缓冲区进行比较(大多数系统为低字节序,直接字节缓冲区的默认值为大字节序),则