logger日志类:

package cn.mepu.utils;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 记录日志,提供公共代码
 * @author shkstart
 * @create 2019-11-09 15:25
 */
@Component("logger")
@Aspect //表示当前类是一个切面类
public class logger {

    //表达式
    @Pointcut("execution(* cn.mepu.service.imp.*.*(..))")
    private void pt1(){

    }
    /**
     * 前置日志
     */
    @Before("pt1()")
    public void beforePrintLog(){
        System.out.println(" logger中前置开始记录日志" );
    }

    /**
     * 后置日志
     */
    @AfterReturning("pt1()")
    public void afterreturningPrintLog(){
        System.out.println(" logger中后置开始记录日志" );
    }

    /**
     * 异常日志
     */
    @AfterThrowing("pt1()")
    public void afterThrowingPrintLog(){
        System.out.println(" logger中异常开始记录日志" );
    }

    /**
     * 最终日志
     */
    @After("pt1()")
    public void afterPrintLog(){
        System.out.println(" logger中最终开始记录日志" );
    }

    //@Around("pt1()")
    public Object aroundPringLog(ProceedingJoinPoint pjp){
        Object reValue = null;
        try {
            Object[] args = pjp.getArgs();//得到方法所需的参数
            //前置通知
            System.out.println("logger中环绕通知开始记录日志前置" );
            pjp.proceed(args);//明确调用切入点方法
            //后置通知
            System.out.println("logger中环绕通知开始记录日志后置" );
            return reValue;
        } catch (Throwable throwable) {
            //异常通知
            System.out.println("logger中环绕通知开始记录日志异常" );
            throw new RuntimeException(throwable);
        }finally {
            //最终通知
            System.out.println("logger中环绕通知开始记录日志最终" );
        }

    }

}

service层:

package cn.mepu.service.imp;

import cn.mepu.service.IAccountService;
import org.springframework.stereotype.Service;

/**
 * 账户业务层实现类
 * @author shkstart
 * @create 2019-11-09 15:23
 */
@Service("accountService")
public class AccountServiceImp implements IAccountService {
    @Override
    public void saveAccount() {
        System.out.println("保存");

    }

    @Override
    public void updateAccount(int i) {
        System.out.println("更新"+i);

    }

    @Override
    public int deleteAccount() {
        System.out.println("删除");
        return 0;
    }
}

测试类:

package cn.mepu.Test;

import cn.mepu.service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 测试Aop
 * @author shkstart
 * @create 2019-11-09 15:59
 */
public class AopTest {
    public static void main(String[] args) {
        //1.获取容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("Bean.xml");
        //2.获取对象
        IAccountService as = (IAccountService) ac.getBean("accountService");
        //3.执行方法
        as.saveAccount();

    }
}

Bea文件:

<?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"
       xmlns:context="http://www.springframework.org/schema/context"
       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
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 配置spring创建容器时要扫描的包-->
    <context:component-scan base-package="cn.mepu"></context:component-scan>

    <!-- 配置spring开启注解AOP的支持 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

POM文件;

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.mepu</groupId>
    <artifactId>day05_spring_aop_annotation</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.7</version>
        </dependency>
    </dependencies>

</project>

最新文章

  1. [转]ASP.NET Core 之 Identity 入门(二)
  2. Fedora 23 忘记root密码
  3. nodejs初探(二)第一个nodejs程序“hello world”
  4. SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-006- 如何保持重定向的request数据(用model、占位符、RedirectAttributes、model.addFlashAttribute(&quot;spitter&quot;, spitter);)
  5. CSharp tar类型文件压缩与解压
  6. 打包jar类库与使用jar类库
  7. AndroidStudio引入so文件
  8. LightOJ 1234 Harmonic Number 调和级数部分和
  9. Timestamp解析0000-00-00 00:00:00报格式错误
  10. Unity3d&amp;C#分布式游戏服务器ET框架介绍-组件式设计
  11. Node入门教程(4)第三章:第一个 Nodejs 程序
  12. Python函数之匿名函数
  13. Jenkins创建job时Check-out Strategy各个选项详细说明(含图)
  14. [CNN] Understanding Convolution
  15. ios开发之--调整UISearchBar的输入框的背景颜色
  16. sklearn 中的 Pipeline 机制 和FeatureUnion
  17. Spring 中出现相同名称的 bean 的处理机制
  18. MySQL学习笔记:少用Null
  19. SQL Server中将多行数据拼接为一行数据并且有特殊字符
  20. 框架----Django之Ajax全套实例(原生AJAX,jQuery Ajax,“伪”AJAX,JSONP,CORS)

热门文章

  1. Https socket 连接
  2. go语言从例子开始之Example24.通道同步
  3. Codeforces 1183H DP 计算子序列数目
  4. tomcat的server.xml配置
  5. Electron-vue实战(三)— 如何在Vuex中管理Mock数据
  6. Java中Calendar类的常用方法(对时间进行计算的类)
  7. Android processDebugManifest 异常
  8. mac终端命令--常用快捷键
  9. linux软件包rpm的使用
  10. sed编辑器基础