user463008 user463008 - 1 year ago 64
Java Question

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

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() {}


// 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

Answer Source

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:

public interface Service{
    void whack(String thing, Integer thang);

Implementation class:

public class DefaultService implements Service{
    public void whack(final String thing, final Integer thang){
            "Inside thing: " + thing + ", inside thang: " + thang

Spring AOP aspect:

public class ServiceAspect{

    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);
        System.out.println("After thing: " + param1 + ", after thang: "
            + param2);


Spring Context XML:

<aop:aspectj-autoproxy proxy-target-class="false"  />
<bean class="" />
<bean class="" />

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);


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.


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(**.*(..)) && 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);
    System.out.println("After thing: " + param1 + ", after thang: "
        + param2);