Announcement Announcement Module
Collapse
No announcement yet.
howto aspect methods with optional parameters Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • howto aspect methods with optional parameters

    How do I write the aspect code that will work on a method with a optional parameter?

    The code below works if MyService.getFacilities() has either no parameters or one database parameter. But if the parameter is database=null, then the aspect code is not called.

    I am new to aop, so any comments on the code below is helpful.

    Code:
    ////////////////////////////////////////////////////////////////////////////////
    // <myapp>\grails-app\service\offload\MyService.groovy
    
    package offload
    
    class MyService {
    	@Auditable
    	def getFacilities(database=null) {
    		// query database & return list
    	}
    }
    
    ///////////////////////////////////////////////////////////////////////////////
    // <myapp>\grails-app\conf\spring\resources.groovy
    
    import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator
     
    // Place your Spring DSL code here
    beans = {
    
        autoProxyCreator(AnnotationAwareAspectJAutoProxyCreator) {
            proxyTargetClass = true
        }
    
    	auditableAspect(offload.AuditableAspect)
    
    }
    
    ///////////////////////////////////////////////////////////////////////////////
    // <myapp>\grails-app\utils\offload\Auditable.groovy
    
    package offload
    
    import java.lang.annotation.ElementType
    import java.lang.annotation.Target
    import java.lang.annotation.RetentionPolicy
    import java.lang.annotation.Retention
    
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    public @interface Auditable {
    }
    
    
    ///////////////////////////////////////////////////////////////////////////////
    // <myapp>\grails-app\utils\offload\AuditableAspect.groovy
    
    package offload
    
    import org.aspectj.lang.annotation.Aspect
    import org.aspectj.lang.annotation.Around
    import org.aspectj.lang.annotation.Pointcut
    import org.aspectj.lang.ProceedingJoinPoint
    
    @Aspect
    class AuditableAspect {
    
    	@Pointcut("@annotation(offload.Auditable)")
    	public void auditableOperation() {
    	}
        
    	@Around("offload.AuditableAspect.auditableOperation()")
    	public Object add(ProceedingJoinPoint jp) throws Throwable {
        	
    		// time the method
    		def start = System.currentTimeMillis()
    		Object retVal = jp.proceed()
    		def stop = System.currentTimeMillis()
    
    		println "elapsed: ${stop-start}ms"
    
    		retVal
    	}
    
    }
    
    ///////////////////////////////////////////////////////////////////////////////
    Thanks,
    Paul Woods
Working...
X