Announcement Announcement Module
Collapse
No announcement yet.
Using pertarget instantiation model for around advice aspect Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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;

    @Component("QueryController")
    @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;

    @Aspect("pertarget(auditLoggingPointcut)")
    public class AuditLogger {

    public Object writeAudit(ProceedingJoinPoint joinPoint){
    // Log audit message
    ....
    joinPoint.proceed();
    ....
    // 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

    <context:annotation-config/>
    <aop:aspectj-autoproxy/>

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


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


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

    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.

    Comment

    Working...
    X