Spring-AOP @AspectJ进阶之切点复合运算
【摘要】
文章目录
概述示例
概述
@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 ");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
配置文件,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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
测试类:
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();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
文章来源: artisan.blog.csdn.net,作者:小小工匠,版权归原作者所有,如需转载,请联系作者。
原文链接:artisan.blog.csdn.net/article/details/77914615
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)