Can anyone explain what I need to do to implement my own annotation that would add functionality to my web requests?
For example:
@Controller
public class MyController {
@RequestMapping("/abc")
@RequiresSomeSpecialHandling
public void handleSecureRequest() {
}
}
Here @RequiresSomeSpecialHandling
would be my own annotation that causes some special work to be done before or after the given web request /abc
.
I know that on a very high level I would need to write a bean post processor, scan classes for my annotations, and inject custom mvc interceptors when needed. But are there any shortcuts to simplify this task? Especially for the two examples above.
Thanks in advance,
This kind of Annotations, (that add additional functionality when invoking a method) looks like annotations that trigger an AOP Advice.
@see Spring Reference Chapter 7. Aspect Oriented Programming with Spring
The idea is to use the Annotation to trigger the AOP Advice.
like:
@Pointcut("@target(com.example.RequiresAuth)")
Depends on what you want to do as a result of @RequiresSomeSpecialHandling. E.g. do you want it to influence request mappings or the invocation of the method (i.e. resolving method arguments, processing the return value)?
The support for annotated classes in Spring 3.1 became much more customizable. You can browse some examples in this repo.
Also keep in mind that a HandlerInterceptor in Spring 3.1 can cast the handler Object to HandlerMethod, which gives you access to the exact method including its annotations. That may be enough for what you need to do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With