<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan base-package="com.wangfei"/>
<aop:aspectj-autoproxy/>
</beans>- package com.wangfei.aspectj.zhushi;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class MyAspect {
@Pointcut("execution(* com.wangfei.jdk.*.*(..))")
private void myPointCut(){}
@Before("myPointCut()")
public void myBefore(JoinPoint joinPoint){
System.out.print("前置通知:模拟权限检查");
System.out.print(" 目标类是:"+joinPoint.getTarget());
System.out.println(",被植入增强处理的目标方法是:"+joinPoint.getSignature().getName());
}
@AfterReturning(value="myPointCut()")
public void myAfterReturning(JoinPoint joinPoint){
System.out.print("后置通知:模拟记录日志");
System.out.println("被植入的目标方法为"+joinPoint.getSignature().getName());
}
@Around("myPointCut()")
public Object myAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
System.out.println("环绕开始:开始目标方法前,模拟开启事务。。。");
Object obj=proceedingJoinPoint.proceed();
System.out.println("环绕结束:执行目标方法,模拟关闭事务");
return obj;
}
@AfterThrowing(value="myPointCut()",throwing="e")
public void myAfterThrowing(JoinPoint joinPoint,Throwable e){
System.out.println("异常通知:出错了"+e.getMessage());
}
@After("myPointCut()")
public void myAfter(JoinPoint joinPoint){
System.out.println("最终通知:模拟方法结束,释放资源");
}
} - package com.wangfei.jdk;
import org.springframework.stereotype.Repository;
@Repository("useDao")
public class UseDaoImpl implements UseDao{
@Override
public void addUser() {
// TODO Auto-generated method stub
System.out.println("add/");
}
@Override
public void deleteUser() {
// TODO Auto-generated method stub
System.out.println("delete//");
}
}