spring aop的使用(注解与xml)

本文深入解析Spring AOP的原理及应用,包括动态代理机制、注解使用、XML配置方式等,帮助开发者理解如何将业务功能与非业务功能分离,提高程序的可维护性和扩展性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、spring aop介绍

spring aop可以方便地将业务功能与非业务功能分离,大大降低了程序之间的耦合性;同时它也为系统功能升级提供便利,因为我们可以不触碰原有功能模块,实现现有功能与原有功能的连接。spring通过动态代理实现aop,在高版本中,框架可自动根据当前被代理类选择使用基于接口的动态代理还是基于子类(类)的动态代理;也可手动指定动态代理的方式。

在java中,所有的公有方法(函数)都可以作为连接点(join point),切点(pointcut)就是一个执行通知(advice)的条件,通常使用一个空体方法来声明(在注解中),切面(Aspect)是一个类,它是由pointcut和advice组成;

二、aop注解

@Aspect: 声明一个切面类,放在类上

@Before  增强(通知)方法,在执行pointcut之前运行

@After 增强(通知)方法,在执行pointcut之后运行

@AfterReturning 增强(通知)方法,在执行pointcut后返回结果执行

@AfterThrowing 增强(通知)方法,在执行pointcut时出现异常时执行

@Around: 增强(通知)方法,在执行pointcut之前和之后执行

@Pointcut 定义通知执行的条件(切点表达式)

三、aop注解使用

需求: 我们需要在核心业务类Bussines中进行非核心业务日志开发

核心业务类

@Component("business")
public class Business {
    //add
    public void add() {
        System.out.println("add method!");
    }
}

非核心业务类

@Component("logginAspectJ")
@Aspect
public class LogginAspectJ {
    @Pointcut("execution(* cn.hidm.aop.service.Business.*(..))")
    public void PointcutDeclaration() {}

    @Before("PointcutDeclaration()")  //通知中的value值是声明切点的方法
    public void BeforeMethod(JoinPoint jp) {    
        String methodName = jp.getSignature().getName();
        Object[] args = jp.getArgs();
        System.out.println("BeforeMethod  The method   "+ methodName +"   parameter is  "+ Arrays.asList(args));
        System.out.println("add before");
        System.out.println();
    }

    @After("PointcutDeclaration()")
    public void AfterMethod(JoinPoint jp) {
        String methodName = jp.getSignature().getName();
        Object[] args = jp.getArgs();
        System.out.println("AfterMethod  The method    "+ methodName +"   parameter is  "+Arrays.asList(args));
        System.out.println();
    }

    @AfterReturning(value="PointcutDeclaration()",returning="result")
    public void AfterReturningMethod(JoinPoint jp,Object result) {
        String methodName = jp.getSignature().getName();
        Object[] args = jp.getArgs();
        System.out.println("AfterReturningMethod  The method   "+ methodName +"   parameter is  "+Arrays.asList(args)+" "+result);
        System.out.println();
    } 

    @AfterThrowing(value="PointcutDeclaration()",throwing="e")
    public void AfterThrowingMethod(JoinPoint jp,Exception e) {
        String methodName = jp.getSignature().getName();
        System.out.println("AfterThrowingMethod  The method   "+ methodName +"exception :"+e);
    }
}
 

测试类

public class AopTest {
    @Test
    public void test() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Business arithmetic = (Business) context.getBean("business");
        arithmetic.add();
        context.close();
    }
}

四、aop的xml使用

原业务类

public class FunctionMethod {
    
    public FunctionMethod() {
        super();
    }

    public void method1() {
        System.out.println("业务方法1");
    }
}

切面类

public class MyAdvice {
    
    public MyAdvice() {
        super();
    }

    public void adviceBefore() {
        System.out.println("前通知运行了");
    }
    
    public void adviceAfter() {
        System.out.println("后通知运行了");
    }
}

xml配置

<aop:config>
        <!-- 配置切入点 expression填写切入点表达式 -->
        <aop:pointcut
            expression="execution(* cn.hidm.aopxml.service.FunctionMethod.method1(..))"
            id="pointcut" />

        <!-- 配置切面 切面是切入点和通知的结合 -->
        <aop:aspect ref="myAdvise">

            <!-- 指定名为before方法作为前置通知 -->
            <aop:before method="adviceBefore" pointcut-ref="pointcut" />
            <aop:around method="adviceAfter" pointcut-ref="pointcut" />
        </aop:aspect>
    </aop:config>

测试类

@Test
    public void test1() {
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        FunctionMethod functionMethod = (FunctionMethod)ac.getBean("functionMethod");
        functionMethod.method1();
        ac.close();
    }

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值