基于Spring AOP @AspectJ进阶说明

网友投稿 430 2023-02-18

基于Spring AOP @AspectJ进阶说明

基于Spring AOP @AspectJ进阶说明

@AspectJ可以使用切点函数定义切点,我们还可以使用逻辑运算符对切点进行复核运算得到复合的切点,为了在切面中重用切点,我们还可以对切点进行命名,以便在其他的地方引用定义过的切点。

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

Waiter接口:

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配置文件:

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-3.0.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-3.0.xsd">

1、切点符合运算

使用切点符合运算符,我们将拥有强大而灵活的切点表达能力。

TestAspect:切点符合运算定义切面

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");

}

输出结果:

NaiveWaiter:greet to John...

--greetToFun() executed!--

--waiterOrSeller() executed!--

NaiveWaiter:serving to John...

--waiterOrSeller() executed!--

NaughtyWaiter:greet to Tom...

--greetToFun() executed!--

--waiterOrSeller() executed!--

--notServeInNaiveWaiter() executed!--

NaughtyWaiter:serving Tom...

--waiterOrSeller() executed!--

2、命名切点

切点直接声明在增强方法处被称为匿名切点,匿名切点只能在声明处使用。如果希望在其他地方重用一个切点,我们可以通过@Pointcut注解以及切面类方法对切点进行命名。

TestNamePointcut:命名切点类

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);

}

输出结果:

--pkgGreetToNotnaiveWaiter() executed!--

NaiveWaiter:smile to Andy2times...

3、增强织入的顺序

一个连接点可以同时匹配多个切点,切点对应的增强在连接点上的织入顺序的安排主要有以下3种情况:

1)如果增强在同一个切面类中声明,则依照增强在切面类中定义的顺序进行织入;

2)如何增强位于不同的切面类中,且这些切面类都实现了org.springframework.core.Order接口,则由接口方法的顺序号决定(顺序号小的先织入);

3)如果增强位于不同的切面类中,且这些切面类没有实现org.springframework.core.Order接口,织入的顺序是不确定的。

4、访问连接点信息

AspectJ使用org.aspectj.lang.JoinPoint接口表示目标类连接点对象,如果是环绕增强时,使用org.aspectj.lang.ProceedingJoinPoint表示连接点对象,该类是JoinPoint的子接口,任何一个增强方法都可以通过将第一个入参声明为JoinPoint访问到连接点上下文的信息。

TestAspect3:切面实现类

@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");

}

输出结果:

---joinPointAccess---

args[0]:Andy

signature:class com.yyq.aspectJAdvanced.NaiveWaiter

NaiveWaiter:greet to Andy...

---joinPointAccess---

5、绑定连接点方法入参

args()用于绑定连接点方法的入参;@annotation()用于绑定连接点方法的注解对象;而@args()用于绑定连接点方法入参的注解。

TestAspect4:切面实现类

@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);

}

输出结果:

---bindJoinPointParams---

name:Andy

num:3

---bindJoinPointParams---

NaiveWaiter:smile to Andy 3 times...

6、绑定代理对象

使用this()或target()可绑定被代理对象实例,在通过类实例名绑定对象时,还依然具有原来连接点匹配的功能,只不过类名是通过增强方法中同名入参的类型间接决定罢了。

TestAspect5:切面实现类

@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");

}

输出结果:

---bindProxyObj---

com.yyq.aspectJAdvanced.NaiveWaiter$$EnhancerByCGLIB$$fefafe52

---bindProxyObj---

NaiveWaiter:greet to Yang...

7、绑定类注解对象

@within()和@target()函数可以将目标类的注解对象绑定到增强方法中,我们通过@within()演示注解绑定的操作。

TestAspect6:切面测试类

@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");

}

输出结果:

---bindTypeAnnoObject---

$Proxy4

---bindTypeAnnoObject---

NaiveWaiter:greet to Yang...

8、绑定返回值

在后置增强中,我们可以通过returning绑定连接点方法的返回值。

TestAspect7:切面实现类

@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");

}

输出结果:

SmartSeller: sell Beer to John...

---bindReturnValue---

returnValue:100

---bindReturnValue---

9、绑定抛出的异常

和通过切点函数绑定连接点信息不同,连接点抛出的异常必须使用AfterThrowing注解的throwing成员进行绑定。

TestAspect8:切面实现类

@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);

}

输出结果:

---bindException---

exception:iae Exception

---bindException---

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:在Mybatis中使用自定义缓存ehcache的方法
下一篇:解决SpringMVC项目连接RabbitMQ出错的问题
相关文章

 发表评论

暂时没有评论,来抢沙发吧~