spring 3.0 aop 切入点格式不正确:期望“名称模式”;错误
以下是我的切入点和建议声明
//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 (..))
^
我卡在这个,任何指针
The following is my pointcut and advise declaration
//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
}
I get the following error
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 (..))
^
Am stuck on this, Any pointers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您缺少返回类型:
并且必须绑定参数名称,如下所示:
示例解决方案
服务接口:
实现类:
Spring AOP方面:< /strong>
Spring Context XML:
测试主类:
现在这是一个测试整个过程的主要方法。它启动一个 Spring ApplicationContext
没有 XML 配置,上面的 XML 定义了服务 bean 和切面(事实证明,没有 XML 的解决方案只能工作,因为我打开了 AspectJ 编织,我不知道我必须包含哪些bean 才能启用aspectj-autoproxy,因此我现在使用ClassPathXmlApplicationContext
和这个最小的XML):输出:
这应该可以帮助您开始。基本上:如果您使用 JDK 代理(spring 默认),您需要检查您正在拦截的方法是否由服务接口支持。阅读此处有关 Spring AOP代理机制。
注意:
如您所见,我将方法参数绑定到方面,而不是切入点,因此使切入点可重用于具有不同参数签名的方法。但也可以将它们绑定在切入点中,如下所示:
You are missing the return type:
and you have to bind the argument names, something like this:
Example Solution
Service Interface:
Implementation class:
Spring AOP aspect:
Spring Context XML:
Main class for testing:
Now here's a main method to test the whole process. It starts a Spring ApplicationContext
without XML configurationwith the above XML defining the service bean and the aspect (it turns out that the solution without XML only worked because I had AspectJ weaving turned on, I don't know what beans I have to include to enable aspectj-autoproxy, so I now useClassPathXmlApplicationContext
with this minimal XML):Output:
This should get you started. Basically: you need to check that the methods you are intercepting are backed by a service interface if you use JDK proxies (spring default). Read here about Spring AOP proxy mechanisms.
Note:
As you see I bind the method arguments to the aspect, not the pointcut, hence making the pointcut reusable for methods with different argument signatures. But it would also be possible to bind them in the pointcut, like this:
你应该这样写,
注意
"... (* backend..."
*
并且应该使用空格。You should write like this
pay attention to
"... (* backend..."
*
and a space should be used.请注意,您与
org.aspectj.lang.annotation.Before
中的@Before
注释有类似的行为。您可以使用不带 execution 关键字且不带返回类型的表达式:
或同时使用两者:
但不能在不使用返回类型的情况下使用
execution
关键字。Note that you have similar behaviour with the
@Before
annotation inorg.aspectj.lang.annotation.Before
.You can use the expression without execution key word and without a return type:
or with both:
but you can not use the
execution
keyword without using the return type.