没有被告知的建议

时间:2015-03-03 09:16:24

标签: java spring-aop

我有一个简单的spring应用程序,代码如下:

Aspect Loader Class     包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;

}

圈子类

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

}

}

形状界面

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>

问题是我无法在shapeservice类的draw1()方法之前运行我的建议。我无法弄清楚我的通配符表达式中的问题。

1 个答案:

答案 0 :(得分:2)

您的通配符表达式很好。

方面不会触发draw1()方法的原因是因为它是从draw()调用的;它位于同一个bean中,即简单的java方法调用。

如果从其他spring bean调用draw1()或调用draw() ),则方面将会触发。

要亲自查看,请尝试以下

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