Results 1 to 4 of 4

Thread: no annotations within aspects ?!

  1. #1
    Join Date
    Aug 2005
    Location
    Germany, Frankfurt am Main
    Posts
    22

    Question no annotations within aspects ?!

    Hi,

    I'm trying to realise a logging of the current user action.
    For that I defined an anspect that logs each method called in a certain package.
    The methods can be annotated with @UserTrace to add more information to the current action.
    Everything works well except that I just can't get the annotation information from the method. Within the Aspectmethod no annotation is present.
    Simple reflections tests without using aspects proofed, that there are annotations and that its working.
    So the question is - why are no annotations present within an aspect?

    My Web-Application uses: Spring 2.5.6 and Spring WebFlow 2.0.8

    Aspect WorkTrace
    Code:
    @Aspect
    public class WorkTrace {
    		
    	@Around("execution(* de.xxx.web.backingbeans.*.*(..))")
    	public Object logCurrentWork(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
    		
    		final Logger log = Logger.getLogger(proceedingJoinPoint.getTarget().getClass());
    		Signature sig = proceedingJoinPoint.getSignature();
    				
    		String infoStep = "";
    		String infoDesc = "";
    		
    		if(sig instanceof MethodSignature){
    			MethodSignature mSig = (MethodSignature)sig;
    			Method method = mSig.getMethod();	
    /* following doesnt work - says there is no annotation */		
    			if(method.isAnnotationPresent(UserTrace.class)){
    				UserTrace ut = method.getAnnotation(UserTrace.class);
    				infoStep = ut.stepName();
    				infoDesc = ut.stepDescription();
    			}
    		}
    	    final String methodName = sig.getName();
    
    		log.info("WORKTRACE:: "+methodName+" STEP: "+infoStep+" INFO: "+infoDesc);
    	    
    		Object returnValue = proceedingJoinPoint.proceed();
    		return returnValue;
    		
    	}	
    }
    Annotation UserTrace
    Code:
    @Target(value=ElementType.METHOD)
    @Retention(value=RetentionPolicy.RUNTIME)
    public @interface UserTrace {
    
    	public String stepName();
    	public String stepDescription();
    	
    }
    matching codepart
    Code:
    @Secured(value={"ROLE_ADMIN"})
    	@UserTrace(stepName="Speicher oder Updaten eines Hauptbereichs",stepDescription="n.a.")
    	public void saveOrUpdateBackEndArea(SoBackEndArea bea){
    /* ...do some things ... */
    }
    works without aspect
    Code:
    Method mme = me.getMethod("saveOrUpdateBackEndArea", SoBackEndArea.class);
                                   if(mme.isAnnotationPresent(UserTrace.class)){
    					UserTrace rA = mme.getAnnotation(UserTrace.class);
    					logger.info("--> "+rA.stepName());
    				}
    			Annotation[] allAnnotations = mme.getAnnotations();
    			for (int i = 0; i < allAnnotations.length; i++) {
    				Annotation a = allAnnotations[i];
    				Class aClass = a.annotationType();
    				logger.info("x -Annotation: "+a.getClass().getName()+" -> "+aClass.getName());								
    			}

    Has anyone an idea why it's not possible to access annotations within the aspectcode ?!?

    Greetz - Jan

  2. #2
    Join Date
    Nov 2007
    Posts
    420

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    You are also having an @Secured, which probably means you are proxying a proxy class and the proxy doesn't have the annotations. Spring uses a proxy based aop approach which in general can be enough but when using multiple proxy mechanism/proxy generators together it might become troublesome.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  4. #4
    Join Date
    Aug 2005
    Location
    Germany, Frankfurt am Main
    Posts
    22

    Arrow

    thanks both of you!

    I read about annotation inheritance and in fact that spring uses proxies and therefore might extend the annotation, I tried to get it working with @Inherited
    ...unfortunately this didn't change anything

    I also removed the @Secured annotation to be sure its has nothing in common with that one, but again no change in behaviour ...still no annotations found.

    So if spring proxies are the cause of this problem it could be useful to switch to aspectJ Loadtimewaeving instead of using proxy based spring aop.
    ...unfortunately I didn't had luck in configuration change, so it didn't work for now ...but I will try again tomorrow.

    however, do you think there is a solution allowing to stay with proxy based spring aop?

Tags for this Thread

Posting Permissions

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