Announcement Announcement Module
No announcement yet.
Using pertarget instantiation model for around advice aspect Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using pertarget instantiation model for around advice aspect

    I have a Query class defined as follows:

    package abc;

    @Scope ("Session")
    public class QueryController extends BaseController {

    public String runQuery(){


    Invocations of the runQuery() method need to be audited using an Aspect class that defines a writeAudit() method which outputs the audit message both before and after the runQuery() invocation. In essence, this aspect implements an Around Advice for the runQuery() method as determined by the auditLoggingPointcut)

    package xyz;

    public class AuditLogger {

    public Object writeAudit(ProceedingJoinPoint joinPoint){
    // Log audit message
    // Log audit message

    Note the pertarget() declaration since I want an instance of the aspect per target object matched by the auditlogPointcut (i.e. for every instance of QueryController I need a corresponding instance of the aspect)

    The Spring configuration defining the pointcut is as follows


    <!-- Scans for application @Components to deploy -->
    <context:component-scan base-package="abc.*, xyz.*" />

    <bean id="auditLogger" class="abc.AuditLogger" scope="prototype"/>

    <aopointcut id="auditLoggingPointcut"
    expression="execution(* pax.lrds.web.controller.QueryController.runQuery(. .))"/>
    <aop:aspect id="auditAspect" ref="auditLogger">
    <aop:around pointcut-ref="auditLoggingPointcut" method="writeAudit"/>

    The problem is that unless I specify scope="prototype" for the auditLogger this does not work. Also is there a way to specify the aspect instantiation using perTarget() in the Spring configuration XML <aop:config> block instead of the code.

  • #2
    Please use [ code][/code ] tags when posting code that way it remains readable ...

    But with spring you cannot do this it only allows for a subset of functionality. If you want to use this use the fullblown aspectJ solution with either loadtime or compiletimeweaving.