Announcement Announcement Module
Collapse
No announcement yet.
Cannot get aspect to work with java.lang.reflect.Method in signature Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

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

    Comment


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

      Comment


      • #4
        You probably are. How is this done so? Thanks.

        Comment


        • #5
          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)

          Comment


          • #6
            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).

            Comment


            • #7
              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.

              Comment

              Working...
              X