aop日志管理(摘)

<bean id="logAopBean" class="com.demo.common.aop.LogAop"></bean>
    <aop:config>
        <aop:aspect id="logAspect" ref="logAopBean">
            <aop:pointcut expression="execution(* com.demo..*(..))" id="allMethod"/>
            <aop:before method="before" pointcut-ref="allMethod" />
            <aop:after-throwing method="afterThrowing" pointcut-ref="allMethod" />
            <aop:after-returning method="afterReturn" pointcut-ref="allMethod" />
            <aop:after method="after" pointcut-ref="allMethod" />
        </aop:aspect>
    </aop:config>

2、日志处理类,

/**
 * LogAop.java
 *
 * Shanghai NTT DATA Synergy Software Co., Ltd. All Rights Reserved.
 * @author wyl
 * @date 2016-10-18
 */ 

package com.demo.common.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

/**
 * @author wyl
 * @Description TODO 
 * @date 2016-10-18
 *
 */

public class LogAop {
    public void before(JoinPoint call){
        String className = call.getTarget().getClass().getName();
        String methodName = call.getSignature().getName();
        System.out.println("开始执行:"+className+"."+methodName+"()方法...");
    }
    public void afterThrowing(JoinPoint call){
        String className = call.getTarget().getClass().getName();
        String methodName = call.getSignature().getName();
        System.out.println(className+"."+methodName+"()方法抛出了异常...");
    }
    public void afterReturn(JoinPoint call){
        String className = call.getTarget().getClass().getName();
        String methodName = call.getSignature().getName();
        System.out.println(className+"."+methodName+"()方法正常执行结束...");
    }
    public void after(JoinPoint call){
        String className = call.getTarget().getClass().getName();
        String methodName = call.getSignature().getName();
        System.out.println(className+"."+methodName+"()最终执行步骤(finally)...");
    }
    /*//用来做环绕通知的方法可以第一个参数定义为org.aspectj.lang.ProceedingJoinPoint类型
    public Object doAround(ProceedingJoinPoint call) throws Throwable {
        Object result = null;
        this.before(call);//相当于前置通知
        try {
            result = call.proceed();
            this.afterReturn(call); //相当于后置通知
        } catch (Throwable e) {
            this.afterThrowing(call); //相当于异常抛出后通知
            throw e;
        }finally{
            this.after(call);  //相当于最终通知
        }
        return result;
    }*/
}

二、注解方式

1、配置applicationContext.xml,

<bean id="logAspectBean" class="com.demo.common.aop.LogAnnotationAspect"></bean>
    <aop:aspectj-autoproxy/>

2、日志处理类,

/**
 * LogAnnotationAspect.java
 *
 * Shanghai NTT DATA Synergy Software Co., Ltd. All Rights Reserved.
 * @author wyl
 * @date 2016-10-18
 */ 

package com.demo.common.aop;

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.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

/**
 * @author wyl
 * @Description TODO 
 * @date 2016-10-18
 *
 */

@Aspect  //定义切面类
public class LogAnnotationAspect {
    @SuppressWarnings("unused")
    //定义切入点,提供一个方法,这个方法的名字就是改切入点的id
    @Pointcut("execution(* com.demo..*(..))")
    private void allMethod(){}
    //针对指定的切入点表达式选择的切入点应用前置通知
    @Before("allMethod()")
    public void before(JoinPoint call) {
        String className = call.getTarget().getClass().getName();
        String methodName = call.getSignature().getName();
        System.out.println("开始执行:"+className+"."+methodName+"()方法...");
    }
    //访问命名切入点来应用后置通知
    @AfterReturning("allMethod()")
    public void afterReturn(JoinPoint call) {
        String className = call.getTarget().getClass().getName();
        String methodName = call.getSignature().getName();
        System.out.println(className+"."+methodName+"()方法正常执行结束...");
    }
    //应用最终通知
    @After("allMethod()")
    public void after(JoinPoint call) {
        String className = call.getTarget().getClass().getName();
        String methodName = call.getSignature().getName();
        System.out.println(className+"."+methodName+"()最终执行步骤(finally)...");
    }
    //应用异常抛出后通知
    @AfterThrowing("allMethod()")
    public void afterThrowing(JoinPoint call) {
        String className = call.getTarget().getClass().getName();
        String methodName = call.getSignature().getName();
        System.out.println(className+"."+methodName+"()方法抛出了异常...");
    }
    //应用周围通知
    //@Around("allMethod()")
    public Object doAround(ProceedingJoinPoint call) throws Throwable{
        Object result = null;
        this.before(call);//相当于前置通知
        try {
            result = call.proceed();
            this.afterReturn(call); //相当于后置通知
        } catch (Throwable e) {
            this.afterThrowing(call);  //相当于异常抛出后通知
            throw e;
        }finally{
            this.after(call);  //相当于最终通知
        }
        return result;
    }
}
时间: 2024-08-29 19:14:01

aop日志管理(摘)的相关文章

Spring AOP日志管理

AOP的介绍 AOP(Aspect-OrientedProgramming,面向方面编程) AOP的几个概念 1.切面(Aspect):切面就是一个关注点的模块化,如事务管理.日志管理.权限管理等: 2.连接点(Joinpoint):程序执行时的某个特定的点,在Spring中就是一个方法的执行: 3.通知(Advice):通知就是在切面的某个连接点上执行的操作,也就是事务管理.日志管理等: 4.切入点(Pointcut):切入点就是描述某一类选定的连接点,也就是指定某一类要织入通知的方法: 5.

SpringBoot整合aop日志管理

1. 开发前准备 1.1 前置知识 java基础自定义注解.反射 Spring aop SpringBoot简单基础知识即可 1.2 环境参数 开发工具:IDEA 基础环境:Maven+JDK8 所用技术:SpringBoot.lombok.mybatisplus.Spring aop SpringBoot版本:2.1.4 1.3 涉及知识点 自定义注解. 反射 spring aop 环绕通知 2. aop日志实现 AOP(Aspect Oriented Programming)是一个大话题,这

spring boot aop日志管理(MongoDB)

aop拦截的是controller层请求,正常的请求用@Before来拦截, 异常的请求用@AfterThrowing来拦截 1.引用aop jar包 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> <version>2.0.3.RELEASE</version>

spring AOP自定义注解方式实现日志管理

转:spring AOP自定义注解方式实现日志管理 今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在applicationContext-mvc.xml中要添加的 <mvc:annotation-driven />     <!-- 激活组件扫描功能,在包com.gcx及其子包下面自动扫描通过注解配置的组件 -->     <conte

Spring MVC 中使用AOP 进行统一日志管理--XML配置实现

1.介绍 上一篇博客写了使用AOP进行统一日志管理的注解版实现,今天写一下使用XML配置实现版本,与上篇不同的是上次我们记录的Controller层日志,这次我们记录的是Service层的日志.使用的工程还是原来的那个,具体的Spring mvc 工程搭建暂不介绍.上篇记录controller层日志的时候是将切面类组件叫给spring MVC 进行管理,因为 controller 也是交给spring MVC进行管理的,但是记录service 层日志的时候应该就应该再spring 容器中进行了,

SpringBoot全局日志管理(AOP)

1.在pom.xml中引入aop的jar包 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> 2.创建WebLogAspect类 package com.cppdy.log; import java.util.Enumeration; import

spring AOP自定义注解 实现日志管理

今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在applicationContext-mvc.xml中要添加的 <mvc:annotation-driven />     <!-- 激活组件扫描功能,在包com.gcx及其子包下面自动扫描通过注解配置的组件 -->     <context:component-scan base-pac

Spring Boot 入门(五):集成 AOP 进行日志管理

本篇文章是接着 Spring boot 入门(四):集成 Shiro 实现登陆认证和权限管理写的,按照前面几篇博客的教程,可以搭建一个简单的项目,主要包含了 Pagehelper+MyBatis 分页查询,Generator 代码自动生成器,Shiro登录及权限管理.本篇博客主要是集成 AOP 进行日志管理 1.导入 jar 包 1 <!-- aop --> 2 <dependency> 3 <groupId>org.springframework.boot</g

Spring Boot AOP 简易操作日志管理

AOP (Aspect Oriented Programming) 面向切面编程. 业务有核心业务和边缘业务. 比如用户管理,菜单管理,权限管理,这些都属于核心业务. 比如日志管理,操作记录管理,这些都是边缘业务,可以统一的提出来. 尝试使用SpringBoot +AOP 提出操作记录业务. github aop_demo package com.lick.aspect.lang.annotation; import com.lick.aspect.lang.enums.BusinessType