使用DefaultAdvisorAutoProxyCreator实现spring的自动代理

本文介绍了如何使用DefaultAdvisorAutoProxyCreator在Spring中实现自动代理。通过配置Advisor,匹配业务实现类中特定模式的方法,将通知切入到相应的方法中。在测试程序中,展示了所有匹配的sell方法都被前置通知成功切入。
摘要由CSDN通过智能技术生成

DefaultAdvisorAutoProxyCreator这个类功能更为强大,这个类的奇妙之处是他实现了BeanProcessor接口,当ApplicationContext读如所有的Bean配置信息后,这个类将扫描上下文,寻找所有的Advistor(一个Advisor是一个切入点和一个通知的组成),将这些Advisor应用到所有符合切入点的Bean中

业务接口:

 

package  StaticAdvisorTest;

public   interface  Shopping  {
  
public String buySomething(String type);
  
public String buyAnything(String type);
  
public String sellSomething(String type);
  
public String sellAnything(String type);

}

 

业务实现类:

 

package  AutoProxyTwo;

public   class  ShoppingImplA  implements  Shopping  {
    
private Customer customer;
    
public Customer getCustomer() {
        
return customer;
    }

    
public void setCustomer(Customer customer) {
        
this.customer = customer;
    }

    
public String buySomething(String type) {
        System.out.println(
this.getCustomer().getName()+" bye "+type+" success");
        
return null;
    }

    
    
public String buyAnything(String type) {
       System.out.println(
this.getCustomer().getName()+" bye "+type+" success");
       
return null;

     }

    
public String sellAnything(String type) 
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值