谁能说说@AspectJ中this和target这两个切点标识符的具体区别

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接口织入切面。
请问这两个切点表示符到底有什么区别,我现在看到的是两者好象完全一样。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值