监视服务提供者中ServiceImpl中方法的调用

监视服务提供者中方法的调用情况,统计方法执行所需要的时间

_**使用到了slf4jFactory提供给的类
-**使用到了aspectj的连接点和环绕通知和切面
_**可以在服务调用者中使用,监视服务对服务提供者的调用情况

1、导入pom文件中导入maven依赖
  <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.7.4</version>
        </dependency>
2、新增实体类
package cn.org.demo;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Proxy;


@Aspect
public class ServiceMonitor {

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


    @Around("execution(* cn.org..*Service.*(..))|| execution(* cn.org..*Interface.*(..))")
    public Object serviceInfo(ProceedingJoinPoint joinPoint) {

        Object target = joinPoint.getTarget();
        String className = null;
        String methodName = joinPoint.getSignature().getName();
        if ((Proxy.isProxyClass(target.getClass()))) {
            className = target.toString();
       }else {
            className=target.getClass().getName();
        }

        long begin = System.currentTimeMillis();
        try {
           Object retVal=  joinPoint.proceed();
           return retVal;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            throw new RuntimeException("方法执行异常!",throwable);
        }finally {
            long costTime = System.currentTimeMillis() - begin;
            logger.error("className:{} methodName:{} costtime:{}", className, methodName, costTime);
        }
    }
}
3、在在contextConfigLocation定义的配置文件中引入对aspectj aop的支持
<aop:aspectj-autoproxy proxy-target-class="true" />

3、在同样的配置文件中定义该对象,使该对象成为一个父容器中的对象,如果定义在子容器SpringMVC配置文件中会出错
<bean id="serviceMonitor" class="该类全路径名" />
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值