spring-aop深入

Posted 老邱2

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring-aop深入相关的知识,希望对你有一定的参考价值。

<!--我一般习惯把我所引用包的版本集中放在这里,这样比较直观-->  
  <properties>  
     <spring.version>4.1.3.RELEASE</spring.version>  
     <aspectj.version>1.6.11</aspectj.version>  
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  </properties>  
  
<!--测试包,自动生成-->  
  <dependencies>  
    <dependency>  
      <groupId>junit</groupId>  
      <artifactId>junit</artifactId>  
      <version>3.8.1</version>  
      <scope>test</scope>  
    </dependency>  
    <!--不用说,肯定是spring aop包-->  
      <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-aop</artifactId>  
            <version>${spring.version}</version>  
    </dependency>  
    <!--spring上下文包,在加载spring配置文件时用到-->  
      <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-context</artifactId>  
            <version>${spring.version}</version>  
    </dependency>  
    <!--使用AspectJ方式注解需要相应的包-->  
      <dependency>  
            <groupId>org.aspectj</groupId>  
            <artifactId>aspectjrt</artifactId>  
            <version>${aspectj.version}</version>  
        </dependency>  
         <!--使用AspectJ方式注解需要相应的包-->  
        <dependency>  
            <groupId>org.aspectj</groupId>  
            <artifactId>aspectjweaver</artifactId>  
            <version>${aspectj.version}</version>  
        </dependency>  
      
  </dependencies>  
package com.example.aop;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.example.annotation.LogInfo;
import com.example.config.BusinessException;
import com.example.config.Constants;
import com.example.config.HttpCode;
import com.example.config.ResultData;
import com.example.dao.vo.SysLog;
import com.example.service.SysLogService;
import com.google.gson.Gson;

/** 
 * @author  lilufeng 
 * @date 创建时间:2016年5月9日 下午1:01:47 
 */
@Component
@Aspect
public class ControllerAop {
    
    private static Logger logger = LoggerFactory.getLogger(ControllerAop.class);
    
    @Autowired
    SysLogService sysLogService;

    @Pointcut("execution(* com.example.controller..*.*(..))")
    public void init() {

    }
    
    @Before(value = "init()")
    public void before(JoinPoint jp) {
        //Object[] args = jp.getArgs();
        //logger.info("---------方法执行前执行.....");
    }

    @AfterReturning(value = "init()")
    public void afterReturning() {
        //logger.info("---------方法执行完执行.....");
    }

    @AfterThrowing(value = "init()")
    public void throwss() {
        //logger.info("--------方法异常时执行.....");
    }

    @After(value = "init()")
    public void after() {
        //logger.info("-------方法最后执行.....");
    }

    /**
     * 記錄日誌、全局異常處理
     * @param pjp
     * @return
     * @throws Exception
     */
    @SuppressWarnings("rawtypes")
    @Around(value = "init()")
    public Object around(ProceedingJoinPoint pjp) throws Exception {
        boolean isLog = false;//是否記錄日誌
        boolean logException = false;//是否記錄異常日誌
        SysLog sysLog = new SysLog();
        LogInfo logInfo = null;
        Gson g = new Gson();
        Map<String, Object> map = new HashMap<String, Object>();
        Object[] params = pjp.getArgs();
        if(params.length > 0){
            for (int i = 0; i < params.length; i++) {
                map.put("params"+i, params[i]);
            }
        }
        String targetName = pjp.getTarget().getClass().getName();    
        String methodName = pjp.getSignature().getName();    
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        logger.info("-------【方法 " + targetName + "." + methodName + "() 执行开始】.....");
        Class targetClass = Class.forName(targetName);    
        Method[] methods = targetClass.getMethods(); 
        for (int i = 0; i < methods.length; i++) {
            if(methodName.equals(methods[i].getName())){
                logInfo = methods[i].getAnnotation(LogInfo.class);
                if(null != logInfo){
                    isLog = true;
                    logger.info("记录日志:" + logInfo.operationContent());
                    sysLog.setContent(logInfo.operationContent());
                    sysLog.setTypeKey(logInfo.logType().value());
                    sysLog.setTypeValue(logInfo.logType().name());
                    sysLog.setIp(request.getRemoteAddr());
                    sysLog.setParams(g.toJson(map));
                }
            }
        }
        Object o = null;
        try {
            o = pjp.proceed();
        } catch (Throwable e) {
            if(isLog){
                logException = true;
                logger.info("记录异常日志:" + e.getMessage());
                if (e instanceof BusinessException) {
                    sysLog.setFailureCause(e.getMessage());
                }else if(e instanceof IllegalArgumentException){
                    sysLog.setFailureCause(e.getMessage());
                }else{
                    sysLog.setFailureCause("服务器内部错误!");
                }
                sysLog.setResult("0");
            }
            exceptionHandler(response,e);
        }
        if(isLog){
            if(!logException){
                logger.info("记录成功日志:" + logInfo.operationContent() + "-----成功!");
                sysLog.setResult("1");
            }
            sysLog.setOperator(2);
            sysLogService.addSysLog(sysLog);
        }
        logger.info("-------【方法 " + targetName + "." + methodName + "() 执行结束】.....");
        return o;
    }
    
    /**
     * 全局异常处理
     * @param response
     * @param e
     * @throws Exception
     */
    public void exceptionHandler(HttpServletResponse response, Throwable e) throws Exception {
        logger.error(Constants.Exception_Head, e);
        ResultData resultData = new ResultData();
        if (e instanceof IllegalArgumentException) {
            if (StringUtils.isNotBlank(e.getMessage())) {
                resultData.setHttpCode(HttpCode.BAD_REQUEST.value());
                resultData.setMsg(e.getMessage());
            } else {
                resultData.setHttpCode(HttpCode.BAD_REQUEST.value());
                resultData.setMsg(HttpCode.BAD_REQUEST.msg());
            }
        } else if (e instanceof BusinessException) {
            if (StringUtils.isNotBlank(e.getMessage())) {
                resultData.setHttpCode(HttpCode.CONFLICT.value());
                resultData.setMsg(e.getMessage());
            } else {
                resultData.setHttpCode(HttpCode.CONFLICT.value());
                resultData.setMsg(HttpCode.CONFLICT.msg());
            }
        } else {
            resultData.setHttpCode(HttpCode.INTERNAL_SERVER_ERROR.value());
            resultData.setMsg(HttpCode.INTERNAL_SERVER_ERROR.msg());
        }
        response.setContentType("application/json;charset=UTF-8");
        Gson g = new Gson();
        byte[] bytes = g.toJson(resultData).getBytes();
        response.getOutputStream().write(bytes);
    }
}

 

以上是关于spring-aop深入的主要内容,如果未能解决你的问题,请参考以下文章

Spring-AOP

Spring-AOP注解开发

Spring-AOP

spring-AOP原理

spring-AOP(面向切面编程)

Spring-AOP