spring 3.0 aop 切入点格式不正确:期望“名称模式”;错误

发布于 2024-09-25 22:13:41 字数 759 浏览 3 评论 0原文

以下是我的切入点和建议声明

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

}

我收到以下错误

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 (..))
                                                                 ^

我卡在这个,任何指针

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浮光之海 2024-10-02 22:13:41

您缺少返回类型:

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

并且必须绑定参数名称,如下所示:

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

示例解决方案

服务接口:

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

实现类:

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方面:< /strong>

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

测试主类:

现在这是一个测试整个过程的主要方法。它启动一个 Spring ApplicationContext 没有 XML 配置,上面的 XML 定义了服务 bean 和切面(事实证明,没有 XML 的解决方案只能工作,因为我打开了 AspectJ 编织,我不知道我必须包含哪些bean 才能启用aspectj-autoproxy,因此我现在使用ClassPathXmlApplicationContext 和这个最小的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

这应该可以帮助您开始。基本上:如果您使用 JDK 代理(spring 默认),您需要检查您正在拦截的方法是否由服务接口支持。阅读此处有关 Spring AOP代理机制

注意:

如您所见,我将方法参数绑定到方面,而不是切入点,因此使切入点可重用于具有不同参数签名的方法。但也可以将它们绑定在切入点中,如下所示:

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

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);
}
完美的未来在梦里 2024-10-02 22:13:41

你应该这样写,

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

注意 "... (* backend..."

* 并且应该使用空格。

You should write like this

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

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

* and a space should be used.

如日中天 2024-10-02 22:13:41

请注意,您与 org.aspectj.lang.annotation.Before 中的 @Before 注释有类似的行为。

您可以使用不带 execution 关键字且不带返回类型的表达式:

@Before("allGetters()")

或同时使用两者:

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

但不能在不使用返回类型的情况下使用 execution 关键字。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文