簡體   English   中英

Spring AOP建議未執行

[英]Spring AOP advice not getting executed

我正在嘗試執行建議,但它不起作用。 我正在嘗試在沒有應用程序上下文的情況下進行操作。

這是我的休息控制器:

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
//@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. 執行切入點的格式

      execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-pattern(param-pattern) throws-pattern?) 
  2. 您的控制器方法帶有參數,但它不在定義中

嘗試:

 @Before("* execution(hello.GreetingController.greeting(..)")
  1. 您需要在完成@Before建議之后繼續

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM