Ecco un semplice esempio con AspectJ load-time Tessitura su GitHub https://github.com/medvedev1088/aspectj-ltw-example
Si utilizza la libreria Joda momento di dimostrare come intercettare il DateTime # toString() chiamate di metodi.
L'aspetto:
@Aspect
public class DateTimeToStringAspect {
public static final String TO_STRING_RESULT = "test";
@Pointcut("execution(* org.joda.time.base.AbstractDateTime.toString())")
public void dateTimeToString() {
}
@Around("dateTimeToString()")
public Object toLowerCase(ProceedingJoinPoint joinPoint) throws Throwable {
Object ignoredToStringResult = joinPoint.proceed();
System.out.println("DateTime#toString() has been invoked: " + ignoredToStringResult);
return TO_STRING_RESULT;
}
}
aop.xml
<aspectj>
<aspects>
<!-- Aspects -->
<aspect name="com.example.aspectj.DateTimeToStringAspect"/>
</aspects>
<weaver options="-verbose -showWeaveInfo">
<include within="org.joda.time.base.AbstractDateTime"/>
</weaver>
</aspectj>
prova:
public class DateTimeToStringAspectTest {
@Test
public void testDateTimeToString() throws Exception {
assertThat(new DateTime().toString(), is(DateTimeToStringAspect.TO_STRING_RESULT));
}
}
configurazione del plugin infallibile da pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version>
<configuration>
<argLine>-XX:-UseSplitVerifier</argLine>
<argLine>-javaagent:${user.home}/.m2/repository/org/aspectj/aspectjweaver/${aspectjweaver.version}/aspectjweaver-${aspectjweaver.version}.jar</argLine>
</configuration>
</plugin>
grazie. Proverò con call() pointcut. Penso che sarà un'opzione facile per me –
Ciao Sean Patrick, funzionerà su tutte le versioni di Aspectj? Funziona su Aspectj 1.5.3 ma non in 1.1.3. –
@sonic Non ho idea, non ho mai usato alcuna versione di AspectJ più vecchia di 1.6.x –