Comment utiliser l'advice around ?
Bonjour à tous,
je suis en train de mettre en place un aspect AOP avec Spring afin de générer automatiquement des messages de log avant et après certaines méthodes. Pour cela, j'ai ajouté un aop:config dans mon fichier config.xml :
Code:
1 2 3 4 5 6 7 8 9 10 11
| <bean id="loggerAOP" class="test.commons.logger.LoggerAOP" />
<aop:config>
<aop:pointcut id="servicePointcut" expression="execution(* test.*.servicemetier.impl.*.*BS(..))" />
<aop:aspect id="serviceAspect" ref="loggerAOP">
<aop:before method="logMethodEntry" pointcut-ref="servicePointcut" />
<aop:after method="logMethodExit" pointcut-ref="servicePointcut" />
<aop:after-returning method="logMethodReturn" returning="result" pointcut-ref="servicePointcut" />
<aop:after-throwing method="logMethodThrow" throwing="error" pointcut-ref="servicePointcut" />
</aop:aspect>
</aop:config> |
Comment vous le constatez, je gère un advice "before" et trois advices "after" (i.e. after, after-returning et after-throwing).
J'ai entendu dire que l'advice "around" est super et qu'il permet d'englober tout ça en une seule déclaration. Le problème c'est que je ne sais pas comment l'utiliser et j'ai beaucoup de mal à trouver des ressources sur le net à ce sujet.
Pour vous éclairer un peu, voici ma classe LoggerAOP :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| public class LoggerAOP {
private Log LOGGER;
public void logMethodEntry(final JoinPoint jp) {
this.LOGGER.info(jp.getSignature().toShortString() + " - DEBUT");
}
public void logMethodExit(final JoinPoint jp) {
this.LOGGER.info(jp.getSignature().toShortString() + " - FIN");
}
public void logMethodReturn(final JoinPoint jp, final Object result) {
this.LOGGER.info(jp.getSignature().toShortString() + " - Return:" + result + " - DEBUT");
}
public void logMethodThrow(final JoinPoint jp, final Throwable error) {
this.LOGGER.error((jp.getSignature().toShortString() + " - ERROR"), error);
}
} |
Si quelqu'un pouvait m'orienter un peu, ou me transmettre un bon tuto, je sui preneur :)
Merci d'avance