基于Spring AOP @AspectJ进阶说明

论坛 期权论坛 脚本     
niminba   2021-5-23 02:56   992   0

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

<?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>

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");
 }
[\[\KCB[YN[CB[NKKX[\[\KCBZ]U]\[H[H [Y\ˋ OB ]B k.!khO σB/o\y%\ yc(!khyk/*:`&/nkhy#:/yam繣yc.zacyb#9c#z/n+o.y+yd#9d#yaiyc9gyah. B\\X{b! B]\HHBH\H\]NB\XBXX\\\XHYJ\]\HCBXX[S]\]\^[K [KKX[SKKHN[K []\\ K[YJ JN[K [KKX[SKKHNCBO OB ]Bby& B]\HHBH\H\]NB\BXX\X\J H[Y]HW^\W\XY[X[[\X[^H]\][\X[^ Y] N]\]\H ]\H X[Z]U]\N]\Y]X[NO OB ]B/; B]\HHBH\H\]NBKKX[SKKCBK^\K\XY[ Z]U]\ [[PP YYMLBKKX[SKKCBZ]U]\Y]X[ˋ OB ]B hO σB[ yd\ yal9c!!9hykb,9ho.y+{$y.&/[ y%9g8 B\\Xb!bnB]\HHBH\H\]NB\XBXX\\\XYJ[JHCBXX[\P[X[]XHJH[K [KKX[\P[XKKHN[K [K\ K[YJ JN[K [KKX[\P[XKKHNCBO OB ]Bby& B]\HHBH\H\]NB\BXX\X\ H[Y]HW^\W\XY[X[[\X[^H]\][\X[^ Y] N]\]\H ]\H X[Z]U]\N Z]U]\]]\KY]X[NO OB ]B/; B]\HHBH\H\]NBKKX[\P[XKKCBMBKKX[\P[XKKCBZ]U]\Y]X[ˋ OB ]B 8k// σBg*9d#ho..+{$y.&/]\[/繣yy://8 B\\X! B]\HHBH\H\]NB\XBXX\\\XY\]\[[YHH\ K^\K\XY[ \\H]\[H][CBXX[]\[YJ[][ H[K [KKX[]\[YKKKHN[K []\[YN ][ N[K [KKX[]\[YKKKHNCBO OB ]Bby& B]\HHBH\H\]NB\BXX\X\H[Y]HW^\W\XY[X[[\X[^H]\][\X[^ Y] N\\\H \\H X[\N\ Y\NH OB ]B/; B]\HHBH\H\]NB\\Y\BKKX[]\[YKKKCB]\[YNL BKKX[]\[YKKKO OB ]B xk9o n. σBd:`&/!l9k/繣y/k#;/繣y9o n.9on*Y\[[f:/c9k B\\X;b! B]\HHBH\H\]NB\XBXX\\\XY\[[YHH\ K^\K\XY[ \\H[HXYHCBXX[^[[Y\Y[^[XYJH[K [KKX[^[KKHN[K [^[ XYKY\Y JN[K [KKX[^[KKHNCBO OB ]Bby& B]\HHBH\H\]NB\BXX\X\ H[Y]HW^\W\XY[X[[\X[^H]\][\X[^ Y] N\\\H \\H X[\N\X[ JNO OB ]B/; B]\HHBH\H\]NBKKX[^[KKCB^[XYH^[BKKX[^[KKO OB ]B."...*.j;n#9&) 9.*c g&i&i&+/c.i"ze&z+%* 9aj9g,9{&'z-d9fx
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1060120
帖子:212021
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP