Configuration d'un aspect logging avec Spring2.0
Je débute avec la configuration d'AOP dans Spring 2 et j'essaie de configurer un aspect logging en suivant la documentation de référence et en utilisant les annotation d'AspectJ.
Voilà ce que j'ai fait à ce stade:
1- J'ai créé une classe SystemArchitecture pour enregistrer les pointcuts:
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
|
package com.mycompany.myapp.server.utilities;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect public class SystemArchitecture {
@Pointcut("within(com.mycompany..*")
public void inAllApplication() {}
@Pointcut("inBusinessLayer() && inPersistenceLayer() && inRemoteLayer() && inServiceLayer()")
public void inAllLayers() {}
@Pointcut("within(com.mycompany.myapp.server.business..*)")
public void inBusinessLayer() {}
@Pointcut("within(com.mycompany.myapp.server.persistence..*")
public void inPersistenceLayer() {}
@Pointcut("within(com.mycompany.myapp.server.remote..*")
public void inRemoteLayer() {}
@Pointcut("within(com.mycompany.myapp.server.service..*")
public void inServiceLayer() {}
} |
2- J'ai créé une classe aspect:
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
package com.mycompany.myapp.server.utilities.logging;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.Ordered;
@Aspect public class ServerSideLogger
implements Ordered {
public ServerSideLogger() {}
@After("com.mycompany.myapp.server.utilities.SystemArchitecture.inAllApplication()")
public void logOperationEnd(JoinPoint logged) {
getLog(logged).info("End of operation " + getLoggedOperationDescription(logged));
}
@AfterThrowing(
pointcut = "com.mycompany.myapp.server.utilities.SystemArchitecture.inAllApplication()",
throwing = "exception"
)
public void logOperationFailure(JoinPoint logged,
RuntimeException exception) {
Logger log = getLog(logged);
log.warn("Exception was thrown in " + getLoggedOperationDescription(logged));
if (log.isDebugEnabled()) {
log.debug(exception.getMessage(), exception);
}
}
@Before("com.mycompany.myapp.server.utilities.SystemArchitecture.inAllApplication()")
public void logOperationStart(JoinPoint logged) {
getLog(logged).info("Entering operation " + getLoggedOperationDescription(logged));
}
@AfterReturning(
pointcut = "com.mycompany.myapp.server.utilities.SystemArchitecture.inAllApplication()",
returning = "returnValue"
)
public void logOperationSuccess(JoinPoint logged,
Object returnValue) {
Logger log = getLog(logged);
log.info("Successful operation " + getLoggedOperationDescription(logged));
if (log.isTraceEnabled()) {
log.trace("Operation returned: " + returnValue);
}
}
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
private Logger getLog(JoinPoint logged) {
return Logger.getLogger(logged.getTarget().getClass());
}
private String getLoggedOperationDescription(JoinPoint logged) {
return logged.toLongString();
}
private int order;
} |
3- Comme j'ai plusieurs fichiers de config Spring qui sont tous chargés dans une application web, j'ai ajouté l'élément suivant à l'un de ces fichiers:
Code:
1 2
|
<aop:aspectj-autoproxy/> |
Et d'après ce que j'ai compris de la documentation, c'est tout ce qu'il faut faire pour que ça fonctionne. Malheureusement, quand je fais tourner mon application et que j'appelle quelques services, rien n'est loggé. Du coup j'ai ajouté quelques points d'arrêts dans Eclipse sur mes méthodes d'aspect, mais elles semblent ne jamais être appelées.
Alors de toute évidence j'ai oublié quelque chose d'important.
Est-ce que quelqu'un peut m'aider là-dessus?