Announcement Announcement Module
Collapse
No announcement yet.
Annotation problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Annotation problem

    Hi.
    I got problem with annotation in pointcut. I want to run my aspect only when methods which are invoked are annotated with my annotation.

    xml
    Code:
    <aop:aspect id="dataWarehouseAspect" ref="dataWarehouseAspectBean">
           <aop:around method="processMessage" pointcut="com.mikel.aspect.DataWarehouseAspect.dataWarehouseMethods()"/>
    </aop:aspect>
    I got more aspect which are ordered.

    Now the pointcuts in the class:
    Code:
        @Pointcut("@annotation(com.mikel.aspect.DataWarehouseMessage)")// the pointcut expression
        private void dataWarehouseMessage() {}// the pointcut signature  
        
        @Pointcut("execution(* com.mikel.server.kernel.service.*.*(..))")// the pointcut expression
        private void allServiceMethods() {}// the pointcut signature
        
        @Pointcut("execution(* com.mikel.server.kernel.CallmanServer.*(..))")// the pointcut expression
        private void allServerMethods() {}// the pointcut signature
        
        @Pointcut("((allServiceMethods() || allServerMethods()) && dataWarehouseMessage())")// the pointcut expression
        public void dataWarehouseMethods() {}// the pointcut signature
    Those pointcut are defined in DataWarehouseAspect. When annotaed methos are invoked, no action in processMessage method in this aspect

    What im doing wrong ? I tried almost everythink.

  • #2
    When i use @Transactional annotation everythink is ok. Im using
    Code:
     <aop:advisor order="2" advice-ref="txAdvice" pointcut="com.mikel.kernel.aspect.TransactionAspect.transactionalMethods()"/>
    Is this advisor have something in common with matching annotations ?

    Comment


    • #3
      Originally posted by mikel1982 View Post
      When i use @Transactional annotation everythink is ok. Im using
      Code:
       <aop:advisor order="2" advice-ref="txAdvice" pointcut="com.mikel.kernel.aspect.TransactionAspect.transactionalMethods()"/>
      Is this advisor have something in common with matching annotations ?
      Ok problem solved, i have my annotation infront of interface method not in implementation.

      Comment


      • #4
        I have another problem connected with this case. Im inside the aspect and i need to get annotation from method which has been invoked.

        I get the method by this code:

        Code:
            protected Method getMethod(ProceedingJoinPoint call)
            {
                Signature sign = call.getSignature();
                if(sign instanceof MethodSignature)
                {
                    return ((MethodSignature)sign).getMethod();
                }
            }
        But method its not method from implementation class but from interface. And in the interface there is no annotation. Somebody know how to get correct method ? or this annotation in different way ?

        Comment


        • #5
          I assume the code below is your advice.
          There is a getAnotation*() methods on Method type and you need to get a reference t it first.
          Here is the modified code.
          Code:
          protected Method getMethod(ProceedingJoinPoint call) {
                 MethodInvocation mi = ExposeInvocationInterceptor.currentInvocation();
                 Method method = mi.getMethod();
                 System.out.println(method.getAnnotations()[0]);
          }
          
          Here is my sample output:
          
               @beanpointcut.MyAnnotation()

          Comment


          • #6
            But this method is also from interface. I need method from the target.

            Comment


            • #7
              All you need to do is access the Method definition from the target object (the one that is being advised)
              Just interrogate your MethodInvocation a little further:
              Code:
              MethodInvocation mi = ExposeInvocationInterceptor.currentInvocation();
              Method iMethod = mi.getMethod();
              
              System.out.println("Method name: " + iMethod.getName());
              Annotation[] annotations = iMethod.getAnnotations();
              for (int i = 0; i < annotations.length; i++) {
              	System.out.println("IAnnotation: " + annotations[i]);
              }
              		
              Object obj = mi.getThis();
              try {
              	Method cMethod = obj.getClass().getMethod(iMethod.getName(), iMethod.getParameterTypes());
              	Annotation[] cAnnotations = cMethod.getAnnotations();
              	for (int i = 0; i < cAnnotations.length; i++) {
              		System.out.println("CAnnotation: " + cAnnotations[i]);
              	}
              } catch (Exception e) {
              	e.printStackTrace();
              }
              And the output on my end is:

              Method name: foo
              IAnnotation: @test.MyOtherAnnotation()
              CAnnotation: @test.MyAnnotation()
              executing foo()

              Comment

              Working...
              X