2015-03-03 58 views
0

我有以下代碼的簡單彈簧應用:通知沒有被稱爲

看點裝載器類 包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類

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; 

} 

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"); 

} 

}

Shape接口

package com.ishan.spring.interfaces; 

public interface Shape { 

public void draw(); 
} 

記錄方面

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> 

問題是我不能讓DRAW1之前運行我的意見( )方法d的shapeservice類。我無法找出通配符表達式中的問題。

回答

2

您的通配符表達很好。

該方面在draw1()方法上未觸發的原因是因爲它是從draw()調用的;它在同一個bean中,即簡單的java方法調用。

如果draw1()從其他的Spring bean(或者就像draw()調用)方面將火肯定調用。

要親身感受一下試試下面

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