Spring AOP建议没有被执行

时间:2015-12-17 19:25:13

标签: java maven spring-mvc spring-aop

我试图获得建议,而且它不起作用。我试图在没有应用程序上下文的情况下这样做。

这是我的Rest Controller:

package hello;

import java.util.concurrent.atomic.AtomicLong;

import aspect.exception.GreetingsNotFoundException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @RequestMapping("/greeting")
     public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
        return new Greeting(counter.incrementAndGet(),
                String.format(template, name));
    }
}

Aspect类:

    @Aspect
//@ComponentScan
public class AfterThrowingException implements ThrowsAdvice {

    // Obtain a suitable logger.
    private static Logger logger = LoggerFactory.getLogger(AfterThrowingException.class);

      @Before("execution(hello.GreetingController.greeting()")
    public void logBefore(JoinPoint joinPoint){
    System.out.println("Inside AfterThrowingException.logBefore()");
}

配置类:

    @Configuration
@EnableAspectJAutoProxy
public class AppConfig {
    @Bean
    public AfterThrowingException afterThrowingException() {
        return new AfterThrowingException();
    }
}    

1 个答案:

答案 0 :(得分:0)

  1. execution pointcut is的格式:

      execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-pattern(param-pattern)
            throws-pattern?)
    
  2. 您的控制器方法需要参数,但它不在定义

  3. 尝试:

     @Before("* execution(hello.GreetingController.greeting(..)")
    
    1. 您需要在完成@Before建议后继续

        jointPoint.proceed();
      
    2. 您的姓名不同步