Giriş
Şu satırı dahil ederiz.
Örnek - @Before + Anotasyon
Şöyle yaparız. @Before + Benim belirlediğim bir anotasyon ile hangi metodlara ne işlem yapılacağı belirtilebilir. Bu örnekte aroundRateLimitationAction() void dönüyor.
Elimizde şöyle bir kod olsun.
Şöyle yaparız.
Şu satırı dahil ederiz.
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;Örnek - @Before + Anotasyon
Şöyle yaparız. @Before + Benim belirlediğim bir anotasyon ile hangi metodlara ne işlem yapılacağı belirtilebilir. Bu örnekte aroundRateLimitationAction() void dönüyor.
@Aspect
public class RateLimitationAspect {
  @Autowired
  RateLimitationValidator rateLimitationValidator;
  public RateLimitationAspect() {
  }
  @Before("@annotation(rateLimitation)")
  public void aroundRateLimitationAction(JoinPoint joinPoint,
                                       RateLimitation rateLimitation) throws Throwable {
    MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();
    Method method = methodSignature.getMethod();
    log.info("Validating rate limitation on method " + method.getName());
    ...
    throw new IllegalStateException("...");
    }
  }
}Elimizde şöyle bir kod olsun.
@RestController
@RequestMapping("/test")
public class TestController {
  @Anonymous
  @GetMapping("")
  public ResponseEntity<String> test(ExampleRequest exampleRequest,
                                     BindingResult bindingResult) {
    if (bindingResult.hasErrors())
      return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    else
      return new ResponseEntity<>(HttpStatus.OK);
    }    
}import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Anonymous {
}  import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;
@Aspect
@Component
public class TestAspect {
  @Before(value = "@annotation(Anonymous) && args(exampleRequest, bindingResult,..)",
   argNames = "joinPoint,exampleRequest,bindingResult")
  public Object logExecutionTime(JoinPoint joinPoint, ExampleRequest exampleRequest,
    BindingResult bindingResult) throws Throwable {
    if (exampleRequest.getName().equals("anonymous"))
      bindingResult.rejectValue("name", "incorrect");
    return joinPoint;
  }
}   Şöyle yaparız.
@Aspect
public class MyIntroducer {
  @DeclareParents(value="SomeAdvisedClass+",
                  defaultImpl=MyIntroducedBeanImpl.class)
  public static MyIntroducedBean myIntroducedBean;
}
Örnek - @Before + @PointCut
Şöyle yaparız. Pointcut ile hangi metodlarda uygulanacağını belirtiyoruz. @Before ile bu metodlara ne işlem yapılacağını belirtiyoruz.
Şöyle yaparız. Pointcut ile hangi metodlarda uygulanacağını belirtiyoruz. @Before ile bu metodlara ne işlem yapılacağını belirtiyoruz.
@Aspect
public class MyAppHeaderAspect {
 @Before("withinServiceApiPointcut() && executionOfRestTemplateExecuteMethodPointcut()")
 public void addHeader(RequestCallback requestCallback){
   ...
 }
 @Pointcut("within (com.my.app.service.NeedsHeaderService)")
 public void withinServiceApiPointcut() {}
 @Pointcut("execution (* org.springframework.web.client.RestTemplate.execute(..)) &&
    args(requestCallback)")
 public void executionOfRestTemplateExecuteMethodPointcut(RequestCallback requestCallback)
 {...}
} 
Hiç yorum yok:
Yorum Gönder