Search code examples
javaspringparameter-passingaopaspect

Passing parameter to same aspect method for different pointcut from xml


Is it possible to use same aspect method for multiple pointcuts but with different parameters given from xml? Something like this(1 and 2 are parameters) :

<!-- Aspect -->
<bean id="logAspect" class="LoggingAspect" />
<aop:config>
<aop:aspect id="aspectLoggging" ref="logAspect" >
    <aop:pointcut id="testAround" expression="execution(* methodA(..))" />
    <aop:pointcut id="testAroundC" expression="execution(* methodC(..))" />

    <!-- @Around -->
    <aop:around method="logProcess(1)" pointcut-ref="testAround" />
    <aop:around method="logProcess(2)" pointcut-ref="testAroundC" />
</aop:aspect>

When I call methodA I want logProcess method to output 1 and when I call methodC I want logProcess method to output 2

My logProcess method :

public Object logProcess(ProceedingJoinPoint joinPoint) throws Throwable {}

Spring @Transactional wont rollback after putting aspect around method


Solution

  • I'm pretty sure not.

    However you can generally achieve this kind of functionality by utilising the ProceedingJoinPoint object that you have as a parameter:

    public Object logProcess(ProceedingJoinPoint joinPoint) throws Throwable
    
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        if(method.getName().equals("methodA")) {
    
        }
        //etc..