首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么Spring-AOP切入点适用于save(..)但不是saveAll(..)

为什么Spring-AOP切入点适用于save(..)但不是saveAll(..)
EN

Stack Overflow用户
提问于 2020-07-11 06:35:33
回答 1查看 301关注 0票数 1

我有一个Spring数据存储库,如下所示:

代码语言:javascript
复制
package com.example.demo;

@RepositoryRestResource
public interface FooRepository extends JpaRepository<Foo, Long> {

    @Override
    <S extends Foo> S save(S entity);

    @Override
    <S extends Foo> List<S> saveAll(Iterable<S> entities);

}

像这样的一个方面:

代码语言:javascript
复制
@Aspect
@Component
public class FooAspect {

    @Before("execution(* org.springframework.data.repository.CrudRepository.save(*))")
    void crudSaveBefore(JoinPoint joinPoint) throws Throwable {
        System.out.println("crud save");
    }

    @Before("execution(* com.example.demo.FooRepository.save(*))")
    void fooSaveBefore(JoinPoint joinPoint) throws Throwable {
        System.out.println("foo save");
    }

    @Before("execution(* org.springframework.data.repository.CrudRepository.saveAll(*))")
    void crudSaveAll(JoinPoint joinPoint) throws Throwable {
        System.out.println("crud save all");
    }

    @Before("execution(* com.example.demo.FooRepository.saveAll(*))")
    void fooSaveAll(JoinPoint joinPoint) throws Throwable {
        System.out.println("foo save all");
    }

}

当我运行fooRepository.save(..)时,我在控制台中看到:foo save

当我运行fooRepository.saveAll(..)时,我在控制台中看到foo save allcrud save all

我希望saveAll只截取FooRepository版本,因为我直接对package.class.method进行了切入点。这似乎对save有效,但对saveAll无效。

这是因为saveAll中的参数是Iterable吗?或者在泛型中发生了某种类型擦除?还有别的吗?

EN

回答 1

Stack Overflow用户

发布于 2020-07-12 20:50:27

这似乎是AOP的问题。对于代理FooRepository.saveAll,它调用CrudRepository.saveAll @Before表达式:

AbstractAspectJAdvice 683

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62843210

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档