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

  • AspectJ Pointcut definitions

    Hi, I am having problems with pointcuts definitions using AspectJ.

    This does work:
    @Before("execution(public * *(..))")
    public void printMessage() {
    System.out.println("in printMessage");
    }

    Yet this does not:
    @Pointcut("execution(public * *(..))")
    public void allMethodCalls() { }

    @Before("allMethodCalls()")
    public void printMessage() {
    System.out.println("in printMessage");
    }

    The following exception is thrown when loading Spring:
    java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut allMethodCalls

    Similarly, the following does not work:
    @Pointcut("execution(* doSomeOperation(..))")
    public void businessMethods() { }

    @Around("businessMethods()")
    public Object profile(ProceedingJoinPoint pjp) throws Throwable {
    System.out.println("Going to call the method.");
    Object output = pjp.proceed();
    System.out.println("Method execution completed.");
    return output;
    }

    Any help is very much appreciated

  • #2
    I am pretty sure that is correct. Perhaps something else is amiss?

    Comment


    • #3
      Here is the code for the aspect class:
      @Aspect
      public class LoggingAdvice {

      private static final Log LOG = LogFactory.getLog(LoggingAdvice.class);

      @Pointcut("execution(public * *(..))")
      public void allMethodCalls() { }

      @Before("allMethodCalls()")
      public void printMessage(Object[] args) {
      System.out.println("in printMessage");
      }

      In applicationContext.xml I have:

      <bean class="com.drf.hapoa.aop.LoggingAdvice" />

      <aop:aspectj-autoproxy />

      What else should I add - thanks.

      Comment


      • #4
        Oh. My bad - I missed the args. Try either removing the args from both or adding the argument to the pointcut so that they match. This should work:

        Code:
        @Aspect
        public class LoggingAdvice {
        
        	private static final Log LOG = LogFactory.getLog(LoggingAdvice.class);
        
        	@Pointcut("execution(public * *(..)) && args(args)")
        	public void allMethodCalls(Object[] args) {
        	}
        
        	@Before("allMethodCalls(args)")
        	public void printMessage(Object[] args) {
        		System.out.println("in printMessage");
        	}
        }

        Comment


        • #5
          Thank you very much - but after cut and paste of your example I get this exception:

          Caused by: java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut allMethodCalls
          at org.aspectj.weaver.tools.PointcutParser.parsePoint cutExpression(PointcutParser.java:302)
          at org.springframework.aop.aspectj.AspectJExpressionP ointcut.buildPointcutExpression(AspectJExpressionP ointcut.java:204)
          at org.springframework.aop.aspectj.AspectJExpressionP ointcut.checkReadyToMatch(AspectJExpressionPointcu t.java:191)
          at org.springframework.aop.aspectj.AspectJExpressionP ointcut.getClassFilter(AspectJExpressionPointcut.j ava:172)
          at org.springframework.aop.support.AopUtils.canApply( AopUtils.java:200)
          at org.springframework.aop.support.AopUtils.canApply( AopUtils.java:254)
          at org.springframework.aop.support.AopUtils.findAdvis orsThatCanApply(AopUtils.java:286)
          at org.springframework.aop.framework.autoproxy.Abstra ctAdvisorAutoProxyCreator.findAdvisorsThatCanApply (AbstractAdvisorAutoProxyCreator.java:117)
          at org.springframework.aop.framework.autoproxy.Abstra ctAdvisorAutoProxyCreator.findEligibleAdvisors(Abs tractAdvisorAutoProxyCreator.java:87)
          at org.springframework.aop.framework.autoproxy.Abstra ctAdvisorAutoProxyCreator.getAdvicesAndAdvisorsFor Bean(AbstractAdvisorAutoProxyCreator.java:68)
          at org.springframework.aop.framework.autoproxy.Abstra ctAutoProxyCreator.wrapIfNecessary(AbstractAutoPro xyCreator.java:359)
          at org.springframework.aop.framework.autoproxy.Abstra ctAutoProxyCreator.postProcessAfterInitialization( AbstractAutoProxyCreator.java:322)
          at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.applyBeanPostProcessors AfterInitialization(AbstractAutowireCapableBeanFac tory.java:407)
          at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.initializeBean(Abstract AutowireCapableBeanFactory.java:1418)
          at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:519)

          Comment


          • #6
            I just noticed that the code you post in your second post was different then the code you posted in your first post. The first post looks like it should work. The second looks like it should not be able to find the pointcut.

            Is it possible the code exists in two places in your project?

            This should also work :

            Code:
            @Aspect
            public class LoggingAdvice {
            
            	private static final Log LOG = LogFactory.getLog(LoggingAdvice.class);
            
            	@Pointcut("execution(public * *(..))")
            	public void allMethodCalls() {
            	}
            
            	@Before("allMethodCalls()")
            	public void printMessage() {
            		System.out.println("in printMessage");
            	}
            }

            Comment

            Working...
            X