Bonjour,

j'ai le code suivant mais MonLogger ne fait pas son boulot. En effet, il ne se lance jamais.

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
 
package ew.aop;
 
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.JoinPoint.StaticPart;
 
public class MonLogger {
 
    public void logMethodEntry(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        String name = joinPoint.getSignature().toLongString();
        StringBuffer sb = new StringBuffer(name + " called with: [");
        for (int i = 0; i < args.length; i++) {
            Object o = args[i];
            sb.append("'" + o + "'");
            sb.append((i == args.length - 1) ? "" : ", ");
        }
        sb.append("]");
        System.out.println(sb);
    }
 
    public void logMethodExit(StaticPart staticPart, Object result) {
        String name = staticPart.getSignature().toLongString();
        System.out.println(name + " returning: [" + result + "]");
    }
 
}
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
 
package ew.service;
 
public class MonService {
 
    public String hello(String msg){
        String s = "Hello "+msg;
        System.out.println(s);
        return s;
    }
}
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
 
package ew.test;
 
import junit.framework.TestCase;
 
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
 
import ew.service.MonService;
 
public class MonServiceTest extends TestCase {
 
    public void testMonService() {
 
        ClassPathResource resource = new ClassPathResource("context.xml");
        XmlBeanFactory context = new XmlBeanFactory( resource );
        MonService monService = (MonService) context.getBean("monService");
        monService.hello("from Spring !");
        monService.hello("coucou");
    }
}
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
<?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-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
 
    <!--    Debut de la configuration AOP -->    
    <aop:config>
        <aop:pointcut id="servicePointcut"  expression="execution(* ew.service.*..(..))"/>            
        <aop:aspect id="loggingAspect" ref="monLogger">
            <aop:before method="logMethodEntry"  pointcut-ref="servicePointcut"/>            
            <aop:after-returning method="logMethodExit" returning="result" pointcut-ref="servicePointcut"/>
        </aop:aspect>        
    </aop:config>
 
      <bean id="monLogger" class="ew.aop.MonLogger"/>    
    <!-- Fin de la configuration AOP -->    
 
    <bean name="monService" class="ew.service.MonService" />
 
</beans>
De plus, j'utilise maven mais je en trouve pas les groupID et artifact pour utiliser Spring (IOC, Aspect seulement)....