EJB中实现的AOP

现在AOP是越来越流行了,面向切面的编程,呵呵,似乎比面向对象的编程还要牛一些,在EJB中,就可以利用拦截机制来实现AOP

代码如下:

会话BEAN的 代码
  1. /*  
  2.  * TestInterceptorBean.java  
  3.  *  
  4.  * Created on 2006年12月14日, 上午10:38  
  5.  *  
  6.  * To change this template, choose Tools | Template Manager  
  7.  * and open the template in the editor.  
  8.  */  
  9.   
  10. package com.hadeslee.session;   
  11.   
  12. import javax.ejb.Stateless;   
  13. import javax.interceptor.AroundInvoke;   
  14. import javax.interceptor.Interceptors;   
  15. import javax.interceptor.InvocationContext;   
  16.   
  17. /**  
  18.  *  
  19.  * @author lbf  
  20.  */  
  21. @Stateless  
  22.   
  23. public class TestInterceptorBean implements com.hadeslee.session.TestInterceptorRemote {   
  24.        
  25.     /** Creates a new instance of TestInterceptorBean */  
  26.     public TestInterceptorBean() {   
  27.     }   
  28.        
  29.     public void printSth() {   
  30.         System.out.println("原始的输出");   
  31.     }   
  32.     @Interceptors({MyInter.class})   
  33.     public String sayHello(String string) {   
  34.         return "sayHelloTo: "+string;   
  35.     }   
  36.     @Interceptors({MyInter.class})   
  37.     public String who() {   
  38.         System.out.println("原始方法调用了");   
  39.         return "原始的WHO";   
  40.     }   
  41. }   
拦截机的 代码
  1. /*  
  2.  * MyInter.java  
  3.  *  
  4.  * Created on 2006年12月14日, 上午11:06  
  5.  *  
  6.  * To change this template, choose Tools | Template Manager  
  7.  * and open the template in the editor.  
  8.  */  
  9.   
  10. package com.hadeslee.session;   
  11.   
  12. import javax.interceptor.AroundInvoke;   
  13. import javax.interceptor.InvocationContext;   
  14.   
  15. /**  
  16.  *  
  17.  * @author lbf  
  18.  */  
  19. public class MyInter {   
  20.        
  21.     /** Creates a new instance of MyInter */  
  22.     public MyInter() {   
  23.     }   
  24.     @AroundInvoke  
  25.     public Object intercepter(InvocationContext ic)throws Exception{   
  26.         System.out.println("调用方的名字:"+ic.getBean());   
  27.         System.out.println("调用方的方法:"+ic.getMethod().getName());   
  28.         if(ic.getMethod().getName().equals("who"))   
  29.             ic.proceed();   
  30.         return "就是这个味";   
  31.     }   
  32. }   

在这里我们只需要在需要以后更换的方法加上一个注释@javax.interceptor.Interceptors就可以了,你可以指定一个拦截机,也可以指定多个拦截机,这个注释可以修饰类也可以修饰方法,如果放在类前面就表示此类中的所有公共方法都在调用的范围之内,如果是放在方法前面 ,则表示只作用此方法而不作用于其它方法,然后你可以写一个类来实现拦截,当然你也可以在同一个类里面实现拦截,实现拦截的类没什么特殊的地方,只要你拦截的方法遵守如下签名即可

@AroundInvoke
public Object XXX(InvocationContext ic)throws Exception{}

其中的XXX可以换成任何字符,,InvocationContext在运行的时候,由系统自动传入,

我觉得一般于拦截的地方都是以后可能会变的方法或者可能会增加功能的方法,所以应该拦截的类还是另写一个会比较好.并且使用中还发现,你可以先在方法或者类中添加@Interceptors注释,但是你指定的类没有实现@AroundInvoke方法,也不要紧,也不会报异常.所以可以先定义这个注释,以后需要的时候再改进@AroundInvoke方法,也挺好的.

在拦截的时候,你可以选择做完自己的事情,继续执行原来的方法,也可以选择就此结束,不让它往下执行了.并且如果方法有返回值的话,你如果返回一个不是同一个类型的对象,那编译也可以过,但是在运行时,会在调用此BEAN的地方抛出一个java.lang.ClassCastException异常.,所以有返回值的拦截一定要小心,如果方法没有返回值,那么你随便返回什么都可以,反正调用方也用不到这个值,这个值到最后应该就被丢弃了.

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值