我有一个Spring数据存储库,如下所示:
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);
}像这样的一个方面:
@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 all和crud save all
我希望saveAll只截取FooRepository版本,因为我直接对package.class.method进行了切入点。这似乎对save有效,但对saveAll无效。
这是因为saveAll中的参数是Iterable吗?或者在泛型中发生了某种类型擦除?还有别的吗?
发布于 2020-07-12 20:50:27
这似乎是AOP的问题。对于代理FooRepository.saveAll,它调用CrudRepository.saveAll @Before表达式:
AbstractAspectJAdvice 683

https://stackoverflow.com/questions/62843210
复制相似问题