AOP
AOP(Aspect Oriented Programming),即面向切面编程;它利用一种称为”横切”的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将其命名为”Aspect”,即切面。所谓”切面”,简单说就是那些与业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减少系统的重复代码,降低模块之间的耦合度,并有利于未来的可操作性和可维护性。
使用”横切”技术,AOP把软件系统分为两个部分:核心关注点和横切关注点。业务处理的主要流程是核心关注点,与之关系不大的部分是横切关注点。横切关注点的一个特点是,他们经常发生在核心关注点的多处,而各处基本相似,比如权限认证、日志管理、事务管理。
AOP核心概念
1、横切关注点
对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为横切关注点
2、切面(aspect)
类是对物体特征的抽象,切面就是对横切关注点的抽象
3、连接点(joinpoint)
被拦截到的点,因为Spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法,实际上连接点还可以是字段或者构造器
4、切入点(pointcut)
对连接点进行拦截的定义
5、通知(advice)
所谓通知指的就是指拦截到连接点之后要执行的代码,通知分为前置、后置、异常、最终、环绕通知五类
6、目标对象
代理的目标对象
7、织入(weave)
将切面应用到目标对象并导致代理对象创建的过程
8、引入(introduction)
在不修改代码的前提下,引入可以在运行期为类动态地添加一些方法或字段
Spring中AOP代理由Spring的IOC容器负责生成、管理,其依赖关系也由IOC容器负责管理。因此,AOP代理可以直接使用容器中的其它bean实例作为目标,这种关系可由IOC容器的依赖注入提供。Spring创建代理的规则为:
1、默认使用Java动态代理来创建AOP代理,这样就可以为任何接口实例创建代理了
2、当需要代理的类不是代理接口的时候,Spring会切换为使用CGLIB代理,也可强制使用CGLIB
Spring 的事务管理,是 AOP 的应用,将事务作为切面织入到了 Service 层的业务方法中。
<!-- aop切面类 -->
<bean id="loggerAop" class="com.tp.soft.aop.LoggerAop"></bean>
<aop:config>
<!-- 定义一个切面 -->
<aop:aspect id="loggerAop" ref="loggerAop">
<!-- 定义切点正则表达式匹配 -->
<aop:pointcut expression="execution(* com.tp.soft.service..*.*(..))" id="mypoint"/>
<!-- 定义前置通知 -->
<aop:before method="doBefore" pointcut-ref="mypoint"/>
<!-- 定义环绕通知 -->
<aop:around method="doAround" pointcut-ref="mypoint"/>
<!-- 定义返回通知 -->
<aop:after-returning method="doAfterReturning" pointcut-ref="mypoint"/>
<!-- 定义返回异常通知 -->
<aop:after-throwing method="doAfterThrowing" pointcut-ref="mypoint" throwing="e"/>
</aop:aspect>
</aop:config>
LoggerAop.java
package com.tp.soft.aop;
import java.io.IOException;
import java.sql.SQLException;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import com.tp.soft.common.exception.BaseServiceException;
public class LoggerAop {
private Logger LOG = LoggerFactory.getLogger(Logger.class);
//前置通知
public void doBefore(JoinPoint jp){
System.out.println(jp.getTarget().getClass().getName());
}
public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("环绕开始");
Object proceed = pjp.proceed();
System.out.println("环绕结束");
return proceed;
}
public void doAfterReturning(JoinPoint jp){
System.out.println("返回通知");
}
public void doAfterThrowing(JoinPoint jp, Throwable e){
String errStr = "";
String err_detail = "";
if (e.getClass().equals(DataAccessException.class)) {
errStr = "数据库操作失败!";
} else if (e.getClass().toString()
.equals(NullPointerException.class.toString())) {
errStr = "调用了未经初始化的对象或者是不存在的对象!";
} else if (e.getClass().equals(IOException.class)) {
errStr = "IO异常!";
} else if (e.getClass().equals(ClassNotFoundException.class)) {
errStr = "指定的类不存在!";
} else if (e.getClass().equals(ArithmeticException.class)) {
errStr = "数学运算异常!";
err_detail = errStr;
} else if (e.getClass().equals(ArrayIndexOutOfBoundsException.class)) {
errStr = "数组下标越界!";
err_detail = errStr;
} else if (e.getClass().equals(IllegalArgumentException.class)) {
errStr = "方法的参数错误!";
err_detail = errStr;
} else if (e.getClass().equals(ClassCastException.class)) {
errStr = "类型强制转换错误!";
err_detail = errStr;
} else if (e.getClass().equals(SecurityException.class)) {
errStr = "违背安全原则异常!";
err_detail = errStr;
} else if (e.getClass().equals(SQLException.class)) {
errStr = "操作数据库异常!";
err_detail = errStr;
} else if (e.getClass().equals(NoSuchMethodError.class)) {
errStr = "方法末找到异常!";
err_detail = errStr;
} else if (e.getClass().equals(InternalError.class)) {
errStr = "Java虚拟机发生了内部错误";
err_detail = errStr;
} else if (e.getClass().equals(BaseServiceException.class)){
errStr = e.getMessage();
err_detail = e.getMessage();
} else {
errStr = e.getCause().getMessage();
err_detail = e.getCause().getLocalizedMessage();
}
System.err.println("aop打印错误:"+errStr);
System.err.println("aop打印错误详细:"+err_detail);
//可以操作数据库记录
}
}
测试结果
异常测试
http://blog.csdn.net/taoaic/article/details/53335653
https://www.cnblogs.com/qjjazry/p/6366204.html
https://www.cnblogs.com/hongwz/p/5764917.html
未完待续