Results 1 to 7 of 7

Thread: Cannot get aspect to work with java.lang.reflect.Method in signature

  1. #1

    Default Cannot get aspect to work with java.lang.reflect.Method in signature

    Hi,

    I'd appreciate any help with a problem I'm having with an aspect. I've read the Spring documentation and Spring In Action but I still cannot see what I'm doing wrong!

    Basically, I've an after-throwing aspect configured using the AOP tags. The aspect works fine when my method just takes a Throwable object.
    But I need access to java.lang.reflect.Method as my aspect needs to find out the return type from my Business Layer.
    So when my aspect's method takes ([Method], [args], [target], Throwable) it just does not work. What configuration am I missing here?

    (Would post my code config but don't have access to it today)

    Thanks

  2. #2

    Default

    This is my application context:

    Code:
    <aop:config proxy-target-class="true">
           		<aop:aspect ref="serviceExceptionAspect">
           			<aop:after-throwing 
           			method="catchThrowable"
           			pointcut="execution(* com.application.business.impl.*.*(..)) and args(method, args, target)"
           			throwing="ex" />
           		</aop:aspect>
           </aop:config>

    And my serviceExceptionAspect method which is never entered:

    Code:
    public Wrapper catchThrowable(Method method, Object[] args, Object target, Throwable ex)
    	{
    What is wrong with this config? The pointcut works fine if I only have Throwable in the method signature....

  3. #3
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    Am I right assuming that you think that spring aop will automatically match your aspect Method parameter to the method metadata (Method object)?

  4. #4

    Default

    You probably are. How is this done so? Thanks.

  5. #5
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    AopService.java

    Code:
    package com.spring.aop;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class AopService {
    
        public void service() {
            System.out.println("xxxxxxxxxx: AopService.service()");
            throw new IllegalArgumentException("test exception");
        }
    }

    TestAspect.java

    Code:
    package com.spring.aop;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.reflect.MethodSignature;
    import org.aspectj.lang.annotation.AfterThrowing;
    import org.aspectj.lang.annotation.Aspect;
    import org.springframework.stereotype.Component;
    
    @Aspect
    @Component
    public class TestAspect {
    
        @AfterThrowing(pointcut = "execution(* com.spring.aop.AopService.*(..))", throwing = "e")
        public void advice(JoinPoint joinPoint, Throwable e) {
            MethodSignature signature = (MethodSignature)joinPoint.getSignature();
            System.out.println("xxxxxxxxx: TestAspect.advice(). Return type is " + signature.getReturnType());
        }
    }

    spring-config.xml

    HTML Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:util="http://www.springframework.org/schema/util"
           xsi:schemaLocation="
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
      http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
    
        <context:component-scan base-package="com.spring.aop"/>
        <aop:aspectj-autoproxy proxy-target-class="true"/>
    
    </beans>

    SpringStart.java

    Code:
    package com.spring;
    
    import com.spring.aop.AopService;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class SpringStart {
        public static void main(String[] args) throws Exception {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
    
            AopService aopService = (AopService)context.getBean("aopService");
            aopService.service();
        }
    }

    Output:
    Code:
    xxxxxxxxxx: AopService.service()
    xxxxxxxxx: TestAspect.advice(). Return type is void
    Exception in thread "main" java.lang.IllegalArgumentException: test exception
    	at com.spring.aop.AopService.service(AopService.java:10)
    	at com.spring.aop.AopService$$FastClassByCGLIB$$a6d02733.invoke(<generated>)
    	at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
    	at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:700)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
    	at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:54)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
    	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
    	at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:635)
    	at com.spring.aop.AopService$$EnhancerByCGLIB$$e28809a7.service(<generated>)
    	at com.spring.SpringStart.main(SpringStart.java:11)

  6. #6
    Join Date
    Mar 2009
    Location
    Brazil
    Posts
    25

    Default

    You should access the method using the JoinPoint class. Change your method signature from (Method method, Object[] args, Object target, Throwable ex) to (JoinPoint joinPoint, Throwable ex).

  7. #7
    Join Date
    May 2008
    Posts
    9

    Thumbs up

    Denis and Juk - thank you so much for replying to this question. I have been searching for the solution to this exact problem all day! You saved me a lot of time of experimenting.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •