package com.zgq.aspect;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
public class ObjectAspect {
//写通知,前置通知
public void methodBegin(JoinPoint joinpoint){
//获取签名参数:joinpoint传递参数的形式
Signature signature = joinpoint.getSignature();
//方法传递的实际参数
Object[] args = joinpoint.getArgs();
System.out.println("【前置通知】执行了方法:【"+signature.getName()+"】参数列表为:"+Arrays.asList(args));
}
//返回通知
public void methodReturn(JoinPoint joinpoint,Object result){
//获取签名参数:joinpoint传递参数的形式
Signature signature = joinpoint.getSignature();
String methodName =signature.getName();
//
System.out.println("【返回通知】执行了方法:【"+methodName+"】返回结果:"+result);
}
//异常通知
public void methodThrow(JoinPoint joinpoint,ArithmeticException throwable){
//获取签名参数:joinpoint传递参数的形式
String methodName = joinpoint.getSignature().getName();
String message = throwable.getMessage();
//
System.out.println("【异常通知】执行了方法:【"+methodName+"】返回异常信息为:"+message);
}
//后置通知
public void methodFinallEnd(JoinPoint joinpoint){
//获取签名参数:joinpoint传递参数的形式
Signature signature = joinpoint.getSignature();
String methodName =signature.getName();
//
System.out.println("【后置通知】执行了方法:【"+methodName+"】");
}
//环绕通知
public Object methodAround(ProceedingJoinPoint joinPoint){
//获取签名参数:joinpoint传递参数的形式
Signature signature = joinPoint.getSignature();
String methodName =signature.getName();
//获取参数
Object[] args = joinPoint.getArgs();
System.out.println("【环绕通知】执行了方法:【"+methodName+"】开始执行参数:"+Arrays.asList(args));
Object result = null;
try {
result = joinPoint.proceed();
//执行result表示方法执行结束
System.out.println("【环绕通知】执行了方法:【"+methodName+"】正常结束:结果"+result);
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("【环绕通知】执行了方法:【"+methodName+"】抛出异常 ===》:结果"+e.getMessage());
}finally {
System.out.println("【环绕通知】执行了方法:【"+methodName+"】最终结束:结果"+result);
}
return result;
}
}
配置文件:
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
<!-- 创建通知类对象 实现类-->
<bean id="userManage" class="com.zgq.dao.impl.UserManagerImpl"></bean>
<bean id="animaImpl" class="com.zgq.dao.impl.AnimalImpl"></bean>
<!-- 创建通知类对象 -->
<bean id="objAspect" class="com.zgq.aspect.ObjectAspect"></bean>
<!-- 切面配置 -->
<aop:config>
<!-- 切点配置 -->
<aop:pointcut expression="execution(* com.zgq.dao.*.*(..))" id="pc"/>
<!-- 切面的通知引入 -->
<aop:aspect ref="objAspect">
<aop:before method="methodBegin" pointcut-ref="pc"/>
<aop:after-returning method="methodReturn" pointcut-ref="pc" returning="result"/>
<aop:after-throwing method="methodThrow" pointcut-ref="pc" throwing="throwable"/>
<aop:after method="methodFinallEnd" pointcut-ref="pc"/>
<aop:around method="methodAround" pointcut-ref="pc"/>
</aop:aspect>
</aop:config>
测试:
@Test
public void test6(){
//获取配置spring环境
ApplicationContext acf = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取接口对象
UserManager userManageI = acf.getBean("userManage",UserManager.class);
userManageI.addUser("ll", "123");
}