1

I am working with Spring Framework and AspectJ version 1.8.9

I have many Service classes, lets consider for example

  • CustomerServiceImpl
  • InvoiceServiceImpl
  • CarServiceImpl

The point is, each one has a saveOne method. Therefore

  • saveOne(Customer customer)
  • saveOne(Invoice invoice)
  • saveOne(Car car)

If I use the following:

@Pointcut(value="execution(* com.manuel.jordan.service.impl.*ServiceImpl.saveOne(Car, ..)) && args(entity) && target(object)")
public void saveOnePointcut(Car entity, Object object){}

@Before(value="ServicePointcut.saveOnePointcut(entity, object)")
public void beforeAdviceSaveOne(Car entity, Object object){

It works. Until here for a better understanding:

  • The first parameter represents the Entity (Car in this case) to persist
  • The second parameter represents the Object or target (XServiceImpl) has been 'intercepted'

Note: I need the first parameter for audit and logging purposes. Therefore it is mandatory.

To avoid do verbose and create more for each entity, I want use a 'superclass' type. I've tried with

@Pointcut(value="execution(* com.manuel.jordan.service.impl.*ServiceImpl.saveOne(Object, ..)) && args(entity) && target(object)")
public void saveOnePointcut(Object entity, Object object){}

@Before(value="ServicePointcut.saveOnePointcut(entity, object)")
public void beforeAdviceSaveOne(Object entity, Object object){

Observe the first parameter now is an Object And it does not work.

If is possible accomplish this requirement, what is the correct syntax?

I've read Clarification around Spring-AOP pointcuts and inheritance but it is about only for methods without parameter(s)

Community
  • 1
  • 1
Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158

1 Answers1

3

You can try to use Object+ instead of just Object. + means all classes that extend target class. So your aspect code will look like this:

@Pointcut(value="execution(* com.manuel.jordan.service.impl.*ServiceImpl.saveOne(Object+, ..)) && args(entity) && target(object)")
public void saveOnePointcut(Object entity, Object object){}

@Before(value="ServicePointcut.saveOnePointcut(entity, object)")
public void beforeAdviceSaveOne(Object entity, Object object){

I tried this with my code samples and it's work fine for all type of arguments.

Sergey Bespalov
  • 1,746
  • 1
  • 12
  • 29
  • Thank you!, it works fine. Normally I have seen that `+` in the Interface/Class declaration within the Pointcut, never in the parameter. – Manuel Jordan Sep 21 '16 at 12:34
  • Why not just use `*` instead of `Object+`? It is basically equivalent, less verbose and easier to understand. The `MyClass+` syntax should only be used if you have a specific base class other than `Object` in mind. – kriegaex Sep 25 '16 at 08:40
  • @kriegaex you are right `*` is better then `Object+`, but i make some research and can say that all of this makes no sense for AspectJ in case of `args(entity)` and seems that the best way is `execution(* com.manuel.jordan.service.impl.*ServiceImpl.saveOne(..)) && args(entity) && target(object)` – Sergey Bespalov Sep 29 '16 at 04:05
  • I do not understand your comment. Why does it make no sense? It works as I suggested. What kind of research told you otherwise? – kriegaex Sep 29 '16 at 12:23
  • Sorry if i my comment confused you. I just wanted to say that ‘execution(* com.manuel.jordan.service.impl.*ServiceImpl.saveOne(Object+, ..)) && args(entity)‘ and ‘execution(* com.manuel.jordan.service.impl.*ServiceImpl.saveOne(..)) && args(entity)‘ produce same result, so first variant is overabundant – Sergey Bespalov Sep 29 '16 at 13:40
  • I respect both opinions, but for `AOP` I don't like use a lot wildcards. I keep the idea that when times passes and I return to some `@Pointcut` I want re understand the idea very quickly. Therefore for this scenario I prefer the idea about use `saveOne(Object+, ..))` than `saveOne(..))` or `saveOne(*))` – Manuel Jordan Sep 29 '16 at 14:48
  • @SergeyBespalov sorry to bother you, perhaps you can help me here? http://stackoverflow.com/questions/39821938/spring-aop-how-exclude-an-unnecessary-pointcut-around-advice-execution-due thanks – Manuel Jordan Oct 02 '16 at 21:38
  • Hi, @ManuelJordan. I think that i can help but need to check the answer first. I can do it tomorrow. – Sergey Bespalov Oct 03 '16 at 14:00
  • Hi @SergeyBespalov don't worry, seems I have a possible solution. Testing right now. Thanks a lot again by your support. Best Regards! – Manuel Jordan Oct 03 '16 at 14:01