spring AspectJ打印日志和捕获异常

1.web模块添加AspectJy依赖

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
</dependency>
2.自定义Controller层的aop

package com.queen.manage.aop;

import com.queen.common.ResultBean;
import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Description
 *
 * @author wangsong
 * @date 9:20 2018/1/23
 */
public class ControllerAOP {

    private static final Logger logger = LoggerFactory.getLogger(ControllerAOP.class);

    public Object handlerControllerMethod(ProceedingJoinPoint pjp) {
        long startTime = System.currentTimeMillis();

        ResultBean<?> result;

        try {
            result = (ResultBean<?>) pjp.proceed();
            logger.info(pjp.getSignature() + "use time:" + (System.currentTimeMillis() - startTime));
        } catch (Throwable e) {
            result = handlerException(pjp, e);
        }

        return result;
    }

    private ResultBean<?> handlerException(ProceedingJoinPoint pjp, Throwable e) {
        ResultBean<?> result = new ResultBean();

        // 已知异常
        if (e instanceof Exception) {
            logger.info(e.getMessage());
            result.setMsg(e.getLocalizedMessage());
            result.setCode(ResultBean.FAIL);
        }/* else if (e instanceof UnloginException) {
            result.setMsg("Unlogin");
            result.setCode(ResultBean.NO_LOGIN);
        } else {
            logger.error(pjp.getSignature() + " error ", e);
            //TODO 未知的异常,应该格外注意,可以发送邮件通知等
            result.setMsg(e.toString());
            result.setCode(ResultBean.FAIL);
        }*/

        return result;
    }

}
3.建spring-aop.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" 
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
      
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
       ">

    <!-- aop -->
    <aop:aspectj-autoproxy expose-proxy="true"></aop:aspectj-autoproxy>

    <bean id="controllerAop" class="com.queen.manage.aop.ControllerAOP" />
    <aop:config>
        <aop:aspect id="myAop" ref="controllerAop">
            <aop:pointcut id="target"
                          expression="execution(public com.queen.common.ResultBean *(..))" />
            <aop:around method="handlerControllerMethod" pointcut-ref="target" />
        </aop:aspect>
    </aop:config>
</beans>
5.ResultBean结构

package com.queen.common;

import lombok.Data;

import java.io.Serializable;

/**
 * Description
 *
 * @author wangsong
 * @date 13:54 2018/1/18
 */
@Data
public class ResultBean<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    public static final int NO_LOGIN = -1;

    public static final int SUCCESS = 1;

    public static final int FAIL = 0;

    public static final int NO_PERMISSION = 2;

    private String msg = "success";

    private int code = SUCCESS;


    private T data;

    public ResultBean() {
        super();
    }

    public ResultBean(T data) {
        super();
        this.data = data;
    }

    public ResultBean(Throwable e) {
        super();
        this.msg = e.toString();
        this.code = FAIL;
    }

}
6.Controller层返回ResultBean

/**
 *Description 根据ID查询用户
 *@Author wangsong
 *@Date 11:51 2018/1/23
 *@param
 *@return
 *@throws
*/
@ResponseBody
@RequestMapping(value = "/queryUser",method = RequestMethod.POST)
public ResultBean<Map<String,String>> queryUserById(User user)throws Exception{
    return new ResultBean<Map<String,String>>(userService.queryUserById(user));
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值