Spring—AOP

1.AOP是什么?
AOP(Aspect-oriented programming)面向切面编程。
2.Aop个人理解:
例子:外科医生+护士
外科医生:相当与一个目标(Target)指被通知(被代理)的对象,用来完成具体的业务逻辑。
比如:给病人开刀.下刀的部位叫 Pointcut(切点)
护士:通知(Advice)在某个特定的连接点上执行的动作。
比如:给医生拿东西,给病人打麻药
Target和Advice组合在一起就是个代理(Proxy)。
代理(Proxy):将通知应用到目标对象后创建的对象。
注3:只有代理对象才有AOP功能,而AOP的代码是写在通知的方法里面的。
总体来说:Aop就是“专心做事”,目标只做具体业务逻辑不管其他,通知则是通用的,帮助所有目标做事。
3.Aop相关概念:

  • Aspect(切面): Aspect 声明类似于 Java 中的类声明,在 Aspect 中会包含着一些 Pointcut 以及相应的 Advice。
  • Joint point(连接点):表示在程序中明确定义的点,典型的包括方法调用,对类成员的访问以及异常处理程序块的执行等等,它自身还可以嵌套其它 joint point。
  • Pointcut(切点):表示一组 joint point,这些 joint point 或是通过逻辑关系组合起来,或是通过通配、正则表达式等方式集中起来,它定义了相应的 Advice 将要发生的地方。
  • Advice(增强):Advice 定义了在 Pointcut 里面定义的程序点具体要做的操作,它通过 -before、after 和 around 来区别是在每个 joint point 之前、之后还是代替执行的代码。
  • Target(目标对象):织入 Advice 的目标对象.。
  • Weaving(织入):将 Aspect 和其他对象连接起来, 并创建 Adviced object 的过程。

4.Aop四大通知外加适配器

1.前置通知(org.springframework.aop.MethodBeforeAdvice)
作用:在连接点之前执行的通知()。
2.后置通知(org.springframework.aop.AfterReturningAdvice)
作用:在连接点正常完成后执行的通知
3.环绕通知(org.aopalliance.intercept.MethodInterceptor)
作用:包围一个连接点的通知,最大特点是可以修改返回值,由于它在方法前后都加入了自己的逻辑代码,因此功能异常强大。
4.异常通知(org.springframework.aop.ThrowsAdvice)
作用:这个通知会在方法抛出异常退出时执行。
附加:适配器(org.springframework.aop.support.RegexpMethodPointcutAdvisor)
适配器=通知(Advice)+切入点(Pointcut)

spring.xml

<?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"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 目标 -->
<bean id="bookBizTarget" class="pk.BookBizImpl">
</bean>
<!-- 前置通知 -->
<bean id="myMethodBeforeAdvice" class="aop.MyMethodBeforeAdvice"></bean>
<!-- 后置通知 -->
<bean id="myMyAfterReturningAdvice" class="aop.MyAfterReturningAdvice"></bean>
<!-- 环绕通知 -->
<bean id="myMethodInterceptor" class="aop.MyMethodInterceptor"></bean>
<!-- 异常通知 -->
<bean id="myThrowsAdvice" class="aop.MyThrowsAdvice"></bean>
<!-- 适配器 -->
<bean id="regexpMethod" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
 <property name="patterns">
  <!-- 执行为buy结尾的 -->
  <value>.*buy</value>
 </property>
 <property name="advice">
  <ref bean="myMyAfterReturningAdvice"/>
 </property>
</bean>
<!-- 代理=目标+通知 -->
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
 <!-- 代理对象实现的接口 -->
 <property name="target">
  <ref bean="bookBizTarget"/>
 </property>
 <property name="proxyInterfaces">
  <list>
   <value>pk.IBookBiz</value>
  </list>
 </property>
 <property name="interceptorNames">
  <list>
   <value>myMethodBeforeAdvice</value>
   <value>regexpMethod</value>
   <value>myMethodInterceptor</value>
   <value>myThrowsAdvice</value>
  </list>
 </property>
</bean>
</beans>

MyMethodBeforeAdvice (前置通知)

package aop;
import java.lang.reflect.Method;
import java.util.Arrays;
import org.springframework.aop.MethodBeforeAdvice;
public class MyMethodBeforeAdvice implements MethodBeforeAdvice {
 /**
  * 前置通知
  * method 方法
  * object[] 方法参数
  * Object 目标对象 
  */
 @Override
 public void before(Method method, Object[] args, Object target) throws Throwable {
  String methodName = method.getName();
  String msg = target.getClass().getName()+"."+methodName+"执行,参数为:"+Arrays.toString(args); 
  System.out.println("系统日志:"+msg);
 }
}

MyAfterReturningAdvice (后置通知)

package aop;
import java.lang.reflect.Method;
import java.util.Date;
import org.springframework.aop.AfterReturningAdvice;
public class MyAfterReturningAdvice implements AfterReturningAdvice {
 /**
  * 后置通知
  */
 @Override
 public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
  System.out.println("销售返利]["+new Date().toLocaleString()+"]返利3元");
 }
}

MyMethodInterceptor (环绕通知)

package aop;
import java.util.Arrays;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class MyMethodInterceptor implements MethodInterceptor {
 /**
  * 环绕通知
  */
 @Override
 public Object invoke(MethodInvocation invocation) throws Throwable {
  //获取名
  String methodName = invocation.getMethod().getName();
  Object[] args = invocation.getArguments();
  //目标对象
  Object target = invocation.getThis();
  String msg = target.getClass().getName()+"."+methodName+"执行,参数为:"+Arrays.toString(args); 
  System.out.println("[环绕通知]系统日志:"+msg);
  Object returnValue = invocation.proceed();//放行执行目标对象中方法
  System.out.println("[环绕通知]returnValue="+returnValue);
  return returnValue;
 }
}

MyThrowsAdvice (异常通知)

package aop;
import java.sql.SQLException;
import org.springframework.aop.ThrowsAdvice;
import ex.PriceException;
public class MyThrowsAdvice implements ThrowsAdvice {
 /**
  * 异常通知
  * @param e
  */
 public void afterThrowing(SQLException e) {
  System.out.println("SQLException");
 }
 public void afterThrowing(PriceException e) {
  System.out.println("订单取消");
 }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值