spring项目中使用XML方式配置AOP切面

在XML中配置

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       https://www.springframework.org/schema/aop/spring-aop.xsd">



    <bean id="interceptorAop" class="com.hwxx.common.aop.InterceptorAop"></bean>
    <aop:config>
       <!-- 定义一个切入点 , 拦截所有的方法-->
        <aop:pointcut id="pointcut" expression="execution(public * *(..))"/>  <!-- 切入全部的方法-->
<!--   <aop:pointcut id="pointcut" expression="@annotation(org.springframework.web.bind.annotation.RequestMapping)"/> -->

   <!--  切面开始, 参数是 目标类中的有通知方法 -->
        <aop:aspect ref="interceptorAop">
<!--            前置增强
            <aop:before method="before" pointcut-ref="pointcut"></aop:before>
           		后置运行后通知
            <aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"></aop:after-returning>     
             	后置异常通知
             <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="e"/>
             	后置结束通知
             <aop:after method="after" pointcut-ref="pointcut"/> -->
             <!-- 环绕通知 -->
     <!--       <aop:around method="aroundAdvise" pointcut-ref="pointcut"/> -->
        </aop:aspect>
    </aop:config>
</beans>

2.必须要指定spring 的加载

3.最后配置切面类

 

package com.hwxx.common.aop;

import org.aspectj.lang.JoinPoint;		
import org.aspectj.lang.ProceedingJoinPoint;
import java.util.Arrays;

/**
 * 包含aop 的增强管理 切面
 *  * @author 
 * 2022-3-13
 */
public class InterceptorAop {
    public void before(JoinPoint jp){
        System.out.println("前置切面AOP!!!!" + jp.getTarget() + 
        		"等等待" + jp.getSignature().getName()
        + "方法参数是: " + Arrays.toString(jp.getArgs()));
    }
    public void afterReturning(JoinPoint jp, Object result){
        System.out.println("后置切面是AOP!!" + jp.getTarget() + ""
        		+ "d的顶顶顶" + jp.getSignature().getName()
        + " 方法返回值: " + result);
    }
    public void afterThrowing(JoinPoint jp , Exception e) {
    	System.out.println("这是一个异常通知");
    	System.out.println("调用了" + jp.getTarget() + "的" + 
    	jp.getSignature().getName() + "方法 发生了异常" + e.getMessage() );    	
    	System.out.println(e.getMessage() + "这是发生的异常");
	}
        
    public void after(JoinPoint jp) {
    	System.out.println("这个是一个后置通知after " + 
    	jp.getTarget() + "噢噢噢噢" + jp.getSignature().getName());
	}

    public Object aroundAdvise(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("环绕通知前置 切面AOP!!!!" +  jp.getTarget() + 
        		"目标对象:" + jp.getSignature().getName()
        + "当前方法的方法参数是: " + Arrays.toString(jp.getArgs()));
        Object result;
        try {	
			result = jp.proceed();
			System.out.println("方法执行后!!" + jp.getTarget() + "d的顶顶顶" + jp.getSignature().getName()
			        + " 方法返回值: " + result);
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
	    	System.out.println("调用了" + jp.getTarget() + "的" 
			+ jp.getSignature().getName() + "方法 发生了异常" + e.getMessage() );    	
			throw e;
		}finally {
			System.out.println("这个是一个后置通知after " + jp.getTarget() 
			+ "噢噢噢噢" + jp.getSignature().getName() + "后置通知执行完毕");
		}
        return result;
	}
}

还有另一种方法: 就是在类中加注解方式更加简单一些!  以后再写

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值