您缺少返回类型:
@Pointcut("execution(* backend.repository.QuestionsRepository.AnswerQuestion (..))")
并且您必须绑定参数名称,如下所示:
@Pointcut("execution(* backend.repository.QuestionsRepository.AnswerQuestion (..))
&& args(question, answer)") // wrapped for readability only
示例解决方案
服务接口:
package foo.bar.service;
public interface Service{
void whack(String thing, Integer thang);
}
实现类:
package foo.bar.service;
public class DefaultService implements Service{
@Override
public void whack(final String thing, final Integer thang){
System.out.println(
"Inside thing: " + thing + ", inside thang: " + thang
);
}
}
Spring AOP方面:
@Aspect
public class ServiceAspect{
@Pointcut("execution(* foo.bar.service.*.*(..))")
public void serviceMethodExecution(){
}
@Around(value = "serviceMethodExecution() && args(param1, param2)")
public void aroundServiceMethodExecution(final ProceedingJoinPoint pjp,
final String param1,
final Integer param2) throws Throwable{
System.out.println("Before thing: " + param1 + ", before thang: "
+ param2);
pjp.proceed();
System.out.println("After thing: " + param1 + ", after thang: "
+ param2);
}
}
弹簧上下文 XML:
<aop:aspectj-autoproxy proxy-target-class="false" />
<bean class="foo.bar.service.DefaultService" />
<bean class="foo.bar.aspects.ServiceAspect" />
主要测试类:
现在这里有一个主要的方法来测试整个过程。它启动了一个没有 XML 配置的 Spring ApplicationContext ,上面的 XML 定义了服务 bean 和方面(事实证明,没有 XML 的解决方案只起作用,因为我打开了 AspectJ 编织,我不知道我必须包含哪些 bean启用 aspectj-autoproxy,所以我现在使用ClassPathXmlApplicationContext
这个最小的 XML):
public static void main(final String[] args){
final ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("classpath:/aspectContext.xml");
final Service service = applicationContext.getBean(Service.class);
service.whack("abc", 123);
}
输出:
Before thing: abc, before thang: 123
Inside thing: abc, inside thang: 123
After thing: abc, after thang: 123
这应该让你开始。基本上:如果您使用 JDK 代理(spring 默认),您需要检查您正在拦截的方法是否由服务接口支持。在此处阅读有关Spring AOP 代理机制的信息。
笔记:
如您所见,我将方法参数绑定到方面,而不是切入点,因此使切入点可重用于具有不同参数签名的方法。但也可以在切入点中绑定它们,如下所示:
@Pointcut(value = "execution(* foo.bar.service.*.*(..)) && args(a,b)",
argNames = "a,b")
public void serviceMethodExecution(final String a, final Integer b){
}
@Around(value = "serviceMethodExecution(param1, param2)",
argNames = "param1,param2")
public void aroundServiceMethodExecution(final String param1,
final Integer param2,
final ProceedingJoinPoint pjp) throws Throwable{
System.out.println("Before thing: " + param1 + ", before thang: "
+ param2);
pjp.proceed();
System.out.println("After thing: " + param1 + ", after thang: "
+ param2);
}