Spring——AOP

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");
    }
   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值