博客
关于我
spring 基于注解实现Aop
阅读量:396 次
发布时间:2019-03-05

本文共 3805 字,大约阅读时间需要 12 分钟。

一.基于spirng的注解实现Aop的开发步骤

 

1.1 在pom文件中添加依赖

junit
junit
4.11
test
org.springframework
spring-context
5.0.2.RELEASE
org.aspectj
aspectjweaver
1.8.7

1.2 创建目标类

1. iAccountService接口

package com.ljf.spring.aop.anno.service;public interface IAccountService {    /**     * 模拟保存账户     */    void saveAccount();    /**     * 模拟更新账户     * @param i     */    void updateAccount(int i);    /**     * 删除账户     * @return     */    int  deleteAccount();}

2. 实现类

package com.ljf.spring.aop.anno.service.impl;import com.ljf.spring.aop.anno.service.IAccountService;import org.springframework.stereotype.Service;/** * @ClassName: AccountService * @Description: TODO * @Author: liujianfu * @Date: 2021/02/05 10:41:48  * @Version: V1.0 **/@Service("accountService")public class AccountService implements IAccountService {    @Override    public void saveAccount() {        System.out.println("执行了保存");        int i=1/0;    }    @Override    public void updateAccount(int i) {        System.out.println("执行了更新"+i);    }    @Override    public int deleteAccount() {        System.out.println("执行了删除");        return 0;    }}

 

1.3 创建切面类

在切面类中定义切入点、通知、切面

package com.ljf.spring.aop.anno.util;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.*;import org.springframework.stereotype.Component;/** * @ClassName: Logger * @Description: TODO * @Author: liujianfu * @Date: 2021/02/05 10:45:13  * @Version: V1.0 **/@Component("logger")@Aspect//表示当前类是一个切面类public class Logger {    //切入点    @Pointcut("execution(* com.ljf.spring.aop.anno.service.impl.*.*(..))")    private void pt1(){}   //作用是切点表达式的抽取    /**     * 前置通知     */  // @Before("pt1()")    public  void beforePrintLog(){        System.out.println("前置通知Logger类中的beforePrintLog方法开始记录日志了。。。");    }    /**     * 后置通知     *///    @AfterReturning("pt1()")    public  void afterReturningPrintLog(){        System.out.println("后置通知Logger类中的afterReturningPrintLog方法开始记录日志了。。。");    }    /**     * 异常通知     */    //@AfterThrowing("pt1()")    public  void afterThrowingPrintLog(){        System.out.println("异常通知Logger类中的afterThrowingPrintLog方法开始记录日志了。。。");    }    /**     * 最终通知     *///    @After("pt1()")    public  void afterPrintLog(){        System.out.println("最终通知Logger类中的afterPrintLog方法开始记录日志了。。。");    }    @Around("pt1()")    public Object aroundPringLog(ProceedingJoinPoint pjp){        Object rtValue = null;        try{            Object[] args = pjp.getArgs();//得到方法执行所需的参数            System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。前置");            rtValue = pjp.proceed(args);//明确调用业务层方法(切入点方法)            System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。后置");            return rtValue;        }catch (Throwable t){            System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。异常");            throw new RuntimeException(t);        }finally {            System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。最终");        }    }}

1.4 在xml配置文件中开启Aop注解

1.5 调用

package com.ljf.spring.aop.anno;import com.ljf.spring.aop.anno.service.IAccountService;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * Hello world! * */public class App {    public static void main(String[] args) {        //1.获取容器        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");        //2.获取对象        IAccountService as = (IAccountService)ac.getBean("accountService");        //3.执行方法        as.saveAccount();    }}

使用了环绕通知:

转载地址:http://lruzz.baihongyu.com/

你可能感兴趣的文章
Mysql学习总结(74)——慢SQL!压垮团队的最后一根稻草!
查看>>
Mysql学习总结(75)——并发量大、数据量大的互联网业务数据库设计军规
查看>>
Mysql学习总结(76)——MySQL执行计划(explain)结果含义总结
查看>>
Mysql学习总结(77)——温故Mysql数据库开发核心原则与规范
查看>>
Mysql学习总结(78)——MySQL各版本差异整理
查看>>
Mysql学习总结(79)——MySQL常用函数总结
查看>>
Mysql学习总结(7)——MySql索引原理与使用大全
查看>>
Mysql学习总结(80)——统计数据库的总记录数和库中各个表的数据量
查看>>
Mysql学习总结(81)——为什么MySQL不推荐使用uuid或者雪花id作为主键?
查看>>
Mysql学习总结(82)——MySQL逻辑删除与数据库唯一性约束如何解决?
查看>>
Mysql学习总结(83)——常用的几种分布式锁:ZK分布式锁、Redis分布式锁、数据库分布式锁、基于JDK的分布式锁方案对比总结
查看>>
Mysql学习总结(84)—— Mysql的主从复制延迟问题总结
查看>>
Mysql学习总结(85)——开发人员最应该明白的数据库设计原则
查看>>
Mysql学习总结(8)——MySql基本查询、连接查询、子查询、正则表达查询讲解
查看>>
Mysql学习总结(9)——MySql视图原理讲解与使用大全
查看>>
Mysql学习笔记 - 在Centos7环境下离线安装Mysql
查看>>
MySQL学习笔记十七:复制特性
查看>>
Mysql学习第一课-mysql的定义及sql语句
查看>>
mysql学号的字符长度_MYSQL--2
查看>>
mysql安全模式: sql_safe_updates
查看>>