一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Spring使用AspectJ注解和XML配置實現AOP

Spring使用AspectJ注解和XML配置實現AOP

2020-06-24 11:53玄玉 JAVA教程

這篇文章主要介紹了Spring使用AspectJ注解和XML配置實現AOP的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文演示的是Spring中使用AspectJ注解和XML配置兩種方式實現AOP

下面是使用AspectJ注解實現AOP的Java Project
首先是位于classpath下的applicationContext.xml文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
   
  <!-- 啟用AspectJ對Annotation的支持 -->    
  <aop:aspectj-autoproxy/>
       
  <bean id="userManager" class="com.jadyer.annotation.UserManagerImpl"/>
   
  <bean id="securityHandler" class="com.jadyer.annotation.SecurityHandler"/>
</beans>

然后是服務層接口以及實現類

?
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
package com.jadyer.annotation;
public interface UserManager {
  public void addUser(String username, String password);
  public void delUser(int userId);
  public String findUserById(int userId);
  public void modifyUser(int userId, String username, String password);
}
 
/**
 * 上面的UserManager是服務層的接口
 * 下面的UserManagerImpl是服務層接口的實現類
 */
 
package com.jadyer.annotation;
 
public class UserManagerImpl implements UserManager {
  public void addUser(String username, String password) {
    System.out.println("------UserManagerImpl.addUser() is invoked------");
  }
 
  public void delUser(int userId) {
    System.out.println("------UserManagerImpl.delUser() is invoked------");
  }
 
  public String findUserById(int userId) {
    System.out.println("------UserManagerImpl.findUserById() is invoked------");
    return "鐵面生";
  }
 
  public void modifyUser(int userId, String username, String password) {
    System.out.println("------UserManagerImpl.modifyUser() is invoked------");
  }
}

接下來是使用AspectJ注解標注的切入類

?
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
package com.jadyer.annotation;
 
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
 
@Aspect
public class SecurityHandler {
  /**
   * 定義Pointcut
   * @see Pointcut的名稱為addAddMethod(),此方法沒有返回值和參數
   * @see 該方法就是一個標識,不進行調用
   */
  @Pointcut("execution(* add*(..))") //匹配所有以add開頭的方法
  private void addAddMethod(){};
   
  /**
   * 定義Advice
   * @see 表示我們的Advice應用到哪些Pointcut訂閱的Joinpoint上
   */
  //@Before("addAddMethod()")
  @After("addAddMethod()")
  private void checkSecurity() {
    System.out.println("------【checkSecurity is invoked】------");
  }    
}

最后是客戶端測試類

?
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
package com.jadyer.annotation;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
/**
 * Spring對AOP的支持:采用Annotation方式
 * @see -------------------------------------------------------------------------------------
 * @see Spring提供的AOP功能還是很強大的,支持可配置,它的默認實現使用的就是JDK動態代理
 * @see 使用Spring的AOP不需要繼承相關的東西,也不需要實現接口
 * @see 但有個前提條件:由于是JDK動態代理,所以若想生成代理,該類就必須得實現一個接口才行
 * @see 如果該類沒有implements接口的話,仍去使用Spring的默認AOP實現時,那么就會出錯
 * @see 通常需要生成代理的類都是服務層的類,所以通常都會抽一個接口出來。即養成面向接口編程的習慣
 * @see -------------------------------------------------------------------------------------
 * @see 采用Annotation方式完成AOP示例的基本步驟,如下
 * @see 1、Spring2.0的依賴包配置。新增Annotation支持
 * @see   * SPRING_HOME//dist//spring.jar
 * @see   * SPRING_HOME//lib//log4j//log4j-1.2.14.jar
 * @see   * SPRING_HOME//lib//jakarta-commons//commons-logging.jar
 * @see   * SPRING_HOME//lib//aspectj//*.jar
 * @see 2、將橫切性關注點模塊化,建立SecurityHandler.java
 * @see 3、采用注解指定SecurityHandler為Aspect
 * @see 4、采用注解定義Advice和Pointcut
 * @see 5、啟用AspectJ對Annotation的支持,并且將目標類和Aspect類配置到IoC容器中
 * @see 6、開發客戶端
 * @see -------------------------------------------------------------------------------------
 */
public class Client {
  public static void main(String[] args) {
    ApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserManager userManager = (UserManager)factory.getBean("userManager");
    userManager.addUser("張起靈", "02200059");
  }
}

下面是使用XML配置文件實現AOP的Java Project
首先是位于src根目錄中的applicationContext-cglib.xml文件

?
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
<?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"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
       
  <!-- 強制使用CGLIB代理 -->
  <!-- <aop:aspectj-autoproxy proxy-target-class="true"/> -->
   
  <bean id="userManager" class="com.jadyer.cglib.UserManagerImpl"/>
   
  <bean id="securityHandler" class="com.jadyer.cglib.SecurityHandler"/>
   
  <aop:config>
    <aop:aspect id="securityAspect" ref="securityHandler"
      <aop:pointcut id="addAddMethod" expression="execution(* add*(..))"/>
      <aop:before method="checkSecurity" pointcut-ref="addAddMethod"/>
    </aop:aspect>
  </aop:config>
</beans>
 
<!-- 
匹配add開頭的所有的方法
execution(* add*(..))
 
匹配com.jadyer.servcices.impl包下的所有的類的所有的方法
execution(* com.jadyer.servcices.impl.*.*(..))
 
匹配com.jadyer.servcices.impl包下的add或者del開頭的所有的方法
execution(* com.jadyer.servcices.impl.*.add*(..)) || execution(* com.jadyer.servcices.impl.*.del*(..))
 -->

然后是服務層接口以及實現類

?
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
package com.jadyer.cglib;
public interface UserManager {
  public void addUser(String username, String password);
  public void delUser(int userId);
  public String findUserById(int userId);
  public void modifyUser(int userId, String username, String password);
}
 
/**
 * 上面的UserManager是服務層接口
 * 下面的UserManagerImpl是服務層接口的實現類
 */
 
package com.jadyer.cglib;
 
public class UserManagerImpl { 
//implements UserManager {
  public void addUser(String username, String password) {
    System.out.println("------UserManagerImpl.addUser() is invoked------");
  }
 
  public void delUser(int userId) {
    System.out.println("------UserManagerImpl.delUser() is invoked------");
  }
 
  public String findUserById(int userId) {
    System.out.println("------UserManagerImpl.findUserById() is invoked------");
    return "張三";
  }
 
  public void modifyUser(int userId, String username, String password) {
    System.out.println("------UserManagerImpl.modifyUser() is invoked------");
  }
}

接著是在applicationContext-cglib.xml中所指定的切入類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.jadyer.cglib;
 
import org.aspectj.lang.JoinPoint;
 
/**
 * 將客戶調用信息傳遞到該Advice中
 * @see 可以在Advice中添加一個JoinPoint參數,取得客戶端調用的方法名稱及參數值
 * @see 以后純粹使用AOP去寫類似這樣東西的情況比較少,我們主要使用Spring提供的事務
 * @see 關于這個,知道即可。下面是示例代碼
 */
public class SecurityHandler {
  private void checkSecurity(JoinPoint joinPoint) {
    for (int i=0; i<joinPoint.getArgs().length; i++) {
      System.out.println(joinPoint.getArgs()[i]); //獲取客戶端調用的方法的參數值
    }
    System.out.println(joinPoint.getSignature().getName()); //獲取客戶端調用的方法名稱
    System.out.println("------【checkSecurity is invoked】------");
  }
}

最后是客戶端測試類

?
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
package com.jadyer.cglib;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
/**
 * @see --------------------------------------------------------------------------------------------------
 * @see JDK動態代理和CGLIB代理的差別
 * @see 1..JDK動態代理對實現了接口的類進行代理
 * @see 2..CGLIB代理可以對類代理,主要對指定的類生成一個子類。由于是繼承,所以目標類最好不要使用final聲明
 * @see --------------------------------------------------------------------------------------------------
 * @see 代理方式的選擇
 * @see 1..如果目標對象實現了接口,默認情況下會采用JDK動態代理實現AOP,亦可強制使用CGLIB生成代理實現AOP
 * @see 2..如果目標對象未實現接口,那么必須引入CGLIB,這時Spring會在JDK動態代理和CGLIB代理之間自動切換
 * @see 3..比較鼓勵業務對象是針對接口編程的,所以鼓勵使用JDK動態代理。因為我們所代理的目標,一般都是業務對象
 * @see --------------------------------------------------------------------------------------------------
 * @see 使用CGLIG代理的步驟
 * @see 1..新增CGLIB庫:SPRING_HOME//lib//cglib//*.jar
 * @see 2..新增配置標簽,強制使用CGLIB代理<aop:aspectj-autoproxy proxy-target-class="true"/>
 * @see --------------------------------------------------------------------------------------------------
 */
public class Client {
  public static void main(String[] args) {
    ApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext-cglib.xml");
     
    //當UserManagerImpl實現了UserManager接口的情況下,這時Spring會自動使用JDK動態代理
    //如果項目已經引入cglib庫,并在配置文件中強制使用CGLIB代理,此時Spring才會使用CGLIB代理
    //UserManager userManager = (UserManager)factory.getBean("userManager");
     
    //由于此時的UserManagerImpl并沒有實現UserManager接口,所以接收類型就不能再使用UserManager接口
    //并且項目中已經引入了cglib庫,盡管配置文件中沒有強制使用CGLIB代理,但Spring會自動使用CGLIB代理   
    UserManagerImpl userManager = (UserManagerImpl)factory.getBean("userManager");
     
    userManager.addUser("吳三省", "02200059");
  }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
  • JAVA教程解決dubbo錯誤ip及ip亂入問題的方法

    解決dubbo錯誤ip及ip亂入問題的方法

    今天小編就為大家分享一篇關于解決dubbo錯誤ip及ip亂入問題的方法,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨...

    雙斜杠少年5992019-06-24
  • JAVA教程Java 線程池詳解及實例代碼

    Java 線程池詳解及實例代碼

    這篇文章主要介紹了Java 線程池的相關資料,并符實例代碼,幫助大家學習參考,需要的朋友可以參考下 ...

    f2yy2042020-06-16
  • JAVA教程Mybatis傳遞多個參數進行SQL查詢的用法

    Mybatis傳遞多個參數進行SQL查詢的用法

    本文給大家介紹Mybatis傳遞多個參數進行SQL查詢的用法的相關知識,本文還給大家介紹了mybatis通過Map傳遞多個參數和JavaBean傳遞多個參數,本文介紹的非常詳...

    zifangsky1962020-05-23
  • JAVA教程java異常機制分析

    java異常機制分析

    這篇文章主要介紹了java異常機制,包括異常機制的捕獲、拋出及常見的異常機制總結,需要的朋友可以參考下 ...

    shichen20141992019-11-28
  • JAVA教程java數組輸出的實例代碼

    java數組輸出的實例代碼

    這篇文章主要介紹了java數組輸出的實例代碼,有需要的朋友可以參考一下 ...

    java代碼網3202019-10-26
  • JAVA教程java讀取csv文件和寫csv示例分享

    java讀取csv文件和寫csv示例分享

    這篇文章主要介紹了JAVA對CSV格式文本數據處理后再保存成新CSV格式文本的模板,可以學習到java讀取csv文件和寫csv的方法,需要的朋友可以參考下 ...

    java技術網2242019-11-14
  • JAVA教程深入理解Java 對象和類

    深入理解Java 對象和類

    下面小編就為大家帶來一篇深入理解Java 對象和類。小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧 ...

    jingxian3442020-04-28
  • JAVA教程詳解Java中使用externds關鍵字繼承類的用法

    詳解Java中使用externds關鍵字繼承類的用法

    子類使用extends繼承父類是Java面向對象編程中的基礎知識,這里我們就來詳解Java中使用externds關鍵字繼承類的用法,需要的朋友可以參考下 ...

    DBOY3772020-05-25
主站蜘蛛池模板: 疯狂伦交1一6 小说 风间由美在线 | 久久理论片迅播影院一级 | 狠狠狠地啪香蕉 | 久久水蜜桃亚洲AV无码精品偷窥 | 日本片免费观看一区二区 | 欧美精品黑人巨大在线播放 | 星星动漫无删减在线观看 | 国产精选之刘婷野战 | ferr孕妇videos毛茸茸 | 精品在线网站 | 暗卫调教女主肉高h | 日本不卡不码高清免费观看 | 男人的天堂在线 | 青草视频免费 | 色姑娘久| 男人j放进女人的p免费看视频 | 国产美女下面流出白浆视频 | 欧洲肥女大肥臀 | 羞羞视频免费观 | 九九99热久久精品在线6 | 国模一区二区三区视频一 | 久草草在线视视频 | 欧美一区二区三区大片 | 第一福利在线导航 | 福利视频一区二区思瑞 | 美琪美腿白丝交小说 | www免费插插视频 | 亚洲福利一区 | 午夜在线a亚洲v天堂网2019 | 久久精品国产免费播放 | 欧美夜夜精品一级爽 | 国语精彩对白2021 | 97久久天天综合色天天综合色hd | 天堂伊人网 | 国产盗摄女厕美女嘘嘘 | 国产精品麻豆久久99 | 欧美高清videosdesex0 | ts人妖另类国产 | 草草影院国产 | 欧美久草在线 | 桃乃木香在线 |