ZweiVivian 2016-08-10 00:52 采纳率: 100%
浏览 732
已采纳

Sring 通过手工编码方式基于类的代理实现前置增强

Waiter类:
public class Waiter {
public void greetTo(String name) {
System.out.println("waiter greet to "+name+"...");
}

public void serveTo(String name){
    System.out.println("waiter serving "+name+"...");
}

}
TestAdvisorByProxyFactory类:
public class TestAdvisorByProxyFactory {
public static void main(String[] args) {
Waiter waiterTarget = new Waiter();
BeforeAdvice advice = new GreetingBeforeAdvice();
Advisor advisor = new GreetingAdvisor();
ProxyFactory pf = new ProxyFactory();
pf.setOptimize(true);
pf.addAdvice(advice);
pf.addAdvisor(advisor);
Waiter waiter = (Waiter) pf.getProxy();
waiter.greetTo("john");
waiter.serveTo("john");
}
}
运行时候报错:
Exception in thread "main" org.springframework.aop.framework.AopConfigException: TargetSource cannot determine target class: Either an interface or a target is required for proxy creation.
at org.springframework.aop.framework.DefaultAopProxyFactory.createAopProxy(DefaultAopProxyFactory.java:56)
at org.springframework.aop.framework.ProxyCreatorSupport.createAopProxy(ProxyCreatorSupport.java:105)
at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:96)
at cn.zw.test.advisor.TestAdvisorByProxyFactory.main(TestAdvisorByProxyFactory.java:20)
怎么修改才能运行呢?

  • 写回答

2条回答 默认 最新

  • ZweiVivian 2016-08-10 02:02
    关注

    自己来回答吧:
    TestAdvisorByProxyFactory类:
    public class TestAdvisorByProxyFactory {
    public static void main(String[] args) {
    Waiter waiterTarget = new Waiter();
    BeforeAdvice advice = new GreetingBeforeAdvice();
    //把织入切面注释掉
    //Advisor advisor = new GreetingAdvisor();
    ProxyFactory pf = new ProxyFactory();
    pf.setOptimize(true);
    pf.addAdvice(advice);
    //pf.addAdvisor(advisor);
    pf.setTarget(waiterTarget);
    Waiter waiter = (Waiter) pf.getProxy();
    waiter.greetTo("john");
    waiter.serveTo("john");
    }
    }
    现在还没找到织入切面了以后怎么运行,等找到了在更新。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 平板录音机录音问题解决
  • ¥15 请问维特智能的安卓APP在手机上存储传感器数据后,如何找到它的存储路径?
  • ¥15 (SQL语句|查询结果翻了4倍)
  • ¥15 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用
  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 Hadoop集群部署启动Hadoop时碰到问题
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启