Spring AOP @AspectJ进阶
@AspectJ可以使用切点函数定义切点,我们还可以使用逻辑运算符对切点进行复核运算得到复合的切点,为了在切面中重用切点,我们还可以对切点进行命名,以便在其他的地方引用定义过的切点。当一个连接点匹配多个切点时,需要考虑织入顺序的问题,此外一个重要的问题是如何再增强中访问连接点上下文的信息。
package com.yyq.aspectJAdvanced;
public interface Waiter {
void greetTo(String name);
void serveTo(String name);
}
NaiveWaiter实现类:
package com.yyq.aspectJAdvanced;
public class NaiveWaiter implements Waiter {
@Override
public void greetTo(String name) {
System.out.println("NaiveWaiter:greet to " + name + "...");
}
@Override
public void serveTo(String name) {
System.out.println("NaiveWaiter:serving to " + name + "...");
}
public void smile(String clientName,int times){
System.out.println("NaiveWaiter:smile to "+clientName+ times+"times...");
}
}
NaughtyWaiter实现类:
package com.yyq.aspectJAdvanced;
public class NaughtyWaiter implements Waiter {
public void greetTo(String clientName) {
System.out.println("NaughtyWaiter:greet to " + clientName + "...");
}
public void serveTo(String clientName) {
System.out.println("NaughtyWaiter:serving " + clientName + "...");
}
public void joke(String clientName, int times) {
System.out.println("NaughtyWaiter:play " + times + " jokes to " + clientName + "...");
}
}
Seller接口:
package com.yyq.aspectJAdvanced;
public interface Seller {
int sell(String goods, String clientName);
}
SmallSeller实现类:
package com.yyq.aspectJAdvanced;
public class SmartSeller implements Seller {
public int sell(String goods,String clientName) {
System.out.println("SmartSeller: sell "+goods +" to "+clientName+"...");
return 100;
} public void checkBill(int billId){
if(billId == 1) throw new IllegalArgumentException("iae Exception");
else throw new RuntimeException("re Exception");
}
}
beans.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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<aop:aspectj-autoproxy proxy-target-class="true"/>
<bean id="naiveWaiter" class="com.yyq.aspectJAdvanced.NaiveWaiter"/>
<bean id="naughtyWaiter" class="com.yyq.aspectJAdvanced.NaughtyWaiter"/>
<bean id="seller" class="com.yyq.aspectJAdvanced.SmartSeller"/>
<!--
<bean class="com.yyq.aspectJAdvanced.TestAspect"/> <bean class="com.yyq.aspectJAdvanced.TestAspect2"/>
<bean class="com.yyq.aspectJAdvanced.TestAspect3"/>
<bean class="com.yyq.aspectJAdvanced.TestAspect4"/>
<bean class="com.yyq.aspectJAdvanced.TestAspect5"/>
<bean id="naiveWaiter2" class="com.yyq.aspectJAdvanced.NaiveWaiter2"/>
<bean class="com.yyq.aspectJAdvanced.TestAspect6"/>
<bean class="com.yyq.aspectJAdvanced.TestAspect7"/>
<bean class="com.yyq.aspectJAdvanced.TestAspect8"/>
-->
</beans>
package com.yyq.aspectJAdvanced;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before; @Aspect
public class TestAspect {
//与非运算
@Before("!target(com.yyq.aspectJAdvanced.NaiveWaiter) && execution(* serveTo(..))")
public void notServeInNaiveWaiter(){
System.out.println("--notServeInNaiveWaiter() executed!--");
}
//与运算
@After("within(com.yyq.aspectJAdvanced.*) && execution(* greetTo(..))")
public void greetToFun(){
System.out.println("--greetToFun() executed!--");
}
//或运算
@AfterReturning("target(com.yyq.aspectJAdvanced.Waiter) || target(com.yyq.aspectJAdvanced.Seller)")
public void waiterOrSeller(){
System.out.println("--waiterOrSeller() executed!--");
}
}
测试方法:
@Test
public void pointAspectJTest() {
String configPath = "com\\yyq\\aspectJAdvanced\\beans.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath);
Waiter naiveWaiter = (Waiter) ctx.getBean("naiveWaiter");
Waiter naughtyWaiter = (Waiter) ctx.getBean("naughtyWaiter");
naiveWaiter.greetTo("John");
naiveWaiter.serveTo("John");
naughtyWaiter.greetTo("Tom");
naughtyWaiter.serveTo("Tom");
}
package com.yyq.aspectJAdvanced;
import org.aspectj.lang.annotation.Pointcut;
public class TestNamePointcut {
//通过注解方法inPackage()对该切点进行命名,方法可视域修饰符为private,表明该命名切点只能在本切面类中使用
@Pointcut("within(com.yyq.aspectJAdvaned.*)")
private void inPackage(){}
@Pointcut("execution(* greetTo(..))")
protected void greetTo(){}
@Pointcut("inPackage() and greetTo()")
public void inPkgGreetTo(){}
}
TestAspect2:切面实现类
package com.yyq.aspectJAdvanced;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class TestAspect2 {
@Before("TestNamePointcut.inPkgGreetTo()")
public void pkgGreetTo(){
System.out.println("--pkgGreetTo() executed!--");
}
@Before("target(com.yyq.aspectJAdvanced.NaiveWaiter) || TestNamePointcut.inPkgGreetTo()")
public void pkgGreetToNotnaiveWaiter(){
System.out.println("--pkgGreetToNotnaiveWaiter() executed!--");
}
}
测试方法:
@Test
public void pointAspectJTest2() {
String configPath = "com\\yyq\\aspectJAdvanced\\beans.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath);
NaiveWaiter naiveWaiter = (NaiveWaiter) ctx.getBean("naiveWaiter");
naiveWaiter.smile("Andy", 2);
}
@Aspect
public class TestAspect3 {
@Around("execution(* greetTo(..)) && target(com.yyq.aspectJAdvanced.NaiveWaiter)")
public void joinPointAccess(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("---joinPointAccess---");
System.out.println("args[0]:" + pjp.getArgs()[0]);
System.out.println("signature:" + pjp.getTarget().getClass());
pjp.proceed();
System.out.println("---joinPointAccess---");
}
}
测试方法:
@Test
public void pointAspectJTest3() {
String configPath = "com\\yyq\\aspectJAdvanced\\beans.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath);
Waiter naiveWaiter = (Waiter) ctx.getBean("naiveWaiter");
naiveWaiter.greetTo("Andy");
}
@Aspect
public class TestAspect4 {
@Before("target(com.yyq.aspectJAdvanced.NaiveWaiter) && args(name,num,..)")
public void bindJoinPointParams(int num, String name) {
System.out.println("---bindJoinPointParams---");
System.out.println("name:" + name);
System.out.println("num:" + num);
System.out.println("---bindJoinPointParams---");
}
}
测试方法:
@Test
public void pointAspectJTest4() {
String configPath = "com\\yyq\\aspectJAdvanced\\beans.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath);
NaiveWaiter naiveWaiter = (NaiveWaiter) ctx.getBean("naiveWaiter");
naiveWaiter.smile("Andy", 3);
}
@Aspect
public class TestAspect5 {
@Before("this(waiter)")
public void bindProxyObj(Waiter waiter){
System.out.println("---bindProxyObj---");
System.out.println(waiter.getClass().getName());
System.out.println("---bindProxyObj---");
}
}
测试方法:
@Test
public void pointAspectJTest5() {
String configPath = "com\\yyq\\aspectJAdvanced\\beans.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath);
Waiter waiter = (Waiter) ctx.getBean("naiveWaiter");
waiter.greetTo("Yang");
}
@Aspect
public class TestAspect6 {
@Before("@within(m)")
public void bindTypeAnnoObject(Monitorable m) {
System.out.println("---bindTypeAnnoObject---");
System.out.println(m.getClass().getName());
System.out.println("---bindTypeAnnoObject---");
}
}
测试方法:
@Test
public void pointAspectJTest6() {
String configPath = "com\\yyq\\aspectJAdvanced\\beans.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath);
Waiter waiter = (Waiter) ctx.getBean("naiveWaiter2");
((NaiveWaiter2)waiter).greetTo("Yang");
}
@Aspect
public class TestAspect7 {
@AfterReturning(value = "target(com.yyq.aspectJAdvanced.SmartSeller)", returning = "retVal")
public void bindReturnValue(int retVal) {
System.out.println("---bindReturnValue---");
System.out.println("returnValue:" + retVal);
System.out.println("---bindReturnValue---");
}
}
测试方法:
@Test
public void pointAspectJTest7() {
String configPath = "com\\yyq\\aspectJAdvanced\\beans.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath);
SmartSeller seller = (SmartSeller) ctx.getBean("seller");
seller.sell("Beer", "John");
}
@Aspect
public class TestAspect8 {
@AfterThrowing(value = "target(com.yyq.aspectJAdvanced.SmartSeller)", throwing = "iae")
public void bindException(IllegalArgumentException iae) {
System.out.println("---bindException---");
System.out.println("exception:" + iae.getMessage());
System.out.println("---bindException---");
}
}
测试方法:
@Test
public void pointAspectJTest8() {
String configPath = "com\\yyq\\aspectJAdvanced\\beans.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath);
SmartSeller seller = (SmartSeller) ctx.getBean("seller");
seller.checkBill(1);
}
Spring AOP @AspectJ进阶的更多相关文章
- Spring AOP + AspectJ annotation example
In this tutorial, we show you how to integrate AspectJ annotation with Spring AOP framework. In simp ...
- Spring AOP + AspectJ Annotation Example---reference
In this tutorial, we show you how to integrate AspectJ annotation with Spring AOP framework. In simp ...
- 关于 Spring AOP (AspectJ) 该知晓的一切
关联文章: 关于Spring IOC (DI-依赖注入)你需要知道的一切 关于 Spring AOP (AspectJ) 你该知晓的一切 本篇是年后第一篇博文,由于博主用了不少时间在构思这篇博文,加上 ...
- Spring学习(十八)----- Spring AOP+AspectJ注解实例
我们将向你展示如何将AspectJ注解集成到Spring AOP框架.在这个Spring AOP+ AspectJ 示例中,让您轻松实现拦截方法. 常见AspectJ的注解: @Before – 方法 ...
- 关于 Spring AOP (AspectJ) 你该知晓的一切
版权声明:本文为CSDN博主「zejian_」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明.原文链接:https://blog.csdn.net/javazej ...
- 关于 Spring AOP (AspectJ) 你该知晓的一切 (转)
出处:关于 Spring AOP (AspectJ) 你该知晓的一切
- Spring AOP AspectJ Pointcut Expressions With Examples--转
原文地址:http://howtodoinjava.com/spring/spring-aop/writing-spring-aop-aspectj-pointcut-expressions-with ...
- Spring AOP AspectJ
本文讲述使用AspectJ框架实现Spring AOP. 再重复一下Spring AOP中的三个概念, Advice:向程序内部注入的代码. Pointcut:注入Advice的位置,切入点,一般为某 ...
- spring AOP AspectJ 定义切面实现拦截
总结记录一下AOP常用的应用场景及使用方式,如有错误,请留言. 1. 讲AOP之前,先来总结web项目的几种拦截方式 A: 过滤器 使用过滤器可以过滤URL请求,以及请求和响应的信息,但是过 ...
随机推荐
- Uva11582 Colossal Fibonacci Numbers!(同余模定理+快速幂)
https://vjudge.net/problem/UVA-11582 首先明确,斐波那契数列在模c的前提下是有循环节的.而f[i] = f[i-1]+f[i-2](i>=2)所以只要有两个连 ...
- HTML5 学习06——Geolocation(地理定位)
HTML5 Geolocation(地理定位) HTML5 Geolocation API 用于获得用户的地理位置. 鉴于该特性可能侵犯用户的隐私,除非用户同意,否则用户位置信息是不可用的. HTML ...
- HTML5 学习01——浏览器问题、新元素
Internet Explorer 浏览器问题 问题:Internet Explorer 8 及更早 IE 版本的浏览器不支持HTML5的方式. <!--[if lt IE 9]> < ...
- Android如何实现茄子快传
Android如何实现茄子快传茄子快传是一款文件传输应用,相信大家都很熟悉这款应用,应该很多人用过用来文件的传输.它有两个核心的功能: 端到端的文件传输Web端的文件传输这两个核心的功能我们具体来分析 ...
- MySQL关于根据日期查询数据的sql语句
查询在某段日期之间的数据: select * from 数据表 where 时间字段名 BETWEEN '2016-02-01' AND '2016-02-05' 查询往前3个月的数据: selec ...
- ionic1页面切换动画卡顿优化
https://github.com/shprink/ionic-native-transitions https://www.npmjs.com/package/ionic-native-trans ...
- 每天一个linux命令(13):less命令
1.命令简介 less(less) 命令可以对文件或其它输出进行分页显示,与moe命令相似,但是比more命令要强大许多.应该说是linux正统查看文件内容的工具. 2.用法 less [选项].. ...
- jquery中选择器input:hidden和input[type=hidden]的区别
关于选择器:hidden的说明,在jquery说明文档中是这样说的:匹配所有不可见元素,或者type为hidden的元素.而[type=hidden]是查找所有type属性等于hidden的元素.两者 ...
- Docker入门 - 001 CentOS Docker 安装
Docker支持以下的CentOS版本: CentOS 7 (64-bit) CentOS 6.5 (64-bit) 或更高的版本 前提条件 目前,CentOS 仅发行版本中的内核支持 Docker. ...
- 【C#】解析C#中LING的使用
LING提供了一种从数据源中获取数据的方式,不同的语言已经形成了很多种关联的数据源.LING(Language Integrated Query,语言集成查询)提供一种通用的从不同的数据源中获取数据的 ...