一、AOP
Aop
AOP为Aspect Oriented Programming的缩写,意为:面向切面编程(也叫面向方面),可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善。AOP可以说也是这种目标的一种实现。
AOP技术利用一种称为"横切"的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将其命名为"Aspect",即切面。所谓"切面",简单说就是那些与业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减少系统的重复代码,降低模块之间的耦合度,并有利于未来的可操作性和可维护性。
AOP中关键性概念
1、切面(aspect):类是对物体特征的抽象,切面就是对横切关注点的抽象
2、连接点(Joinpoint):程序执行过程中明确的点,如方法的调用,或者异常的抛出.
3、目标(Target):被通知(被代理)的对象
注1:完成具体的业务逻辑
4、通知(Advice):在某个特定的连接点上执行的动作,同时Advice也是程序代码的具体实现,例如一个实现日志记录的代码(通知有些书上也称为处理)
注2:完成切面编程
5、代理(Proxy):将通知应用到目标对象后创建的对象(代理=目标+通知), 例子:外科医生+护士
注3:只有代理对象才有AOP功能,而AOP的代码是写在通知的方法里面的
6、切入点(Pointcut):多个连接点的集合,定义了通知应该应用到那些连接点。
(也将Pointcut理解成一个条件 ,此条件决定了容器在什么情况下将通知和目标组合成代理返回给外部程序)
7、 适配器(Advisor):适配器=通知(Advice)+切入点(Pointcut)
AOP五种通知
1、前置通知(org.springframework.aop.MethodBeforeAdvice):在连接点之前执行的通知()
案例:在购书系统当中使用AOP方式实现日志系统
2、后置通知(org.springframework.aop.AfterReturningAdvice):在连接点正常完成后执行的通知
3、环绕通知(org.aopalliance.intercept.MethodInterceptor):包围一个连接点的通知,最大特点是可以修改返回值,由于它在方法前后都加入了自己的逻辑代码,因此功能异常强大。
它通过MethodInvocation.proceed()来调用目标方法(甚至可以不调用,这样目标方法就不会执行)
4、异常通知(org.springframework.aop.ThrowsAdvice):这个通知会在方法抛出异常退出时执行
5、过滤通知(适配器)(org.springframework.aop.support.RegexpMethodPointcutAdvisor) 适配器=通知(Advice)+切入点(Pointcut)
接下来用案例来完成五种通知
项目结构
前置通知
/**
* 调用项目中的某一接口实现类的方法时,将列名.方法名,携带的参数,作为日志存储到数据库
* @author ASUS
*
*/
public class MyMethodBeforeAdvice implements MethodBeforeAdvice{
public void before(Method method, Object[] args, Object target) throws Throwable {
String targetName = target.getClass().getName();
String methodName = method.getName();
String params = Arrays.toString(args);
String msg = "[系统日志]:正在调用"+targetName+"."+methodName+".携带的参数:"+params;
System.out.println(msg);
}
}
后置通知
package com.zking.spring.aop.advice;
import java.lang.reflect.Method;
import java.util.Arrays;
import org.springframework.aop.AfterReturningAdvice;
public class MyAfterReturningAdvice implements AfterReturningAdvice{
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
// TODO Auto-generated method stub
String targetName = target.getClass().getName();
String methodName = method.getName();
String params = Arrays.toString(args);
String msg = "[返利通知,反3元]:正在调用"+targetName+"."+methodName+".携带的参数:"+params+";目标对象所调用方法的返回值:"+returnValue;
System.out.println(msg);
}
}
环绕通知=前置通知+后置通知。而且用环绕通知时要么前置和后置通知一起用,要么单独用前置或后置,不然会报错。
package com.zking.spring.aop.advice;
import java.lang.reflect.Method;
import java.util.Arrays;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.cglib.proxy.MethodProxy;
public class MyMethodInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation invocation) throws Throwable {
Object target = invocation.getThis();
Method method = invocation.getMethod();
Object[] args = invocation.getArguments();
String targetName = target.getClass().getName();
String methodName = method.getName();
String params = Arrays.toString(args);
String msg = "[返利通知,反3元]:正在调用"+targetName+"."+methodName+".携带的参数:"+params;
System.out.println(msg);
Object returnValue = invocation.proceed();
String msg2 = "[环绕通知:]目标对象所调用方法的返回值:"+returnValue;
System.out.println(msg2);
return returnValue;
}
}
异常通知 :这个接口里面没有定义方法,我们要求我们的类必须实现afterThrows这个方法。出现异常执行系统提示,然后进行处理。价格异常为例
public void afterThrowing( [Method method,] [Object args,] [Object target,] Throwable throwable );
前面三个参数都是可选的,只有第三个参数是必须的,同时我们还可以在同一个类中定义这个方法的多个版本,如:
public void afterThrowing( MyException1 ex ) {}
public void afterThrowing( MyException2 ex ) {}
public class MyThrowsAdvice implements ThrowsAdvice {
public void afterThrowing(PriceException ex) {
System.out.println("价格输入有误,购买失败,请重新输入!!!");
}
}
配置文件 spring-context.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"
default-autowire="byName"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- aop================================= -->
<!--目标对象 -->
<bean class="com.zking.spring.biz.impl.BookBizImpl" id="bookBiz"></bean>
<!-- 配置前置通知 -->
<bean class="com.zking.spring.aop.advice.MyMethodBeforeAdvice" id="MyMethodBeforeAdvice"></bean>
<!-- 配置后置通知 -->
<bean class="com.zking.spring.aop.advice.MyAfterReturningAdvice" id="MyAfterReturningAdvice"></bean>
<!-- 环绕通知 -->
<bean class="com.zking.spring.aop.advice.MyMethodInterceptor" id="MyMethodInterceptor"></bean>
<!-- 异常通知 -->
<bean class="com.zking.spring.aop.advice.MyThrowsAdvice" id="MyThrowsAdvice"></bean>
<!-- 过滤通知 -->
<bean class="org.springframework.aop.support.RegexpMethodPointcutAdvisor" id="MyAfterReturningAdvice2">
<property name="advice" ref="MyAfterReturningAdvice"></property>
<!-- .*buy 指的是任意字符0~n个,以buy结尾 -->
<!-- <property name="pattern" value=".*buy"></property> -->
<property name="patterns">
<list>
<value>.*buy</value>
</list>
</property>
</bean>
<!--生成代理(目标+通知) -->
<bean class="org.springframework.aop.framework.ProxyFactoryBean" id="proxyFactoryBean">
<property name="target" ref="bookBiz"></property>
<!-- 代理工厂成产的代理需要实现的接口列表 -->
<property name="proxyInterfaces">
<list>
<value>com.zking.spring.biz.IBookBiz</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>MyMethodBeforeAdvice</value>
<!-- <value>MyAfterReturningAdvice</value> -->
<value>MyAfterReturningAdvice2</value>
<value>MyMethodInterceptor</value>
<value>MyThrowsAdvice</value>
</list>
</property>
</bean>
</beans>
今天的spring就学习到这里了!!!