aop很早有研究过,但是最近想回顾下,顺便记录下,aop的优点有很多,实用性也很广,就好比最早在公司没有使用aop的时候没个业务层都要写try catch来捕获异常,来处理异常,甚至于记录异常或者日志,那是一件相当繁琐的事情,但是当我们使用aop,讲异常日志处理机制通过切面技术来脱离出业务层,让业务层只来管控自己的业务逻辑,这样即减轻了开发上的效率问题,又使得代码上更加清晰。当然aop用处还不仅仅只有这些,比如权限控制、事务控制,这些将在后续有时间的时候继续记录下来。
下面就来介绍aop的基本配置和使用,aop的配置也是相当简单,基本一步就能搞定,项目就借用之前ssm+redis的基础了,有部分文件也就不一一写了,可以查阅ssm+redis(cache方式)那一章节。
1、spring-source.xml
<!-- 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>
2、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); //可以操作数据库记录 } }
测试打印
异常测试:
修改UserMapper.xml 的sql语句 结尾加上一个;