Spring Reference对这两个切点标识符的用法描述得不太清楚:
[quote]this - limits matching to join points (the execution of methods when using Spring AOP) where the bean
reference (Spring AOP proxy) is an instance of the given type
• target - limits matching to join points (the execution of methods when using Spring AOP) where the target
object (application object being proxied) is an instance of the given type[/quote]
似乎看不出两者的具体区别,我试着做了一个测试:
切面定义类:
[code]package com.baobaotao.expression;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class PreGreetingAspect{
@Before("target(com.baobaotao.expression.NaiveWaiter)")
public void beforeGreeting(){
System.out.println("How are you");
}
}[/code]
目标类接口:
[code]package com.baobaotao.expression;
public interface Waiter {
public void greetTo(String name);
public void serveTo(String name);
}[/code]
目标接口实现类:
[code]package com.baobaotao.expression;
public class NaiveWaiter implements Waiter {
public void greetTo(String name) {
System.out.println("greet to "+name+"...");
}
public void serveTo(String name){
System.out.println("serving "+name+"...");
foo(name);
}
public void foo(String name){
System.out.println("foo "+name+"...");
}
}[/code]
然后是配置:
[code]<aop:aspectj-autoproxy />
<bean id="waiter" class="com.baobaotao.expression.NaiveWaiter" />
<bean id="greetingAfter" class="com.baobaotao.expression.PreGreetingAspect" /> [/code]
最后是测试:
[code]package com.baobaotao.expression;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.baobaotao.expression.Waiter;
public class TestExpression {
public static void main(String[] args) {
String configPath = "com/baobaotao/expression/beans.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath);
Waiter waiter = (Waiter)ctx.getBean("waiter");
waiter.greetTo("John");
waiter.serveTo("John");
}
}[/code]
输出以下信息:
[code]How are you
greet to John...
How are you
serving John...[/code]
将切点定义换成:
@Before("this(com.baobaotao.expression.Waiter)")
输出的效果是完全一样的。
我的分析:
我本来以为NaiveWaiter的foo()方法在使用@Before("target(com.baobaotao.expression.NaiveWaiter)")应该也可以织入切面,但是发现不管采用哪种方式只能为Waiter接口织入切面。
请问这两个切点表示符到底有什么区别,我现在看到的是两者好象完全一样。
[quote]this - limits matching to join points (the execution of methods when using Spring AOP) where the bean
reference (Spring AOP proxy) is an instance of the given type
• target - limits matching to join points (the execution of methods when using Spring AOP) where the target
object (application object being proxied) is an instance of the given type[/quote]
似乎看不出两者的具体区别,我试着做了一个测试:
切面定义类:
[code]package com.baobaotao.expression;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class PreGreetingAspect{
@Before("target(com.baobaotao.expression.NaiveWaiter)")
public void beforeGreeting(){
System.out.println("How are you");
}
}[/code]
目标类接口:
[code]package com.baobaotao.expression;
public interface Waiter {
public void greetTo(String name);
public void serveTo(String name);
}[/code]
目标接口实现类:
[code]package com.baobaotao.expression;
public class NaiveWaiter implements Waiter {
public void greetTo(String name) {
System.out.println("greet to "+name+"...");
}
public void serveTo(String name){
System.out.println("serving "+name+"...");
foo(name);
}
public void foo(String name){
System.out.println("foo "+name+"...");
}
}[/code]
然后是配置:
[code]<aop:aspectj-autoproxy />
<bean id="waiter" class="com.baobaotao.expression.NaiveWaiter" />
<bean id="greetingAfter" class="com.baobaotao.expression.PreGreetingAspect" /> [/code]
最后是测试:
[code]package com.baobaotao.expression;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.baobaotao.expression.Waiter;
public class TestExpression {
public static void main(String[] args) {
String configPath = "com/baobaotao/expression/beans.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath);
Waiter waiter = (Waiter)ctx.getBean("waiter");
waiter.greetTo("John");
waiter.serveTo("John");
}
}[/code]
输出以下信息:
[code]How are you
greet to John...
How are you
serving John...[/code]
将切点定义换成:
@Before("this(com.baobaotao.expression.Waiter)")
输出的效果是完全一样的。
我的分析:
我本来以为NaiveWaiter的foo()方法在使用@Before("target(com.baobaotao.expression.NaiveWaiter)")应该也可以织入切面,但是发现不管采用哪种方式只能为Waiter接口织入切面。
请问这两个切点表示符到底有什么区别,我现在看到的是两者好象完全一样。