gpt4 book ai didi

java - spring 3.0 aop 切入点格式不正确 : expecting 'name pattern' error

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:10:41 24 4
gpt4 key购买 nike

以下是我的切入点和advise声明

//PointCut on A method which takes two parameters and is in a DAO
@Pointcut("execution(backend.repository.QuestionsRepository.AnswerQuestion (..))")
public void answerQuestionPointCut() {}


@Around(
value="web.activity.advisors.UserActivityAdvisor.answerQuestionPointCut()",
argNames="question,answer"
)

// Do something

}

出现以下错误

Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Pointcut is not well-formed: expecting 'name pattern' at character position 65
execution(backend.repository.QuestionsRepository.AnswerQuestion (..))
^

我坚持这个,任何指针

最佳答案

您缺少返回类型:

@Pointcut("execution(* backend.repository.QuestionsRepository.AnswerQuestion (..))")

并且您必须绑定(bind)参数名称,如下所示:

@Pointcut("execution(* backend.repository.QuestionsRepository.AnswerQuestion (..)) 
&& args(question, answer)") // wrapped for readability only

示例解决方案

服务接口(interface):

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);
}

}

Spring 上下文 XML:

<aop:aspectj-autoproxy proxy-target-class="false"  />
<bean class="foo.bar.service.DefaultService" />
<bean class="foo.bar.aspects.ServiceAspect" />

测试主类:

下面介绍一个测试整个过程的主要方法。它启动了一个 Spring ApplicationContext without XML configuration,上面的 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 默认),你需要检查你拦截的方法是否由服务接口(interface)支持。在这里阅读关于 Spring AOP proxy mechanisms 的信息.

注意:

如您所见,我将方法参数绑定(bind)到方面,而不是切入点,因此使切入点可重用于具有不同参数签名的方法。但也可以将它们绑定(bind)在切入点中,如下所示:

@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);
}

关于java - spring 3.0 aop 切入点格式不正确 : expecting 'name pattern' error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3832361/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com