SATO Yusuke
SATO Yusuke

Reputation: 2184

How can I specify method with an parameterized annotation and its value with @Pointcut

Background:

I am developing an web application with Spring MVC.

I want to make an aspect that is executed on POST requests and not executed on GET requests, because I want to inject the logic that prevent POST requests which are sent before completion of HTML rendering.

@RequestMapping(value = "/aaa", method = RequestMethod.POST)
public String methodForPost(AnDto dto, Model model) {
    // the aspect should be executed on this method
}

@RequestMapping(value = "/bbb", method = RequestMethod.GET)
public String methodForGET(AnDto dto, Model model) {
    // the aspect shouldn't be executed on this method
}

Question:

  1. How can I specify method with an parameterized annotation and its value with @Pointcut ?
  2. How can I specify method with an parameterized annotation and its value in <aop:pointcut> in Spring applicationContext.xml?

Upvotes: 0

Views: 53

Answers (1)

pvpkiran
pvpkiran

Reputation: 27048

@Around(value="@annotation(RequestMapping)")
public Object display(ProceedingJoinPoint joinPoint, RequestMapping requestMapping ) throws Throwable {
    // You have access to requestMapping. From which you can get whether its get or post and decide to proceed or not.
}

More info http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html#aop-ataspectj-advice-params-passing

You might have to extend this to intercept only for Requestmapping's in your package. Because this intercepts every RequestMappig you might have in your project including one used by the libraries which you might be using, which is a burden.

Upvotes: 1

Related Questions