10

Am learning AOP concepts in Spring. I am now pretty aware of the usages of @Before and @After Annotations and started using it for Time capturing purpose.

This is pretty much satisfying all my AOP related needs. Wondering what is that @pointcut annotation that every spring guide talks about ? Is that a redundant functionality ? or does it has separate needs ?

1 Answer 1

20

In simple words whatever you specify inside @Before or @After is a pointcut expression. This can be extracted out into a separate method using @Pointcut annotation for better understanding, modularity and better control. For example

 @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)") public void requestMapping() {} @Pointcut("within(blah.blah.controller.*) || within(blah.blah.aspect.*)") public void myController() {} @Around("requestMapping() && myController()") public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable { ............... } 

As you can see instead of specifying the pointcut expressions inside @Around, you can separate it to two methods using @Pointcut.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. So it means that I can use Pointcut expressions for a method and I need to use those methods in any one of the annotations like Before, After& Around. Am i right ?
yup. Think of it as a function declaration. You take some piece of code and put it into a function. This makes your business logic code more succint and also it helps you in reusing this piece of code somewhere else instead of duplicating
Hi can you tell me the purpose of ProceedingJoinPoint joinPoint here? how it will be passed?
lets say you have a function which takes an argument and you want to put an aspect to it. then the arguments passed to that function can be obtained in your aspect using ProceedingJointPoint. And at the end of aspect you have to say joinPoint.proceed() to forward the call to the function.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.