AOP的执行时机

本文探讨了AOP(面向切面编程)的执行时机,通过一个实例详细解释了AOP切面的执行顺序。首先介绍了添加jar包和配置spring XML文件以生成动态代理对象的步骤,接着展示了ICalculatorService接口和实现类,以及两个切面MethodAspect和ArgAspect的创建。通过设置order属性控制切面执行顺序,当不设置order时,XML配置中的切面顺序决定了执行顺序,而注解方式则按切面类型字母顺序执行。

执行时机,也就是AOP的执行顺序嘛。了解AOP中aspect的执行顺序,有助于我们更好的使用AOP。

下面我们通过一个见到那的例子去了解AOP的执行顺序。

按照以下顺序使用AOP:

1、添加jar包:

2、添加spring 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:context="http://www.springframework.org/schema/context"
	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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
	<context:component-scan base-package="com.jd"></context:component-scan>

	<bean id="argaspect" class="com.jd.aspect.ArgAspect"></bean>
	<bean id="methodaspect" class="com.jd.aspect.MthodAspect"></bean>
	
	<aop:config>
		<aop:pointcut expression="execution(public int com.jd.calculator.service.CalculatorService.*(int,int))" id="pointcut"/>
		
            <aop:aspect ref="argaspect" order="2">
			<aop:before method="before" pointcut-ref="pointcut"/>
		</aop:aspect>

		<aop:aspect ref="methodaspect" order="1">
			<aop:before method="before" pointcut-ref="pointcut"/>
		</aop:aspect>
		
		
	</aop:config>
	
	
</beans>

3、创建ICalculatorService接口,和实现类CaluculatorService,代码分贝如下:

ICalculatorService代码:

package com.jd.calculator.service;

public interface ICalculatorService {
	
	int mul(int a, int b);
	
	int div(int a, int b);
}

CaluculatorService代码:

package com.jd.calculator.service;

public interface ICalculatorService {
	
	int mul(int a, int b);
	
	int div(int a, int b);
}

 

4、创建两个aspect(切面)

MethodAspect代码:

package com.jd.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;


public class MthodAspect {

	public void Pointcut(){
		
	}
	//前置增强,目标方法执行前执行
	public void before(JoinPoint jp) {
		Signature signature =jp.getSignature();
		String name = signature.getName();
		System.out.println("The "+name+" method begins.");
	}
}

 

ArgAspect代码如下:

package com.jd.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;


public class ArgAspect {

	public void Pointcut(){
		
	}
	//前置增强,目标方法执行前执行
	public void before(JoinPoint jp) {
		Object [] arges =jp.getArgs();
//		for (Object object : arges) {
//			System.out.println(object);
//		}
		Signature signature =jp.getSignature();
		String name = signature.getName();
		System.out.println("The "+name+" method rags:["+arges[0]+","+arges[1]+"]");
	}
}

5、创建test类

代码如下:

package com.jd.Test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jd.calculator.service.ICalculatorService;

public class test {

	public static void main(String[] args) {
		
		ClassPathXmlApplicationContext applacation = new ClassPathXmlApplicationContext("app.xml");
		ICalculatorService calculatorService = applacation.getBean(ICalculatorService.class);
		//System.out.println(calculatorService.div(8,2));
		calculatorService.div(8,2);
		applacation.close();
	}
}

输出结果:

我们可以看到:通过order已经实现了aspect的执行顺序的控制。那么我们不添加order时,其最根本的执行顺序是什么那?

我们去掉order,直接结果如下:

结论:在xml文件中,哪个aspect先配置就先执行那个aspect。如果是通过注解方式来实现aspect的话,按照aspect类型的字母顺序来执行

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值