AOP调用注意点



package com.agp.aop.impl;



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


//CBuyCar接口的实现类Customer

@Component
class Customer implements CBuyCar {


private int money;

public int getMoney() {
return money;
}


public void setMoney(int money) {
this.money = money;
}


public void buyCar() {
System.out.println(this+"spent"+money+"buy a car");
}


}

//CBuyCar接口

 interface CBuyCar {
public void buyCar();
}


//main测试注解的方式。

public class TestAop {


public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("aop-first.xml");

  CBuyCar cbc=(CBuyCar) ac.getBean("customer");

//    Customer c=ac.getBean("customer") ...com.sun.proxy.$Proxy7 cannot be cast to com.agp.aop.impl.Customer

// 提示$Proxy7不能强转为Customer。   说明从AOP容器ac中取出来的时候就是一个代理。这个代理智能强转为接口类型。

// 加入AOP配置后,ac的getBean也不是简单的实例化一个类。 而是通过代理的方式proxy.newinstance(cl,interfaces,invocationhandler)生成一个代理实例。

// 这个代理实例是一个实现了所有interfaces【】的实例。只能强转为接口的实现类对象。  所以这里不能用Customer 只能用CBuyCar。

  Customer c=(Customer) cbc;
 
  c.setMoney(100000);
  cbc.buyCar();
  c.buyCar();
 
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值