欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

举例讲解Java的Spring框架中AOP程序设计方式的使用

程序员文章站 2024-03-11 17:50:25
1、什么是aop aop是aspect oriented programming的缩写,意思是面向方面编程,aop实际是gof设计模式的延续。 2、关于spring...

1、什么是aop

aop是aspect oriented programming的缩写,意思是面向方面编程,aop实际是gof设计模式的延续。

2、关于spring aop的一些术语:
 a、切面(aspect):在spring aop中,切面可以使用通用类或者在普通类中以@aspect 注解(@aspectj风格)来实现
b、连接点(joinpoint):在spring aop中一个连接点代表一个方法的执行
c、通知(advice):在切面的某个特定的连接点(joinpoint)上执行的动作。通知有各种类型,其中包括"around"、"before”和"after"等通知。许多aop框架,包括spring,都是以拦截器做通知模型, 并维护一个以连接点为中心的拦截器链
d、切入点(pointcut):定义出一个或一组方法,当执行这些方法时可产生通知,spring缺省使用aspectj切入点语法。

3、通知类型
a、前置通知(@before):在某连接点(join point)之前执行的通知,但这个通知不能阻止连接点前的执行(除非它抛出一个异常)
b、返回后通知(@afterreturning):在某连接点(join point)正常完成后执行的通知:例如,一个方法没有抛出任何异常,正常返回
c、抛出异常后通知(@afterthrowing):方法抛出异常退出时执行的通知
d、后通知(@after):当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)
e、环绕通知(@around):包围一个连接点(join point)的通知,如方法调用。这是最强大的一种通知类型,环绕通知可以在方法调用前后完成自定义的行为,它也会选择是否继续执行连接点或直接返回它们自己的返回值或抛出异常来结束执行
 
4、@aspectj风格的aop配置

spring aop配置有两种风格:
a、xml风格 = 采用声明形式实现spring aop
b、aspectj风格 = 采用注解形式实现spring aop

5、实例

切面类testaspect

package com.spring.aop; 
/** 
 * 切面 
 */ 
public class testaspect { 
 
  public void doafter(joinpoint jp) { 
    system.out.println("log ending method: " 
        + jp.gettarget().getclass().getname() + "." 
        + jp.getsignature().getname()); 
  } 
 
  public object doaround(proceedingjoinpoint pjp) throws throwable { 
    long time = system.currenttimemillis(); 
    object retval = pjp.proceed(); 
    time = system.currenttimemillis() - time; 
    system.out.println("process time: " + time + " ms"); 
    return retval; 
  } 
 
  public void dobefore(joinpoint jp) { 
    system.out.println("log begining method: " 
        + jp.gettarget().getclass().getname() + "." 
        + jp.getsignature().getname()); 
  } 
 
  public void dothrowing(joinpoint jp, throwable ex) { 
    system.out.println("method " + jp.gettarget().getclass().getname() 
        + "." + jp.getsignature().getname() + " throw exception"); 
    system.out.println(ex.getmessage()); 
  } 
 
  private void sendex(string ex) { 
    //todo 发送短信或邮件提醒 
  } 
}  

package com.spring.service; 
/** 
 * 接口a 
 */ 
public interface aservice { 
   
  public void fooa(string _msg); 
 
  public void bara(); 
} 
package com.spring.service; 
/** 
 *接口a的实现类 
 */ 
public class aserviceimpl implements aservice { 
 
  public void bara() { 
    system.out.println("aserviceimpl.bara()"); 
  } 
 
  public void fooa(string _msg) { 
    system.out.println("aserviceimpl.fooa(msg:"+_msg+")"); 
  } 
} 
package com.spring.service; 
 
/** 
 *  service类b 
 */ 
public class bserviceimpl { 
 
  public void barb(string _msg, int _type) { 
    system.out.println("bserviceimpl.barb(msg:"+_msg+" type:"+_type+")"); 
    if(_type == 1) 
      throw new illegalargumentexception("测试异常"); 
  } 
 
  public void foob() { 
    system.out.println("bserviceimpl.foob()"); 
  } 
 
} 

applicationcontext

<?xml version="1.0" encoding="utf-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" 
  xmlns:aop="http://www.springframework.org/schema/aop" 
  xsi:schemalocation=" 
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
      http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" 
  default-autowire="autodetect"> 
  <aop:config> 
    <aop:aspect id="testaspect" ref="aspectbean"> 
      <!--配置com.spring.service包下所有类或接口的所有方法--> 
      <aop:pointcut id="businessservice" 
        expression="execution(* com.spring.service.*.*(..))" /> 
      <aop:before pointcut-ref="businessservice" method="dobefore"/> 
      <aop:after pointcut-ref="businessservice" method="doafter"/> 
      <aop:around pointcut-ref="businessservice" method="doaround"/> 
      <aop:after-throwing pointcut-ref="businessservice" method="dothrowing" throwing="ex"/> 
    </aop:aspect> 
  </aop:config> 
   
  <bean id="aspectbean" class="com.spring.aop.testaspect" /> 
  <bean id="aservice" class="com.spring.service.aserviceimpl"></bean> 
  <bean id="bservice" class="com.spring.service.bserviceimpl"></bean> 
 
</beans> 

 测试类aoptest

public class aoptest extends abstractdependencyinjectionspringcontexttests { 
   
  private aservice aservice; 
   
  private bserviceimpl bservice; 
   
  protected string[] getconfiglocations() { 
    string[] configs = new string[] { "/applicationcontext.xml"}; 
    return configs; 
  } 
   
   
  /** 
   * 测试正常调用 
   */ 
  public void testcall() 
  { 
    system.out.println("springtest junit test"); 
    aservice.fooa("junit test fooa"); 
    aservice.bara(); 
    bservice.foob(); 
    bservice.barb("junit test barb",0); 
  } 
   
  /** 
   * 测试after-throwing 
   */ 
  public void testthrow() 
  { 
    try { 
      bservice.barb("junit call barb",1); 
    } catch (illegalargumentexception e) { 
       
    } 
  } 
   
  public void setaservice(aservice service) { 
    aservice = service; 
  } 
   
  public void setbservice(bserviceimpl service) { 
    bservice = service; 
  } 
} 

 
运行结果如下:

log begining method: com.spring.service.aserviceimpl.fooa 
aserviceimpl.fooa(msg:junit test fooa) 
log ending method: com.spring.service.aserviceimpl.fooa 
process time: 0 ms 
log begining method: com.spring.service.aserviceimpl.bara 
aserviceimpl.bara() 
log ending method: com.spring.service.aserviceimpl.bara 
process time: 0 ms 
log begining method: com.spring.service.bserviceimpl.foob 
bserviceimpl.foob() 
log ending method: com.spring.service.bserviceimpl.foob 
process time: 0 ms 
log begining method: com.spring.service.bserviceimpl.barb 
bserviceimpl.barb(msg:junit test barb type:0) 
log ending method: com.spring.service.bserviceimpl.barb 
process time: 0 ms 
 
log begining method: com.spring.service.bserviceimpl.barb 
bserviceimpl.barb(msg:junit call barb type:1) 
log ending method: com.spring.service.bserviceimpl.barb 
method com.spring.service.bserviceimpl.barb throw exception 
测试异常