views:

62

answers:

3

I am just getting started with Spring AOP in my project and am having some problems with getting Spring AOP working correctly.

I have two objects, TransportImpl and SesssionImpl that I would like to profile via AOP. Both objects(beans) are initialised via Spring. Both beans are implementations of business interfaces (Transport and Session). I can get Aspects applied to the TransportImpl bean to work well, but those applied to the SessionImpl just do not fire. I can confirm that the "mySessionMonitor" Aspect is initialised by Spring, and that the SessionImpl object is also initialised without any exceptions or errors.

I have stripped down my PointCuts and Aspect to the most basic form possible. I would have expected the PointCut sessionOperation described below to fire when the SessionImpl bean is initialised and the init-method initialise is called. But this never happens. What might be going wrong here?

From the configuration file:

<bean id="MyTransport" class="my.app.transport.TransportImpl"   scope="singleton" />                
<bean id="MySession" class="my.app.session.SessionImpl" init-method="initialise" scope="singleton" />

<aop:aspectj-autoproxy proxy-target-class="true">
    <aop:include name="myTransportMonitor" />
    <aop:include name="mySessionMonitor" />
</aop:aspectj-autoproxy>

<bean id="myTransportMonitor" class="my.app.aspects.TransportMonitoringAspect"/>
<bean id="mySessionMonitor"   class="my.app.aspects.SessionMonitoringAspect" />

Aspect code

// Aspect monitoring code
@Aspect
public class SessionMonitoringAspect
{
    private Logger fileLogger = Logger.getLogger("myLogger");

    public void initialise()
    {
        fileLogger.info("Initialising SessionMonitoringAspect");
    }

    @Pointcut ("execution (public * *(..))")
    private void anyPublicOperation(){}

    @Pointcut ("within(my.app.session..*)")
    private void inSession(){}

    @Pointcut("anyPublicOperation() && inSession()")
    private void sessionOperation(){}

    @Before("sessionOperation()")
    public void sessionOperationDetected(JoinPoint jp)
    {
        fileLogger.info("Session operation detected - signature: " + jp.getSignature());
    }
}
A: 

I hope "initialise" method is not part of "my.app.session.SessionImpl". If so "initialise" method of your Aspect will not fire. Because you cannot call aspect's method from bean class. Otherwise please attach "my.app.session.SessionImpl" code for detail understanding.

Maheshkumar
+1  A: 

I personally prefer to put the Aspect pointcut configuration in the application context, and you are missing the ref in aop:aspect.

 <bean id="mySessionMonitor"   class="my.app.aspects.SessionMonitoringAspect" />

 <aop:config proxy-target-class="true">
  <aop:aspect ref="mySessionMonitor">
   <aop:pointcut id="around" expression="execution (public * *(..))"/></aop:pointcut>
   <aop:around pointcut-ref="around" method="aroundAdvice"/>
  </aop:aspect>
 </aop:config>
Brian
A: 

If you prefer annotation-based configuration, see this example I wrote. All you'll need in your XML configuration is <aop:aspectj-autoproxy />.

James Earl Douglas