Hi,

I am trying to add advice around an annotation. I want this advice to be applied to methods which are not directly called by the bean. Since Spring AOP cannot achieve this, I am using AspectJ with LTW. When I run this code the advice gets executed twice. Any ideas?

Below is the code:

This is the annotation class:
Code:
package com.test;

import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface UpdatePrint {
	String fields();
}
This is the aspect which executes around the annotation
Code:
package com.test;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class UpdatePrintAspect {

	@Around("@annotation(updatePrint)")
	public Object logPrint(ProceedingJoinPoint pjp, UpdatePrint updatePrint) throws Throwable{
		
		System.out.println("#### Start updating fields: " + updatePrint.fields());
		
		Object retVal = pjp.proceed();
		
		System.out.println("#### End Updating fields: " + updatePrint.fields());
		
		
		return retVal;
	}
}
This is the bean class:
Code:
package com.test;

public class MyBean {
		
	public void update() {
		updateTicker();
		updateIssuer();
	}

	@UpdatePrint(fields="Issuer")
	private void updateIssuer() {		
		System.out.println("updateIssuer called");
	}

	@UpdatePrint(fields="Ticker")
	private void updateTicker() {
		System.out.println("updateTicker called");
		
	}

}
This is the driver class:
Code:
package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public final class Main {

    public static void main(String[] args) {

        ApplicationContext ctx = new ClassPathXmlApplicationContext("/beans.xml", Main.class);

        MyBean bean = (MyBean) ctx.getBean("myBean");

        bean.update();
    }
}
This is beans.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
	<!-- this switches on the load-time weaving -->
    <context:load-time-weaver/>
	   
    
    <bean id="myBean"
          class="com.test.MyBean"/>

   
</beans>
This is META-INF/aop.xml:
Code:
<aspectj>
    
    <aspects>        
        <aspect name="com.test.UpdatePrintAspect"/>
    </aspects>
	
   
    <weaver options="-verbose">
        <include within="com.test.MyBean"/>
    </weaver>

</aspectj>
I get the following output:

Dec 22, 2009 11:25:37 PM org.springframework.context.support.AbstractApplic ationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlAp plicationContext@1608e05: display name [org.springframework.context.support.ClassPathXmlAp plicationContext@1608e05]; startup date [Tue Dec 22 23:25:37 EST 2009]; root of context hierarchy
Dec 22, 2009 11:25:37 PM org.springframework.beans.factory.xml.XmlBeanDefin itionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [/beans.xml]
Dec 22, 2009 11:25:37 PM org.springframework.context.support.AbstractApplic ationContext obtainFreshBeanFactory
INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlAp plicationContext@1608e05]: org.springframework.beans.factory.support.DefaultL istableBeanFactory@12f0999
Dec 22, 2009 11:25:37 PM org.springframework.context.weaving.DefaultContext LoadTimeWeaver setBeanClassLoader
INFO: Found Spring's JVM agent for instrumentation
[AppClassLoader@130c19b] info AspectJ Weaver Version 1.6.5 built on Thursday Jun 18, 2009 at 03:42:32 GMT
[AppClassLoader@130c19b] info register classloader sun.misc.Launcher$AppClassLoader@130c19b
[AppClassLoader@130c19b] info using configuration /F:/Abhijeet/eclipse_workspace/SpringAspectJ1/lib/META-INF/aop.xml
[AppClassLoader@130c19b] info register aspect com.test.UpdatePrintAspect
Dec 22, 2009 11:25:38 PM org.springframework.beans.factory.support.DefaultL istableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultL istableBeanFactory@12f0999: defining beans [org.springframework.context.weaving.AspectJWeaving Enabler#0,loadTimeWeaver,myBean]; root of factory hierarchy
#### Start updating fields: Ticker
#### Start updating fields: Ticker

updateTicker called
#### End Updating fields: Ticker
#### End Updating fields: Ticker
#### Start updating fields: Issuer
#### Start updating fields: Issuer

updateIssuer called
#### End Updating fields: Issuer
#### End Updating fields: Issuer