2

I am familiar with Spring AOP, although not having much hands on experience on that.

My question is, if I want to have some AOP functionality for some methods of a class, not all, then can it be possible with single pointcut. Say, I have four methods save1,save2,get1 and get2 in my class and I want to apply AOP on save1 and save2 only, then in that case how can I create a single pointcut for that? How will my pointcut expression looks like? OR Is it even possible?

4 Answers 4

2

There are many ways to do it(with wildcard expression, with aspectJ annotation, ..) i will give an example with aspectJ

class MyClass{ @MyPoint public void save1(){ } @MyPoint public void save2(){ } public void save3(){ } public void save4(){ } } @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MyPoint { } @Aspect @Component public class MyAspect { @Before("@annotation(com.xyz.MyPoint)") public void before(JoinPoint joinPoint) throws Throwable { //do here what u want } } 

So you are all set, as long as you marked @Mypoint annotation, spring will call before aspect for this method, make sure spring is managing this method and object, not you. include aspectJ in your classpath

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

Comments

1

You need to specify a pointcut expression to select the methods with to apply your advice.

See 7.2.3 Declaring a pointcut in the Spring Documentation and use the execution joinpoint designator to select the methods.

Comments

1

Having a pointcut expression like so should do the trick

**execution(* save*(..))** 

See here for more information

Comments

0

You can use or and and with pointcut expressions:

execution(* my.Class.myMethod(..)) or execution(* my.Class.myOtherMethod(..)) 

1 Comment

Actally or didn't work, had to use || instead execution(* my.Class.myMethod(..)) || execution(* my.Class.myOtherMethod(..))

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.