我有以下方法:

 public ClassifyCamel(Camel camel) {

    if (camel.isHavingSingleHump()) {
       return SINGLE_HUMP_CAMEL;
    }
 }


是否可以创建一个替换整个方法逻辑的方面?例如:

 @Aspect
 .
 .

 @Before("ClassifyCamel") {

   if (camel.isRich) {
     return RICH_CAMEL;
   }
 }


最好。

最佳答案

您可以使用@Around建议,以便在方法执行之前和之后执行一些日志。

在这里您可以查看示例:

@Aspect
public class LoggingAspect {

   @Around("execution(* com.yourPackage.YourClass.yourMethod(..))")
   public void logAround(ProceedingJoinPoint joinPoint) throws Throwable {

    System.out.println("Around before is running!");

    /*
     *  IF YOU DON'T DO THIS YOU CAN SKIP YOUR METHOD EXECUTION.
     * You can do whatever you need
     */
    joinPoint.proceed(); //execute the method.

    System.out.println("Around after is running!");

   }

}


希望对您有所帮助

10-08 00:40