springAOP日志

知识共享许可协议 版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons


springAOP日志配置具体步骤

一,在pom.xml文件中添加以下配置

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>${spring.version}</version>
    </dependency>
 
 
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.6.12</version>
    </dependency>

二,在resources文件夹下新建一个springaop-config.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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
        <!--把业务类交给spring管理-->
    <bean name="deptService" class="com.aaa.aop.service.DeptServiceImpl"></bean>
    <bean name="userService" class="com.aaa.aop.service.UserServiceImpl"></bean>
    <!--把日志记录类交给spring管理-->
    <bean name="logUtil" class="com.aaa.aop.util.LogUtil"></bean>
    <!--aop配置-->
    <aop:config>
        <!--切入点设置 int代表业务类方法的返回值 *通配所有方法  ..代表方法参数0个或多个-->
        <aop:pointcut id="pointcutOne" expression="execution(int *(..))"></aop:pointcut>
        <!--切面配置-->
        <aop:aspect ref="logUtil">
            <!--通知类型 后置通知-->
            <!--<aop:after-returning method="saveLogAfterReturning" pointcut-ref="pointcutOne"></aop:after-returning>-->
            <!--前置通知-->
            <!--<aop:before method="saveLogBefore" pointcut-ref="pointcutOne"></aop:before>-->
            <!--异常通知-->
            <!--<aop:after-throwing method="exceptionAfterThrowing" throwing="ex" pointcut-ref="pointcutOne"></aop:after-throwing>-->
            <!--最终通知 -->
            <!--<aop:after method="executeAfter" pointcut-ref="pointcutOne"></aop:after>-->
            <!--环绕通知-->
            <aop:around method="executeAround" pointcut-ref="pointcutOne"></aop:around>
        </aop:aspect>
    </aop:config>
</beans>

三,写一个员工接口和部门接口并写两个实现类实现这两个接口

package com.aaa.aop.service;
public interface UserService {
    /**
     * 员工添加
     * @return
     */
    int add();
    /**
     * 员工修改
     * @return
     */
    int update();
    /**
     * 员工删除
     * @return
     */
    int delete();
}
public class UserServiceImpl implements UserService{
 
 
    @Override
    public int add() {
        System.out.println("模拟员工添加-----");
        return 0;
    }
 
 
    @Override
    public int update() {
        System.out.println("模拟员工更新-----");
        return 0;
    }
 
 
    @Override
    public int delete() {
        System.out.println("模拟员工删除-----");
        return 0;
    }
}

四,在util包中写一个LogUtil类

public class LogUtil {
    /**
     * 通用日志记录方法(后置通知)
     */
    public void saveLogAfterReturning(JoinPoint joinPoint){
        String jpName = joinPoint.getSignature().getName();
        System.out.println("在执行"+jpName+"方法后,记录了日志---");
    }
 
 
    /**
     * 前置通知
     * @param joinPoint
     */
    public void saveLogBefore(JoinPoint joinPoint){
        //获取连接点的方法名称
        String jpName = joinPoint.getSignature().getName();
        System.out.println("在执行"+jpName+"方法之前,记录了日志---");
    }
 
 
    /**
     * 异常通知
     * @param joinPoint
     * @param ex
     */
    public void exceptionAfterThrowing(JoinPoint joinPoint,Exception ex){
        String jpName = joinPoint.getSignature().getName();
        System.out.println("在执行"+jpName+"方法过程中出现了异常,异常名称" +
                "为"+ex.getClass().getName()+",异常描述:"+ex.getMessage());
    }
 
 
    /**
     * 最终通知
     */
    public void executeAfter(){
        System.out.println("无论有没有异常都会执行的方法---");
    }
 
 
    /**
     * 环绕通知
     * @param proceedingJoinPoint
     * @return
     */
    public Object executeAround(ProceedingJoinPoint proceedingJoinPoint){
        System.out.println(System.currentTimeMillis()+"执行业务之前,处理业务1-------");
        Object o = null;
        try {
            System.out.println("执行"+proceedingJoinPoint.getSignature().getName()+"方法------");
            //调用业务方法
            o = proceedingJoinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        System.out.println(System.currentTimeMillis()+"执行业务之后,处理业务2-------");
        return o;
    }
}

五,最后写一个AOP测试类

public class AopTest {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("springaop-config.xml");
        DeptService deptService = (DeptService) applicationContext.getBean("deptService");
        deptService.add();
        deptService.update();
        deptService.delete();
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_44001965/article/details/92584989