博客
关于我
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之模糊处理
查看>>
Opencv介绍及opencv3.0在 vs2010上的配置
查看>>
OpenCV使用霍夫变换检测图像中的形状
查看>>
opencv保存图片路径包含中文乱码解决方案
查看>>
OpenCV保证输入图像为三通道
查看>>
OpenCV入门教程(非常详细)从零基础入门到精通,看完这一篇就够了
查看>>
opencv图像分割2-GMM
查看>>
opencv图像分割3-分水岭方法
查看>>
opencv图像切割1-KMeans方法
查看>>
OpenCV图像处理篇之阈值操作函数
查看>>
opencv图像特征融合-seamlessClone
查看>>
OpenCV图像的深浅拷贝
查看>>
OpenCV在Google Colboratory中不起作用
查看>>
OpenCV学习(13) 细化算法(1)(转)
查看>>
OpenCV学习笔记(27)KAZE 算法原理与源码分析(一)非线性扩散滤波
查看>>
OpenCV学堂 | CV开发者必须懂的9种距离度量方法,内含欧氏距离、切比雪夫距离等(建议收藏)
查看>>
OpenCV学堂 | OpenCV中支持的人脸检测方法整理与汇总
查看>>
OpenCV学堂 | OpenCV案例 | 基于轮廓分析对象提取
查看>>
OpenCV学堂 | YOLOv8与YOLO11自定义数据集迁移学习效果对比
查看>>
OpenCV学堂 | YOLOv8官方团队宣布YOLOv11 发布了
查看>>