SpringAOP的实现(六)
发布日期:2021-05-10 07:59:18 浏览次数:17 分类:精选文章

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

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

������

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


1. ������������

package com.qfedu.aop06;
public interface IUserService {
List getAllUser();
boolean saveUser(Object user);
boolean deleteUser(int uid);
boolean updateUser(Object obj);
}

2. ���������

package com.qfedu.aop06;
import org.springframework.stereotype.Component;
import java.util.List;
@Component("us")
public class UserServiceImpl implements IUserService {
@Override
public List getAllUser() {
System.out.println("------getAllUser------");
return null;
}
@Override
public boolean saveUser(Object user) {
System.out.println("------saveUser------");
return false;
}
@Override
public boolean deleteUser(int uid) {
System.out.println("------deleteUser------");
return false;
}
@Override
public boolean updateUser(Object obj) {
System.out.println("------updateUser------");
System.out.println(1/0); // ������������
return false;
}
}

3. ������������MyAspect���

package com.qfedu.aop06;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class MyAspect {
@Pointcut("execution(* com.qfedu.aop06.*.*(..))")
public void setAll() {
}
@Before("setAll()")
public void myBefore(JoinPoint jp) {
System.out.println("=============Before==============");
}
@After("setAll()")
public void myAfter(JoinPoint jp) {
System.out.println("=============After==============");
}
@Around("setAll()")
public Object myAround(ProceedingJoinPoint pjp) {
try {
System.out.println("=============Around-Before==============");
Object obj = pjp.proceed();
System.out.println("=============Around-After==============");
return obj;
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return null;
}
@AfterReturning(value = "setAll()", returning = "obj")
public void myReturn(JoinPoint jp, Object obj) {
System.out.println("===============Return==================" + obj);
}
@AfterThrowing(value = "setAll()", throwing = "e")
public void myThrow(JoinPoint jp, Throwable e) {
System.out.println("=================Throw=================" + e.getMessage());
}
}

4. Spring������������


5. ���������

package com.qfedu.aop06;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAOP06 {
@Test
public void testAOP06() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans_aop06.xml");
IUserService us = context.getBean("us", IUserService.class);
Object obj = new Object();
us.getAllUser();
us.saveUser(obj);
us.deleteUser(1);
us.updateUser(obj);
}
}

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

  • ������beans_aop06.xml���������������������com.qfedu.aop06������������������������������
  • ������@Component���@Aspect���������������������������
  • ���������������Spring���������������IUserService������������������������������������������AOP���������
  • MyAspect������������������������������������������������������������������������
  • ���������������������������������������AOP TECHNOLOGY������������������������������������������������������������������������������������������������������

    上一篇:SpringAOP的实现(七)
    下一篇:SpringAOP的实现(五)

    发表评论

    最新留言

    逛到本站,mark一下
    [***.202.152.39]2025年04月18日 08时58分51秒