Java中详述Spring AOP(下)

切面优先级

果一个方法匹配多个切面中相同类型增强方法,那么必须明确指定各个切面优先级,否则切面按照类名字典顺序执行,切面优先级的确定既可以通过在切面类添加@Order注解或实现Ordered接口实现,也可以在XML配置aop:aspect标签的order属性来实现。

@Order注解在这里插入图片描述

此时出现了两个切面,我们给他相同类型增强方法,代码展示如下。
AAspect类:

package com.jd.aspect;

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

@Aspect
@Component
public class AAspect {

    @Pointcut("execution(public int com.jd.calculator.service.CalculatorService.*(..))")
    public void pointcut(){
    }

    @Before("pointcut()")
    public void before(JoinPoint jp){
        System.out.println("@@@@@@@@@@@@@@");
    }

}

CalculatorAspect 类代码展示:

package com.jd.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class CalculatorAspect {

    @Pointcut("execution(public int com.jd.calculator.service.CalculatorService.*(..))")
    public void pointcut(){
    }

    @Before("pointcut()")
    public void before(JoinPoint jp){
        System.out.println("#########");
    }
}

结果展示为:
在这里插入图片描述
将AAscept改为ZAscept后,
在这里插入图片描述
结果展示为:
在这里插入图片描述

我们也可以使用 @Order注解指定切面优先级:
ZAspect类代码展示:

package com.jd.aspect;

import org.aspectj.lang.JoinPoint;
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;

@Order(0)
@Aspect
@Component
public class ZAspect {

    @Pointcut("execution(public int com.jd.calculator.service.CalculatorService.*(..))")
    public void pointcut(){
    }

    @Before("pointcut()")
    public void before(JoinPoint jp){
        System.out.println("@@@@@@@@@@@@@@");
    }

}

CalculatorAspect 类代码展示:

package com.jd.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;


@Order(1)
@Aspect
@Component
public class CalculatorAspect {

    @Pointcut("execution(public int com.jd.calculator.service.CalculatorService.*(..))")
    public void pointcut(){
    }

    @Before("pointcut()")
    public void before(JoinPoint jp){
        System.out.println("#########");
    }

}

结果展示为:在这里插入图片描述
通过观察上面的结果我们不难发现,两者的结果输出调换了顺序,这是由于@Order注解起了决定性作用。

XML文件配置

XML配置aop:aspect标签的order属性来实现。
application.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">
     
	<bean class="com.jd.calculator.CalculatorService"></bean>
	
	<bean id="zAspect" class="com.jd.calculator.ZAspect"></bean>
	<bean id="calculatorAspect" class="com.jd.calculator.CalculatorAspect"></bean>
	
	<!-- 配置AOP -->
	<aop:config proxy-target-class="true">
		<!-- 配置切点表达式 -->
		<aop:pointcut expression="execution(public int com.jd.calculator.CalculatorService.*(..))" id="pointcut"/>
		<!-- 配置切面及增强类型 -->
		<aop:aspect ref="zAspect" order="0">
			<aop:before method="before" pointcut-ref="pointcut"/>
		</aop:aspect>
		
		<aop:aspect ref="calculatorAspect" order="1">
			<aop:before method="before" pointcut-ref="pointcut"/>
		</aop:aspect>
	</aop:config>
</beans>

结果显示为:
在这里插入图片描述
与预计结果相同。

总结

AOP的主要编程对象是切面(aspect),而切面横切关注点。
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值