Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring AOP pointcut with one certain argument

Tags:

java

spring

aop

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

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

Currently I have smt like:

@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]

Any help highly appreciated

Kind regards, Dan

UPDATE the method i'm trying to intercept is:

in public class com.x.y.MainClass :

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

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

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.

Regards, Dan

like image 960
Queequeg Avatar asked Oct 18 '11 10:10

Queequeg


People also ask

How do I declare a pointcut in Spring AOP?

In Spring AOP, a join point always represents a method execution. A pointcut is a predicate that matches the join points, and the pointcut expression language is a way of describing pointcuts programmatically.

What is the difference between JoinPoint and pointcut?

JoinPoint: Joinpoint are points in your program execution where flow of execution got changed like Exception catching, Calling other method. PointCut: PointCut are basically those Joinpoints where you can put your advice(or call aspect). So basically PointCuts are the subset of JoinPoints.

Does Spring AOP supports only method execution JoinPoint?

Spring AOP currently supports only method execution join points (advising the execution of methods on Spring beans). Field interception is not implemented, although support for field interception could be added without breaking the core Spring AOP APIs.


1 Answers

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.

@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.

@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.

like image 161
gkamal Avatar answered Sep 25 '22 05:09

gkamal