2015-03-03 1 views
0

j'ai une simple application de printemps avec le code suivant:conseils ne sont pas appelés

Aspect Loader package classe com.ishan.spring.aspectLoader;

import org.springframework.context.support.AbstractApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 

import com.ishan.spring.services.ShapeService; 

public class AspectLoader { 

public static void main(String a[]){ 

    AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); 

    context.registerShutdownHook(); 

    ShapeService service = (ShapeService) context.getBean("shapeService"); 
    service.draw(); 

} 

}

ShapeService Classe

package com.ishan.spring.services; 

import com.ishan.spring.interfaces.Shape; 

public class ShapeService { 

public Shape getShape() { 
    return shape; 
} 

public void setShape(Shape shape) { 
    this.shape = shape; 
} 

public String draw1(){ 
    System.out.println("String draw called"); 
    this.shape.draw(); 
    return "drawn"; 
} 

public int draw(){ 
    System.out.println("int draw called"); 
    draw1(); 
    return 1; 
} 

private Shape shape; 

} 

classe Circle

package com.ishan.spring.impl; 

import com.ishan.spring.interfaces.Shape; 

public class Circle implements Shape{ 

private String name; 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

@Override 
public void draw() { 
    System.out.println("Circle drawn"); 

} 

}

interface Forme

package com.ishan.spring.interfaces; 

public interface Shape { 

public void draw(); 
} 

aspect Logging

package com.ishan.spring.aspects; 

import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.Before; 

@Aspect 
public class LoggingAspect { 

@Before("execution(public * draw*(..))") 
public void logBefore(){ 
    System.out.println("Advice run before method call"); 
} 

} 

spring.xml

<?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" 
default-autowire="byName" 
xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation="http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop.xsd 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 

<aop:aspectj-autoproxy/> 

<bean id="circle" class="com.ishan.spring.impl.Circle"> 
    <property name="name" value="myCircle"></property> 
</bean> 

<bean id="triangle" class="com.ishan.spring.impl.Triangle"> 
    <property name="name" value="myTriangle"></property> 
</bean> 

<bean id="shapeService" class="com.ishan.spring.services.ShapeService"> 
    <property name="shape" ref="triangle"></property> 
</bean> 

    <bean id="logAspect" class="com.ishan.spring.aspects.LoggingAspect"/> 

</beans> 

Le problème est que je ne peux pas courir mon conseil avant la draw1 () metho d de la classe shapeservice. Je ne suis pas capable de comprendre le problème dans mes expressions génériques.

Répondre

2

Votre expression générique est bonne.

La raison pour laquelle l'aspect ne se déclenche pas sur la méthode draw1() est qu'il est appelé à partir de draw(); qui est dans le même bean, c'est-à-dire une invocation de méthode java simple.

Si draw1() est invoqué l'aspect se déclenche pour que d'autres grains de printemps (ou comme draw() est invoquée).

Pour le voir ci-dessous pour vous-même essayer

ShapeService service = (ShapeService) context.getBean("shapeService"); 
service.draw1();