java 带有一个特定参数的 Spring AOP 切入点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7806013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:30:27  来源:igfitidea点击:

Spring AOP pointcut with one certain argument

javaspringaop

提问by Queequeg

I need to create an aspect that I find hard to describe, so let me point out the ideas:

我需要创建一个我觉得难以描述的方面,所以让我指出这些想法:

  • any method within the package (or any subpackage) of com.x.y...
  • one method argument is an implementation of an interface javax.portlet.PortletRequest
  • there may me more arguments in the method
  • they may be in any order
  • com.xy.. 包(或任何子包)中的任何方法
  • 一个方法参数是接口 javax.portlet.PortletRequest 的实现
  • 方法中可能有更多参数
  • 它们可以按任何顺序排列

I need a pointcut and an "around" advice with the PortletRequest given

我需要一个切入点和一个带有给出的 PortletRequest 的“周围”建议

Currently I have smt like:

目前我有 smt 喜欢:

@Pointcut("execution(* com.x.y..*.*(PortletRequest,..)) && args(request,..)")
public void thePointcut(PortletRequest request) {
}


@Around("thePointcut(request)")
    public Object theAdvice(ProceedingJoinPoint joinPoint, PortletRequest request) {
...

and receive an error:

并收到一个错误:

ERROR 10:47:27.159 [ContainerBackgroundProcessor[StandardEngine[Catalina]]] o.s.web.portlet.DispatcherPortlet - Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet. mvc.HttpRequestHandlerAdapter': Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: w arning no match for this type name: PortletRequest [Xlint:invalidAbsoluteTypeName]

错误 10:47:27.159 [ContainerBackgroundProcessor[StandardEngine[Catalina]]] osweb.portlet.DispatcherPortlet - 上下文初始化失败 org.springframework.beans.factory.BeanCreationException:创建名为“org.springframework.web.servlet”的 bean 时出错。mvc.HttpRequestHandlerAdapter': bean 初始化失败;嵌套异常是 java.lang.IllegalArgumentException:警告不匹配此类型名称:PortletRequest [Xlint:invalidAbsoluteTypeName]

Any help highly appreciated

任何帮助高度赞赏

Kind regards, Dan

亲切的问候,丹

UPDATEthe method i'm trying to intercept is:

更新我试图拦截的方法是:

in public class com.x.y.MainClass:

公共类 com.xyMainClass 中

public String mainRender(Model model, RenderRequest request) throws SystemException

public String mainRender(Model model, RenderRequest request) throws SystemException

in public class com.x.y.asd.HelpClass:

公共类 com.xyasd.HelpClass 中

public final void helpAction(ActionRequest request, ActionResponse response, Model model)

public final void helpAction(ActionRequest request, ActionResponse response, Model model)

Of cource, I want to get the argument that implements PortletRequest, that is RenderRequest from the first method, and ActionRequest from the second.

当然,我想获取实现 PortletRequest 的参数,即第一个方法的 RenderRequest 和第二个方法的 ActionRequest。

Regards, Dan

问候,丹

回答by gkamal

As the error suggests you need to use the fully qualified name of the PortletRequest in the point cut expression - since it is a string the import context is not available during the time of evaluation of the expression.

由于错误表明您需要在切入点表达式中使用 PortletRequest 的完全限定名称 - 由于它是一个字符串,因此在表达式评估期间导入上下文不可用。

@Pointcut("execution(* com.x.y..*.*(javax.portlet.PortletRequest.PortletRequest,..)) && args(request,..)")
public void thePointcut(PortletRequest request) {
}

Since you already are selecting the type in the args construct you don't need that in the signature. The following should also work.

由于您已经在 args 构造中选择了类型,因此您不需要在签名中使用它。以下也应该有效。

@Pointcut("execution(* com.x.y..*.*(..)) && args(request,..)")
public void thePointcut(PortletRequest request) {
}

It is a and boolean operation - i.e., it needs to match the method pattern as well as the args construct.

它是一个布尔运算——即,它需要匹配方法模式以及 args 构造。