开发者

UnsupportedPointcutPrimitiveException on simple AOP example

开发者 https://www.devze.com 2023-03-16 06:16 出处:网络
I try to run a simple aop example in this site. I have spring aop and aspectj, aspectjweaver jars: @Aspect

I try to run a simple aop example in this site. I have spring aop and aspectj, aspectjweaver jars:

@Aspect
public class StringAspect {

    @Pointcut("call(* String.toLowerCase())")
    public void toLowerCasePointcut() {}

    @Around("toLowerCasePointcut()")
    public String toLowerCaseAroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
        String text = ((String) joinPoint.getTarget()).toUpperCase();
        return text;
    }
}

When I run this example in Test.java like "AaBbCc".toLowerCase(), I get this exception;

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean ... Initialization of bean failed; nested exception is org.aspectj.weaver.tools.UnsupportedPointcutPrimitiveException: Pointc开发者_开发技巧ut expression call(* String.toLowerCase()) contains unsupported pointcut primitive 'call'

Spring AOP doesnt contain "call", but why aspectj weaving is not working, ,do you have an idea? Thank you.

edit:

In my spring config file I only have bean definition of @aspect annotated class and <aop:aspectj-autoproxy />. my jars are : spring-aop-3.0.5, aopalliance, aspectjrt1.6.8, aspectjweaver1.5.0


Have you tried to use the AspectJ Eclipse plugin to do the weaving? (It is also included in SpringSource Tool Suite)

If you have some aspect configuration in your Spring configuration. Try to remove it and just enable AspectJ nature on the project. Also remove all AspectJ jar files and only use those that is attached automatically by the plugin.

With this setup it works for me at least.

Updated: Weaving the aspect advice into code

You get an exception from the Spring container because of your call pointcut. But you want AspectJ weavingweave the aspect. Then you need to use either compile-time or load-time weaving. Compile-time weaving is the simplest alternative ant the alternative offered by the plugin.

You can look at the AspectJ compiler as an advanced Java compiler that also supports AspectJ. So you can run your compiled code anywhere.

Also, you do not need the plugin to compile. You can for example compile with an Ant task as I have showed here.

But the easiest alternative is to use the plugin. This also gives you extra help which I have described briefly here.

I hope this helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号