1.扫包
<context:component-scan base-package="xunfang"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
2.替换service
@Service("userService")
public class UserServiceImpl implements UserService{
public void add() {
System.out.println("添加1");
}
public void delete() {
System.out.println("删除1");
}
public void update() {
System.out.println("改动1");
}
@Override
public String selete() {
return "查询1";
}
}
3.替换切面
@Component
@Aspect
public class MyAspect5 implements MyAspectDao {
// <aop:pointcut id="addPointcut" expression="execution(* xunfang.service.impl.*.add*(..))"/>
// <aop:pointcut id="deletePointcut" expression="execution(* xunfang.service.impl.*.delete*(..))"/>
// <aop:pointcut id="updatePointcut" expression="execution(* xunfang.service.impl.*.update*(..))"/>
// <aop:pointcut id="seletePointcut" expression="execution(* xunfang.service.impl.*.selete*(..))"/>
// 公共切入点
@Pointcut("execution(* xunfang.service.impl.*.add*(..))")
public void addPointcut(){}
@Pointcut("execution(* xunfang.service.impl.*.delete*(..))")
public void deletePointcut(){}
@Pointcut("execution(* xunfang.service.impl.*.update*(..))")
public void updatePointcut(){}
@Pointcut("execution(* xunfang.service.impl.*.selete*(..))")
public void seletePointcut(){}
@Override
//<aop:before method="before" pointcut-ref="addPointcut"></aop:before>
@Before("addPointcut()")
public void before(JoinPoint joinPoint) {
System.out.println("前置通知");
}
@Override
//<aop:after-returning method="afterReturning" pointcut-ref="deletePointcut" returning="ret"></aop:after-returning>
@AfterReturning(value = "deletePointcut()",returning = "ret")
public void afterReturning(JoinPoint joinPoint, Object ret) {
System.out.println("后置通知");
}
@Override
//<aop:around method="around" pointcut-ref="updatePointcut"></aop:around>
@Around("updatePointcut()")
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("前环绕");
Object o=proceedingJoinPoint.proceed();
System.out.println("后环绕");
return o;
}
@Override
//<aop:after-throwing method="afterThrowing" pointcut-ref="seletePointcut" throwing="throwable"></aop:after-throwing>
@AfterThrowing(value = "seletePointcut()",throwing = "throwable")
public void afterThrowing(JoinPoint joinPoint, Throwable throwable) {
System.out.println("异常通知");
}
@Override
//<aop:after method="after" pointcut-ref="seletePointcut"></aop:after>
@After("seletePointcut()")
public void after() {
System.out.println("最终通知");
}
}
4.测试
@Test
public void test1(){
ApplicationContext context =
new ClassPathXmlApplicationContext("AspectJ2.xml");
UserService userService =
(UserService) context.getBean("userService");
System.out.println("------------------------------------------------");
userService.add();
System.out.println("------------------------------------------------");
userService.delete();
System.out.println("------------------------------------------------");
userService.update();
System.out.println("------------------------------------------------");
userService.selete();
System.out.println("------------------------------------------------");
}
5.结果
------------------------------------------------
前置通知
添加1
------------------------------------------------
删除1
后置通知
------------------------------------------------
前环绕
改动1
后环绕
------------------------------------------------
最终通知
------------------------------------------------
上一篇的注解写法