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

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

??Spring?AOP??????????

??????????????Spring AOP??????????????????????????AOP???

1. ??????POM??

???????????POM??????????????????Spring?AspectJ?

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

2. ??????????

??????????????????????

package com.ljf.spring.aop.service;public interface IAccountService {    void saveAccount();    void updateAccount(int i);    int deleteAccount();}
package com.ljf.spring.aop.service.impl;import org.springframework.stereotype.Service;@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;    }}

3. ???????????

??????AOP??????????????????????

package com.ljf.spring.aop.util;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.*;import org.springframework.stereotype.Component;@Component("logger")@Aspectpublic class Logger {    @Pointcut("execution(* com.ljf.spring.aop.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????????????");    }    @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??????????????");        }    }}

4. ??Spring XML?????AOP

?Spring?XML????????????AOP??????????

5. ????

???????????????????

package com.ljf.spring.aop;import com.ljf.spring.aop.service.IAccountService;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {    public static void main(String[] args) {        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");        IAccountService as = (IAccountService) ac.getBean("accountService");        as.saveAccount();    }}

??????????????????Spring AOP?????????????????????????????

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

你可能感兴趣的文章
OpenCV与AI深度学习 | 实战 | 文本图片去水印--同时保持文本原始色彩(附源码)
查看>>
OpenCV与AI深度学习 | 实战 | 通过微调SegFormer改进车道检测效果(数据集 + 源码)
查看>>
OpenCV与AI深度学习 | 实战—使用YOLOv8图像分割实现路面坑洞检测(步骤 + 代码)
查看>>
OpenCV与AI深度学习 | 实战篇——基于YOLOv8和OpenCV实现车速检测(详细步骤 + 代码)
查看>>
OpenCV与AI深度学习 | 实战|OpenCV实时弯道检测(详细步骤+源码)
查看>>
OpenCV与AI深度学习 | 实用技巧 | 使用OpenCV进行模糊检测
查看>>
OpenCV与AI深度学习 | 实践教程|旋转目标检测模型-TensorRT 部署(C++)
查看>>
OpenCV与AI深度学习 | 工业缺陷检测中数据标注需要注意的几个事项
查看>>
OpenCV与AI深度学习 | 干货 | 深度学习模型训练和部署的基本步骤
查看>>
OpenCV与AI深度学习 | 手把手教你用Python和OpenCV搭建一个半自动标注工具(详细步骤 + 源码)
查看>>
OpenCV与AI深度学习 | 水下检测+扩散模型:或成明年CVPR最大惊喜!
查看>>
OpenCV与AI深度学习 | 深入浅出了解OCR识别票据原理
查看>>
OpenCV与AI深度学习 | 深度学习检测小目标常用方法
查看>>
OpenCV与AI深度学习 | 超越YOLOv10/11、RT-DETRv2/3!中科大D-FINE重新定义边界框回归任务
查看>>
OpenCV与AI深度学习 | 高效开源的OCR工具:Surya-OCR介绍与使用
查看>>
OpenCV与AI深度学习|16个含源码和数据集的计算机视觉实战项目(建议收藏!)
查看>>
Opencv中KNN背景分割器
查看>>
OpenCV中基于已知相机方向的透视变形
查看>>
OpenCV中的监督学习
查看>>
opencv中读写视频
查看>>