AOP Spring Wildcard不起作用

时间:2014-08-04 14:52:43

标签: java spring spring-mvc aop spring-aop

我正在尝试使用本教程学习AOP:

Spring Tutorial 28 - Pointcuts and Wildcard Expressions

我没有使用XML而是使用注释。

当我使用通配符而不是直接单词时会出现问题。

这段代码效果很好:

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LoggingAspect {

    @Before("execution(public String  get*())")
    public void loggingAdvice(){
        System.out.println("Advice run. Get Method Called");
    }

此代码不起作用:

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LoggingAspect {

    @Before("execution(* *  get*())")
    public void loggingAdvice(){
        System.out.println("Advice run. Get Method Called");
    }

这是主要的:

@Component
public class AopMain {
    private final static String xmlPath = "file:src/main/webapp/WEB-INF/spring/context.xml";

    public static void main(String[] args) throws IOException {
        ApplicationContext ctx = new ClassPathXmlApplicationContext(xmlPath);
        ShapeService shapeService = ctx.getBean("shapeService", ShapeService.class);
        System.out.println(shapeService.getCircle().getName());}
}

我得到的错误是:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.handler.MappedInterceptor#0': Cannot create inner bean '(inner bean)' of type [org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)': Cannot resolve reference to bean 'org.springframework.format.support.FormattingConversionServiceFactoryBean#0' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.format.support.FormattingConversionServiceFactoryBean#0': Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Pointcut is not well-formed: expecting '(' at character position 15
execution(* *  get*())
               ^^^

有谁知道为什么它有效?

3 个答案:

答案 0 :(得分:0)

试试这个:

@Before("execution(*  get*())")
public void loggingAdvice(){
    System.out.println("Advice run. Get Method Called");
}

太多的星号

答案 1 :(得分:0)

应该是

@Before("execution(*  get*())")

答案 2 :(得分:0)

参考下面的解释。它将为您提供有关AOP中通配符的更好的想法: 您必须在get(*)或get(..)

中使用*或..

执行AccountService接口定义的任何方法: 执行(* com.xyz.service.AccountService。*(..))

执行服务包中定义的任何方法: 执行(* com.xyz.service。(..))

执行服务包或子包中定义的任何方法: 执行(* com.xyz.service .. (..))