最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于Spring AOP @AspectJ進階說明

 更新時間:2021年01月20日 11:35:48   作者:我是一名老菜鳥  
這篇文章主要介紹了基于Spring AOP @AspectJ進階說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

@AspectJ可以使用切點函數(shù)定義切點,我們還可以使用邏輯運算符對切點進行復核運算得到復合的切點,為了在切面中重用切點,我們還可以對切點進行命名,以便在其他的地方引用定義過的切點。

當一個連接點匹配多個切點時,需要考慮織入順序的問題,此外一個重要的問題是如何再增強中訪問連接點上下文的信息。

Waiter接口:

package com.yyq.aspectJAdvanced;
public interface Waiter {
 void greetTo(String name);
 void serveTo(String name);
}

NaiveWaiter實現(xiàn)類:

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實現(xiàn)類:

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實現(xiàn)類:

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

輸出結(jié)果:

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:切面實現(xiàn)類

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

輸出結(jié)果:

--pkgGreetToNotnaiveWaiter() executed!--
NaiveWaiter:smile to Andy2times...

3、增強織入的順序

一個連接點可以同時匹配多個切點,切點對應的增強在連接點上的織入順序的安排主要有以下3種情況:

1)如果增強在同一個切面類中聲明,則依照增強在切面類中定義的順序進行織入;

2)如何增強位于不同的切面類中,且這些切面類都實現(xiàn)了org.springframework.core.Order接口,則由接口方法的順序號決定(順序號小的先織入);

3)如果增強位于不同的切面類中,且這些切面類沒有實現(xiàn)org.springframework.core.Order接口,織入的順序是不確定的。

4、訪問連接點信息

AspectJ使用org.aspectj.lang.JoinPoint接口表示目標類連接點對象,如果是環(huán)繞增強時,使用org.aspectj.lang.ProceedingJoinPoint表示連接點對象,該類是JoinPoint的子接口,任何一個增強方法都可以通過將第一個入?yún)⒙暶鳛镴oinPoint訪問到連接點上下文的信息。

TestAspect3:切面實現(xiàn)類

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

輸出結(jié)果:

---joinPointAccess---
args[0]:Andy
signature:class com.yyq.aspectJAdvanced.NaiveWaiter
NaiveWaiter:greet to Andy...
---joinPointAccess---

5、綁定連接點方法入?yún)?/h3>

args()用于綁定連接點方法的入?yún)ⅲ籃annotation()用于綁定連接點方法的注解對象;而@args()用于綁定連接點方法入?yún)⒌淖⒔狻?/p>

TestAspect4:切面實現(xiàn)類

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

輸出結(jié)果:

---bindJoinPointParams---
name:Andy
num:3
---bindJoinPointParams---
NaiveWaiter:smile to Andy 3 times...

6、綁定代理對象

使用this()或target()可綁定被代理對象實例,在通過類實例名綁定對象時,還依然具有原來連接點匹配的功能,只不過類名是通過增強方法中同名入?yún)⒌念愋烷g接決定罷了。

TestAspect5:切面實現(xiàn)類

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

輸出結(jié)果:

---bindProxyObj---
com.yyq.aspectJAdvanced.NaiveWaiter$$EnhancerByCGLIB$$fefafe52
---bindProxyObj---
NaiveWaiter:greet to Yang...

7、綁定類注解對象

@within()和@target()函數(shù)可以將目標類的注解對象綁定到增強方法中,我們通過@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");
 }

輸出結(jié)果:

---bindTypeAnnoObject---
$Proxy4
---bindTypeAnnoObject---
NaiveWaiter:greet to Yang...

8、綁定返回值

在后置增強中,我們可以通過returning綁定連接點方法的返回值。

TestAspect7:切面實現(xiàn)類

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

輸出結(jié)果:

SmartSeller: sell Beer to John...
---bindReturnValue---
returnValue:100
---bindReturnValue---

9、綁定拋出的異常

和通過切點函數(shù)綁定連接點信息不同,連接點拋出的異常必須使用AfterThrowing注解的throwing成員進行綁定。

TestAspect8:切面實現(xiàn)類

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

輸出結(jié)果:

---bindException---
exception:iae Exception
---bindException---

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

  • Spring中property-placeholder的使用與解析詳解

    Spring中property-placeholder的使用與解析詳解

    本篇文章主要介紹了Spring中property-placeholder的使用與解析詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • SpringBoot使用Validation包進行輸入?yún)?shù)校驗

    SpringBoot使用Validation包進行輸入?yún)?shù)校驗

    Spring Boot 自帶的 spring-boot-starter-validation 包支持以標準注解的方式進行輸入?yún)?shù)校驗,本文即關(guān)注 spring-boot-starter-validation 包所涵蓋的標準注解的使用、校驗異常的捕獲與展示、分組校驗功能的使用,以及自定義校驗器的使用,需要的朋友可以參考下
    2024-05-05
  • Java實現(xiàn)將漢字轉(zhuǎn)化為漢語拼音的方法

    Java實現(xiàn)將漢字轉(zhuǎn)化為漢語拼音的方法

    這篇文章主要介紹了Java實現(xiàn)將漢字轉(zhuǎn)化為漢語拼音的方法,實例演示了Java引用pinyin4j庫實現(xiàn)漢子轉(zhuǎn)化成拼音的使用技巧,需要的朋友可以參考下
    2015-12-12
  • SpringBoot整合Shiro實現(xiàn)登錄認證的方法

    SpringBoot整合Shiro實現(xiàn)登錄認證的方法

    這篇文章主要介紹了SpringBoot整合Shiro實現(xiàn)登錄認證的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • Java編程實現(xiàn)打印螺旋矩陣實例代碼

    Java編程實現(xiàn)打印螺旋矩陣實例代碼

    這篇文章主要介紹了Java編程實現(xiàn)打印螺旋矩陣實例代碼,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • java多線程從入門到精通看這篇就夠了

    java多線程從入門到精通看這篇就夠了

    熟悉 Java 多線程編程的同學都知道,當我們線程創(chuàng)建過多時,容易引發(fā)內(nèi)存溢出,因此我們就有必要使用線程池的技術(shù)了,今天通過本文給大家分享java多線程從入門到精通的相關(guān)知識,一起看看吧
    2021-06-06
  • 解讀Hibernate、MyBatis有哪些區(qū)別

    解讀Hibernate、MyBatis有哪些區(qū)別

    Hibernate和MyBatis是Java中常用的持久層框架,各有優(yōu)勢和適用場景,Hibernate通過對象關(guān)系映射簡化數(shù)據(jù)庫操作,適合復雜業(yè)務邏輯;MyBatis提供更高的SQL控制權(quán),適合需要直接操作SQL的場景,選擇應基于項目需求和團隊習慣
    2025-01-01
  • ActiveMQ消息隊列技術(shù)融合Spring過程解析

    ActiveMQ消息隊列技術(shù)融合Spring過程解析

    這篇文章主要介紹了ActiveMQ消息隊列技術(shù)融合Spring過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-11-11
  • 深入理解java三種工廠模式

    深入理解java三種工廠模式

    下面小編就為大家?guī)硪黄钊肜斫鈐ava三種工廠模式。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06
  • SpringBoot中的@EnableAutoConfiguration注解解析

    SpringBoot中的@EnableAutoConfiguration注解解析

    這篇文章主要介紹了SpringBoot中的@EnableAutoConfiguration注解解析,@EnableAutoConfiguration也是借助@Import的幫助,將所有符合自動配置條件的bean定義注冊到IoC容器,需要的朋友可以參考下
    2023-09-09

最新評論

仙游县| 库伦旗| 永福县| 凤冈县| 孝义市| 喀什市| 台山市| 西贡区| 时尚| 湖南省| 江津市| 灵川县| 遂平县| 庆元县| 原阳县| 武胜县| 商城县| 淮南市| 白沙| 夏津县| 永德县| 扎囊县| 平遥县| 湛江市| 余姚市| 麻城市| 象山县| 六枝特区| 泾川县| 奉新县| 雅江县| 乌鲁木齐县| 边坝县| 浠水县| 湘阴县| 五大连池市| 若尔盖县| 洪江市| 桐柏县| 潜山县| 富平县|