1

I am wondering, why my point cuts ins Spring AOP do only work, if I specify my beans containing the join points explicitly in the application context XML.

Normally in my project, all Spring beans are defined over annotations:

@Service
@Component

configured with

<context:component-scan base-package="my.package.base" scoped-proxy="interfaces" />
<context:annotation-config />

The beans get created and are usable throughout my application but the point cut is not triggered.

When I specify the bean manually in my application context with

<bean class="..." />

The point cut is matched and the according advice is executed.

@Pointcut("execution(* my.package.base..*.update*(..))")
public void updateDataPointcut() {}

AOP is configured in the application context with

<aop:aspectj-autoproxy />

I have also created a pointcut for Spring Data JPA CrudRepository which works fine.

What is the difference? Is there a pitfall in the "component-scan" configuration?

theFriedC
  • 424
  • 7
  • 20
  • Is your aspect also a `@Component`? – kriegaex Jan 30 '17 at 23:25
  • @kriegaex yes - my pointcut class is annotated with "@Component" and "@Aspect" – theFriedC Jan 31 '17 at 07:36
  • Did you also activate Spring AOP via either `@Configuration @EnableAspectJAutoProxy public class AppConfig {}` or ``? – kriegaex Jan 31 '17 at 10:23
  • I use the config in the application context. I updated my question above – theFriedC Jan 31 '17 at 10:37
  • Two things (1) Do you need `scoped-proxy="interfaces"` in `component-scan` or can it be done away (2) Does the advised class implement any interface ? – Bond - Java Bond Feb 07 '17 at 09:58
  • @Bond-JavaBond ad1 -> I am not quite sure. This is legacy-configuration in my project. I can give it a try and remove it. ad2 -> Yes. There is `SpecialImpl implements SpecialInterface` and `SpecialInterface extends BaseInterface`. The Pointcut is set onto the methods of the `BaseInterface` – theFriedC Feb 07 '17 at 14:30
  • 1
    `scoped-proxy` is used when a shorter scope bean (*e.g. request or session*) is injected to wider scope bean(s) viz singleton. Check if there are such beans – Bond - Java Bond Feb 07 '17 at 16:23
  • Since the advised bean is implementing interface then JDK proxy is being created as `aop:aspectj-autoproxy` is configured default to use JDK proxy. Thus my suspicion of problem due to mixing of CGLIB and JDK proxy won't hold here. – Bond - Java Bond Feb 07 '17 at 16:32
  • @Bond-JavaBond - I have only found wider scoped beans injected into session-scoped beans. The other way around as you described it. So I guess `scoped-proxy="interfaces"` won't be necessary? – theFriedC Feb 09 '17 at 09:32
  • @theFriedC Yes, in this case you can safely remove it – Bond - Java Bond Feb 09 '17 at 09:41

0 Answers0