spring 3.0 aop Pointcut is not well-formed: expecting 'name pattern' error

33,496

Solution 1

You are missing the return type:

@Pointcut("execution(* backend.repository.QuestionsRepository.AnswerQuestion (..))")

and you have to bind the argument names, something like this:

@Pointcut("execution(* backend.repository.QuestionsRepository.AnswerQuestion (..)) 
&& args(question, answer)") // wrapped for readability only

Example Solution

Service Interface:

package foo.bar.service;
public interface Service{
    void whack(String thing, Integer thang);
}

Implementation class:

package foo.bar.service;
public class DefaultService implements Service{
    @Override
    public void whack(final String thing, final Integer thang){
        System.out.println(
            "Inside thing: " + thing + ", inside thang: " + thang
        );
    }
}

Spring AOP aspect:

@Aspect
public class ServiceAspect{

    @Pointcut("execution(* foo.bar.service.*.*(..))")
    public void serviceMethodExecution(){
    }

    @Around(value = "serviceMethodExecution() && args(param1, param2)")
    public void aroundServiceMethodExecution(final ProceedingJoinPoint pjp,
        final String param1,
        final Integer param2) throws Throwable{

        System.out.println("Before thing: " + param1 + ", before thang: "
            + param2);
        pjp.proceed();
        System.out.println("After thing: " + param1 + ", after thang: "
            + param2);
    }

}

Spring Context XML:

<aop:aspectj-autoproxy proxy-target-class="false"  />
<bean class="foo.bar.service.DefaultService" />
<bean class="foo.bar.aspects.ServiceAspect" />

Main class for testing:

Now here's a main method to test the whole process. It starts a Spring ApplicationContext without XML configuration with the above XML defining the service bean and the aspect (it turns out that the solution without XML only worked because I had AspectJ weaving turned on, I don't know what beans I have to include to enable aspectj-autoproxy, so I now use ClassPathXmlApplicationContext with this minimal XML):

public static void main(final String[] args){
    final ApplicationContext applicationContext =
        new ClassPathXmlApplicationContext("classpath:/aspectContext.xml");
    final Service service = applicationContext.getBean(Service.class);
    service.whack("abc", 123);
}

Output:

Before thing: abc, before thang: 123
Inside thing: abc, inside thang: 123
After thing: abc, after thang: 123

This should get you started. Basically: you need to check that the methods you are intercepting are backed by a service interface if you use JDK proxies (spring default). Read here about Spring AOP proxy mechanisms.

Note:

As you see I bind the method arguments to the aspect, not the pointcut, hence making the pointcut reusable for methods with different argument signatures. But it would also be possible to bind them in the pointcut, like this:

@Pointcut(value = "execution(* foo.bar.service.*.*(..)) && args(a,b)",
          argNames = "a,b")
public void serviceMethodExecution(final String a, final Integer b){
}

@Around(value = "serviceMethodExecution(param1, param2)",
        argNames = "param1,param2")
public void aroundServiceMethodExecution(final String param1,
    final Integer param2,
    final ProceedingJoinPoint pjp) throws Throwable{

    System.out.println("Before thing: " + param1 + ", before thang: "
        + param2);
    pjp.proceed();
    System.out.println("After thing: " + param1 + ", after thang: "
        + param2);
}

Solution 2

You should write like this

@Pointcut("execution(* backend.repository.QuestionsRepository.AnswerQuestion (..))")

pay attention to "... (* backend..."

* and a space should be used.

Solution 3

Note that you have similar behaviour with the @Before annotation in org.aspectj.lang.annotation.Before.

You can use the expression without execution key word and without a return type:

@Before("allGetters()")

or with both:

@Before("execution(public * allGetters())")

but you can not use the execution keyword without using the return type.

Share:
33,496
user463008
Author by

user463008

Updated on June 07, 2020

Comments

  • user463008
    user463008 almost 4 years

    The following is my pointcut and advise declaration

    //PointCut on A method which takes two parameters and is in a DAO
    @Pointcut("execution(backend.repository.QuestionsRepository.AnswerQuestion (..))")
    public void answerQuestionPointCut() {}
    
    
    @Around(
       value="web.activity.advisors.UserActivityAdvisor.answerQuestionPointCut()",
       argNames="question,answer"
    )
    
     // Do something
    
    }
    

    I get the following error

    Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Pointcut is not well-formed: expecting 'name pattern' at character position 65
    execution(backend.repository.QuestionsRepository.AnswerQuestion (..))
                                                                     ^
    

    Am stuck on this, Any pointers

  • user463008
    user463008 over 13 years
    Thnaks, I guess a) means this @Pointcut("execution(*backend.repository.QuestionsRepository‌​.AnswerQuestion (..)) && args(question, answer)") public void answerQuestionPointCut(Question question,Answer answer) {}
  • user463008
    user463008 over 13 years
    I could not figure out what b means, need some more directions
  • user463008
    user463008 over 13 years
    Pointcut is not well-formed: expecting 'name pattern' at character position 66 execution(*backend.repository.QuestionsRepository.AnswerQues‌​tion (..)) && args(question, answer)
  • user463008
    user463008 over 13 years
    Thanks a ton .. got it working, was missing the return type and the other points you mentioned. the spring documentation is really confusing
  • Sean Patrick Floyd
    Sean Patrick Floyd over 13 years
    Thanks a ton .. got it working. You're welcome. But the best way of saying thank you here is accepting the answer.
  • Avik
    Avik over 11 years
    Please make an effort to read previous answers before posting.
  • Program-Me-Rev
    Program-Me-Rev over 9 years
    Hi @Sean Patrick Floyd. I have a problem similar to this one here: Advise method error in Spring AOP. I'm stuck and was wondering whether you would mind taking a look and helping me out if you can? Thank you. I would really appreciate it.
  • Sean Patrick Floyd
    Sean Patrick Floyd over 9 years
    @user3663765 ask a separate question or contact me (see my profile page)
  • Program-Me-Rev
    Program-Me-Rev over 9 years
    I published a separate question Advise method error in Spring AOP
  • Bandham Manikanta
    Bandham Manikanta about 6 years
    @SeanPatrickFloyd, I had same issue & code, and reading your answer, i passed arguments. before: @Pointcut("execution(* com.mani.*.*.*(..))) public void general() { } After: @Pointcut(value = "execution(* com.mani.*.*.*(..)) && args(a,b)", argNames = "a,b") public void generalPointcut(final String a, final String b) { } On running wth updtd code, i'm getting exception