SpringAOP使用注解引入新功能

首先,我们声明一个接口:Performer,演员具有表演的能力

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com;  
  2.   
  3. import com.exception.PerformanceExcetion;  
  4.   
  5. public interface Performer {  
  6.   
  7.     void perform() throws PerformanceExcetion;  
  8. }  
然后,让一个杂技师继承演员的接口,并实现:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.entity;  
  2.   
  3. import com.Performer;  
  4. import com.exception.PerformanceExcetion;  
  5.   
  6. public class Juggler implements Performer{  
  7.       
  8.     private int beanBags=3;  
  9.       
  10.     public Juggler(){  
  11.           
  12.     }  
  13.       
  14.     public Juggler (int beanBags){  
  15.         this.beanBags=beanBags;  
  16.     }  
  17.   
  18.     @Override  
  19.     public void perform() throws PerformanceExcetion {  
  20.         System.out.println("Juggler(杂技师) "+ beanBags +" beanBags ");  
  21.           
  22.     }  
  23. }  
 

然后我们在声明一个接口:竞争者,有一个奖励现金的方法:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.entity;  
  2.   
  3. public interface Contestant {  
  4.     public void receiveAward();  
  5. }  
我们写一个他的实现类,每一个竞争者都给予现金奖励:
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.entity;  
  2.   
  3.   
  4. public class ContestantImpl implements Contestant{  
  5.   
  6.     public void receiveAward() {  
  7.         System.out.println("给予现金五千万");  
  8.           
  9.     }  
  10.   
  11. }  


上面两个接口是没有任何关联的,那么我们如何让杂技师既能具有表演者,有能是竞争者,表演的同时拿到现金奖励?这里我可以使用注解注入新的功能:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.entity;  
  2.   
  3. import org.aspectj.lang.annotation.Aspect;  
  4. import org.aspectj.lang.annotation.DeclareParents;  
  5.   
  6. @Aspect  
  7. public class ContestantIntroducer {  
  8.     //value:被引入指定接口的Bean类型,defaultImpl:提供了所引入接口的实现  
  9.         //@DeclareParents:指定了将被引入的接口  
  10.         @DeclareParents(value="com.entity.Juggler+",defaultImpl=ContestantImpl.class)  
  11.         public static Contestant contestant;  
  12.   
  13. }  

我们可以看到,vaule指定了要被引入的接口的实现类,defaultImpl:引入的接口实现类

在配置文件中声明这个两个Bean:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <aop:aspectj-autoproxy/>  
  2.         <bean id="juggler" class="com.entity.Juggler"></bean>  
  3.         <bean id="ContestantIntroducer" class="com.entity.ContestantIntroducer"></bean>  
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <bean id="ContestantImpl" class="com.entity.ContestantImpl"></bean>  
 

测试类:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. import com.entity.Contestant;  
  7. import com.entity.ContestantImpl;  
  8. import com.entity.ContestantIntroducer;  
  9. import com.entity.Juggler;  
  10. import com.entity.Thinker;  
  11.   
  12. public class springTest {  
  13.     public static void main(String[] args) {  
  14.         ApplicationContext cxt=new ClassPathXmlApplicationContext(  
  15.                  "classpath:/spring/spring.xml");  
  16.         Performer performer =(Performer)cxt.getBean("juggler");  
  17.         performer.perform();  
  18.         Contestant contestant =(Contestant)cxt.getBean("juggler");  
  19.         contestant.receiveAward();  
  20.           
  21.     }  
  22. }  

我们可以看到,获取同一个Bean杂技师,转换成不同的两个Bean,具有两个方法,那么此时杂技师就既是一名表演者,又是一名竞争者

输出:

Juggler(杂技师) 3 beanBags 
给予现金五千万

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值