设计模式之(动态)代理模式
发布日期:2021-05-10 02:44:57 浏览次数:21 分类:精选文章

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

OOP���AOP���������

OOP���������������������������AOP���������������������������������������������������������������������OOP������������������������������������������������AOP������������������������������������������������������������������������������������������������

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

���������������������

���Spring������������������������������AOP������������������������������������������������������������������

<.component-scan base-package="com.springboot">
@Repositorypublic interface StudentDao {    void insert(Student student);    void delete(int id);}@Repositorypublic class StudentDaoImpl implements StudentDao {    @Override    public void insert(Student student) {        System.out.println("������������������");    }    @Override    public void delete(int id) {        System.out.println("������������������");    }}
@Component@Aspectpublic class CheckSecurityAop {    @Order(1)    @Before("execution(* com.springboot.StudentDaoImpl.delete(..))")    public void checkSecurity() {        System.out.println("������������������");    }}
public class Client {    public static void main(String[] args) {        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");        StudentDao studentDao = context.getBean("studentDaoImpl", StudentDao.class);        studentDao.delete(1);    }}

@Before���@After���������������

  • @Before������������������������������������������������������������������������������������������������������������������������������������������������������������������
  • @After������������������������������������������������������������������������������������������������������������������������������������������������@After���������������������
  • @Around���������������������������@Before���@After���@AfterReturning���@AfterThrowing���������������������������������������������������������
  • @AfterReturning���������������������������������������������������������������������������������������������������������������������������������
  • @AfterThrowing���������������������������������������������������������������������������������������������������������

������������@Around���������������

@Around("execution(* com.springboot.ArithmeticCalculator.add(int,int))")public Object aroundMethod(ProceedingJoinPoint joinPoint) {    try {        Object result = joinPoint.proceed();        return result;    } catch (Throwable e) {        throw new RuntimeException("������������������", e);    } finally {        System.out.println("������������������");    }}

������������

���������������������������������������������������������������result������������������������������������������������������������������������������������null������������������������������������������������������������try...catch������������������������������������������������������������������������null���

������������������������������������@AfterThrowing��������������������������������������������������������������� throwable ���������������������������

������������������AOP������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

上一篇:反射原理详解
下一篇:设计模式之(静态)代理模式

发表评论

最新留言

感谢大佬
[***.8.128.20]2025年04月08日 16时15分19秒