MethodInterceptor avec JSF et Spring
Bonjour,
Je veux calculer la durée de toutes mes méthodes. Pour cela j'ai voulu intégrer Spring dans mon application en JSF et j'ai mis un interceptor.
Voici ce que j'ai fait:
1) J'ai ajouté la librairie spring.jar et aopalliance.jar
2) J'ai créer une classe Java pour l'interception
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
package fr.ademe.evalad.util.PerformanceAudit;
import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.lang.time.StopWatch;
import org.apache.log4j.Logger;
/**
* Class interceptant n'importe quelle m�thode et mesure la dur�e de son
* ex�cution.
* @author Thomas Noirez
*/
public class PerformanceInterceptor implements MethodInterceptor {
/*** Le log. */
private final Logger log = Logger.getLogger(this.getClass());
/*** Temps max d'execution avant le log d'un message warning. */
private final long warningTimeInMs;
/*** Temps max d'execution avant le log d'un message warning. */
private final static long WARNING_TIME_IN_MS = 300;
/**
* Creates a new PerformanceInterceptor object.
*/
public PerformanceInterceptor() {
super();
this.warningTimeInMs = WARNING_TIME_IN_MS;
log.info("PerformanceInterceptor initialisé");
}
/**
* Creates a new PerformanceInterceptor object.
*/
public PerformanceInterceptor(long warningTimeInMs) {
super();
this.warningTimeInMs = warningTimeInMs;
log.info("PerformanceInterceptor initialisé");
}
/**
* Mesure du temps d'execution d'une methode.
* @param methodInvocation
* @return
* @throws Throwable
*/
public final Object invoke(final MethodInvocation methodInvocation)
throws Throwable {
log.debug("invoke - IN");
StopWatch sw = new StopWatch();
sw.start();
Object o = methodInvocation.proceed();
sw.stop();
this.logDebug(methodInvocation, sw.getTime());
log.debug("invoke - OUT");
return o;
}
/**
* Log des infos de la duree d'execution d'une methode
* @param methodInvocation
* @param ms
*/
private void logDebug (final MethodInvocation methodInvocation, long ms) {
log.debug("logDebug - IN");
Method m = methodInvocation.getMethod();
Object t = methodInvocation.getThis();
String tmp = "Dur. : " + ms + " ms " + t.getClass().getName() + " " + m.getName();
log.debug(tmp);
log.debug("logDebug - OUT");
}
} |
3) J'ai créer un fichier applicationContext.xml qui contient:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
<beans>
<bean id="performanceInterceptor" class="fr.ademe.evalad.util.PerformanceAudit.PerformanceInterceptor"/>
<bean id="performanceAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="performanceInterceptor"/>
<property name="patterns">
<list>
<value>.*</value>
</list>
</property>
</bean>
<bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />
</beans> |
4) Et dans mon web.wml j'ai ajouté un listenner:
Code:
1 2 3 4 5 6 7
|
<!-- Spring -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener> |
Suite à tous j'ai démarrer mon serveur qui m'as indiquer que tous était initialisé:
Code:
1 2 3 4 5 6 7 8
|
18:31:14,165 INFO ContextLoader:189 - Root WebApplicationContext: initialization started
18:31:14,227 INFO XmlWebApplicationContext:412 - Refreshing org.springframework.web.context.support.XmlWebApplicationContext@15c2843: display name [Root WebApplicationContext]; startup date [Wed Mar 25 18:31:14 CET 2009]; root of context hierarchy
18:31:14,352 INFO XmlBeanDefinitionReader:323 - Loading XML bean definitions from ServletContext resource [/WEB-INF/applicationContext.xml]
18:31:14,555 INFO XmlWebApplicationContext:427 - Bean factory for application context [org.springframework.web.context.support.XmlWebApplicationContext@15c2843]: org.springframework.beans.factory.support.DefaultListableBeanFactory@c19fbf
18:31:14,789 INFO DefaultListableBeanFactory:414 - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@c19fbf: defining beans [performanceInterceptor,performanceAdvisor,autoProxyCreator]; root of factory hierarchy
18:31:14,789 INFO PerformanceInterceptor:38 - PerformanceInterceptor initialisé
18:31:14,867 INFO ContextLoader:209 - Root WebApplicationContext: initialization completed in 702 ms |
Mais pourtant rien ne se logue, pouvez vous me dire poourquoi?
Merci