Spring-AOP @AspectJ进阶之切点复合运算

文章目录

概述

@AspectJ可以使用切点函数定义切点,还可以使用逻辑运算符对切点进行复合运算得到复合切点。

为了在切面中重用切点,还可以对切点进行命名,以便在其他地方引用定义过的切点。

当一个连接点匹配多个切点时,需要考虑织入顺序的问题,另外一个重要的问题是如何在增强中访问连接点上下文的信息。


示例

代码已托管到Github—> https://github.com/yangshangwei/SpringMaster


这里写图片描述

这里仅仅展示关键代码

切面部分:

package com.xgj.aop.spring.advisor.aspectJAdvance.pointcutComplex;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

/**
 * 
 * 
 * @ClassName: PointcutComplexAspect
 * 
 * @Description: 标注了@Aspect的切面
 * 
 * @author: Mr.Yang
 * 
 * @date: 2017年9月9日 上午1:50:45
 */

@Aspect
public class PointcutComplexAspect {

	/**
	 * 
	 * 
	 * @Title: matchGreetTo
	 * 
	 * @Description: 与预算,匹配com.xgj.aop.spring.advisor.aspectJAdvance.
	 *               pointcutComplex包中所有greetTo()方法的切点
	 * 
	 * 
	 * @return: void
	 */
	@Before("within(com.xgj.aop.spring.advisor.aspectJAdvance.pointcutComplex.*)"
			+ " && execution(* greetTo(..))")
	public void matchGreetTo() {
		System.out.println("matchGreetTo executed,some logic is here ");
	}

	/**
	 * 
	 * 
	 * @Title: something
	 * 
	 * @Description: 非与预算,匹配所有的serverTo方法,且不位于WaiterOne目标类切点
	 * 
	 * 
	 * @return: void
	 */

	@After("!target(com.xgj.aop.spring.advisor.aspectJAdvance.pointcutComplex.WaiterOne)"
			+ " && execution(* serverTo(..))")
	public void something() {
		System.out.println("something executed,some logic is here ");
	}

	/**
	 * 
	 * 
	 * @Title: method
	 * 
	 * @Description: 或运算,匹配IWaiter和ISeller接口实现类所有连接点的切点
	 * 
	 * 
	 * @return: void
	 */

	@AfterReturning("target(com.xgj.aop.spring.advisor.aspectJAdvance.pointcutComplex.IWaiter)"
			+ " || target(com.xgj.aop.spring.advisor.aspectJAdvance.pointcutComplex.ISeller)")
	public void method() {
		System.out.println("method executed,some logic is here ");
	}

}

配置文件,Bean的初始化使用context:component-scan扫描

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop.xsd
		http://www.springframework.org/schema/context 
       	http://www.springframework.org/schema/context/spring-context.xsd">

<!-- (1)声明Context命名空间以及Schema文件   (2)扫描类包以及应用注解定义的bean -->
<context:component-scan base-package="com.xgj.aop.spring.advisor.aspectJAdvance.pointcutComplex"/>

<!-- 基于@AspectJ切面的驱动器 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
 
<!-- 使用了@AspectJ注解的切面类 -->
<bean class="com.xgj.aop.spring.advisor.aspectJAdvance.pointcutComplex.PointcutComplexAspect"/>

</beans>

测试类:

package com.xgj.aop.spring.advisor.aspectJAdvance.pointcutComplex;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class PointcutComplexAspectTest {

	@Test
	public void test() {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"com/xgj/aop/spring/advisor/aspectJAdvance/pointcutComplex/conf-pointcutComplex.xml");

		WaiterOne waiterOne = ctx.getBean("waiterOne", WaiterOne.class);
		WaiterTwo waiterTwo = ctx.getBean("waiterTwo", WaiterTwo.class);

		SellerOne sellerOne = ctx.getBean("sellerOne", SellerOne.class);
		SellerTwo sellerTwo = ctx.getBean("sellerTwo", SellerTwo.class);

		waiterOne.greetTo("XiaoGongJiang");
		System.out.println();

		waiterOne.serverTo("XiaoGongJiang");
		System.out.println();

		waiterTwo.greetTo("XiaoGongJiang");
		System.out.println();

		sellerOne.greetTo("XiaoGongJiang");
		System.out.println();

		sellerOne.greetTo("XiaoGongJiang");
		System.out.println();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小工匠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值