Précédent   Forum des professionnels en informatique > Java > Général Java > Spring
Spring Forum d'entraide pour le framework Spring. Avant de poster -> FAQ Spring
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 01/02/2012, 10h47   #1
Membre éprouvé
 
Inscription : juin 2005
Messages : 726
Détails du profil
Informations forums :
Inscription : juin 2005
Messages : 726
Points : 492
Points : 492
Par défaut [debut]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 :
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 :
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 :
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 :
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
ep31 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 01/02/2012, 12h00   #2
Membre éprouvé
 
Inscription : juin 2005
Messages : 726
Détails du profil
Informations forums :
Inscription : juin 2005
Messages : 726
Points : 492
Points : 492
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 :
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 :
1
2
 
			Object o = AopContext.currentProxy();
Je me retrouve avec l'exception suivante :

Code :
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.
ep31 est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 17h20.


 
 
 
 
Partenaires

Hébergement Web