spring(aop面向切面编程)

aop很早有研究过,但是最近想回顾下,顺便记录下,aop的优点有很多,实用性也很广,就好比最早在公司没有使用aop的时候没个业务层都要写try catch来捕获异常,来处理异常,甚至于记录异常或者日志,那是一件相当繁琐的事情,但是当我们使用aop,讲异常日志处理机制通过切面技术来脱离出业务层,让业务层只来管控自己的业务逻辑,这样即减轻了开发上的效率问题,又使得代码上更加清晰。当然aop用处还不仅仅只有这些,比如权限控制、事务控制,这些将在后续有时间的时候继续记录下来。


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语句 结尾加上一个;


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值