IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Spring Java Discussion :

Spring AOP, aspects non pris en compte [Framework]


Sujet :

Spring Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Juin 2005
    Messages
    731
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2005
    Messages : 731
    Par défaut Spring AOP, aspects non pris en compte
    Bonjour,
    j'essaie d'intégrer Spring dans une appli Web et plus particulièrement Spring AOP.

    J'ai spécifié mon aspect-config.xml pour ajouter un aspect :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    <?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:aop="http://www.springframework.org/schema/aop"
    		xsi:schemaLocation="http://www.springframework.org/schema/beans
    							http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    							http://www.springframework.org/schema/aop
    							http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
     
    	<bean id="loggerException" class="loc.sppgroup.production.sampy.aspects.ExceptionLogger">
    	</bean>
     
    	<aop:aspectj-autoproxy>
    		<aop:include name="loggerException"/>
    	</aop:aspectj-autoproxy>
     
    </beans>
    J'ai ajouté mon aspect :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    package loc.sppgroup.production.sampy.aspects;
     
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.AfterThrowing;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
     
    @Aspect
    public class ExceptionLogger 
    {
    	private static Log log = null;
     
    	static
    	{
    		log = LogFactory.getLog(ExceptionLogger.class);
    	}
     
    	public ExceptionLogger ()
    	{
    		int a =0;
    		a++;
    	}
     
    	@AfterThrowing(
    		pointcut="* loc..*.*(..)",
    		throwing="ex")
    	public void dologgingActions(JoinPoint joinPoint, Throwable ex) 
    	{
    		if (joinPoint != null && ex != null)
    		{
    			log.error(joinPoint.getSignature().toLongString());
    			log.error(ex);
    		}
    	}
     
    	@Before ("* loc..*.*(..)")
    	public void doBefore ()
    	{
    		int a=0;
                    a++;
    	}
     
    	@Around("execution(* loc..*.*(..))")
    	public Object encoreuntest(ProceedingJoinPoint method) throws Throwable {
    		int a = 0;
    		return method.proceed();
    	}
    }
    Je constate que je ne passe JAMAIS par aucun de mes greffons.
    J'ai pourtant mis un point d'arrêt sur le constructeur et le static log de mon aspect et j'y passe bien. Par contre, pour les @Before, @AfterThrowing et @Around, rien du tout. J'ai pourtant spécifié large en indiquant que je voulais passer par toutes les méthodes des classes de tous les package de mon application.

    J'ai bien spécifié mon fichier de configuration aspect dans mon web.xml :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    	<!-- Spring Application Context -->
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>/WEB-INF/application-config.xml /WEB-INF/aspects-config.xml</param-value>
    	</context-param>
     
    	<listener>
    		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    	</listener>
    Et lorsque je démarre mon serveur, j'ai bien la prise en compte de Spring AOP :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    INFO [org.springframework.web.context.ContextLoader] - Root WebApplicationContext: initialization started
    INFO [org.springframework.web.context.support.XmlWebApplicationContext] - Refreshing Root WebApplicationContext: startup date [Wed Feb 01 10:46:06 CET 2012]; root of context hierarchy
    INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from ServletContext resource [/WEB-INF/application-config.xml]
    INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from ServletContext resource [/WEB-INF/aspects-config.xml]
    INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1a1ad24: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,loggerException,org.springframework.aop.config.internalAutoProxyCreator]; root of factory hierarchy
    INFO [org.springframework.web.context.ContextLoader] - Root WebApplicationContext: initialization completed in 1421 ms
    Ma question est : pourquoi est-ce que je ne passe jamais par mes aspects ?
    Merci


    PS : je tiens à préciser que j'ai ajouté les librairies aspectjrt et aspectjweaver dans mon projet pour l'utilisation des annotations aspectj

  2. #2
    Membre éclairé
    Profil pro
    Inscrit en
    Juin 2005
    Messages
    731
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2005
    Messages : 731
    Par défaut
    En regardant le schéma xml de l'annotation spring aop, j'ai vu un truc intéressant : expose-proxy

    J'ai donc modifié mon aspect-config.xml :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    	<aop:aspectj-autoproxy expose-proxy="true">
    		<aop:include name="loggerException"/>
    	</aop:aspectj-autoproxy>
    Et quand je fais ceci depuis une classe de test :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    			Object o = AopContext.currentProxy();
    Je me retrouve avec l'exception suivante :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    java.lang.IllegalStateException: Cannot find current proxy: Set 'exposeProxy' property on Advised to 'true' to make it available.
    Alors que j'ai bien positionné exposeproxy à true dans mon fichier de conf.

    Si quelqu'un a une idée, je l'en remercie.


    Petite précision : même sans passer par la WEBAPP en faisant un test en appli Java J2SE simple, je ne passe pas par mes greffons. J'ai une fonction à laquelle je sors exprès en NullPointerException et je ne passe pas par mon greffon @AfterThrowing.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [QR 4.05] Propriété imprimante non pris en compte
    Par portu dans le forum SAP Crystal Reports
    Réponses: 5
    Dernier message: 30/09/2008, 15h21
  2. CSS non pris en compte
    Par DragOr dans le forum Mise en page CSS
    Réponses: 7
    Dernier message: 08/06/2006, 14h18
  3. [ASE][SQL]WHERE non pris en compte
    Par Benjamin78 dans le forum Sybase
    Réponses: 1
    Dernier message: 24/03/2006, 13h00
  4. Accents non pris en compte dans les requêtes SELECT
    Par YanK dans le forum Requêtes
    Réponses: 1
    Dernier message: 30/08/2005, 11h57
  5. [event] keyListener non pris en compte
    Par pierre.zelb dans le forum Agents de placement/Fenêtres
    Réponses: 5
    Dernier message: 03/08/2005, 09h35

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo