Spring 中基於 AOP 的 @AspectJ註解實例

@AspectJ 作爲通過 Java 5 註釋註釋的普通的 Java 類,它指的是聲明 aspects 的一種風格。通過在你的基於架構的 XML 配置文件中包含以下元素,@AspectJ 支持是可用的。

1.第一步:倒入jar包,跟上個例子包是一樣的

  • aspectjrt.jar

  • aspectjweaver.jar

  • aspectj.jar

  • aopalliance.jar

2.第二步:創建三個類  

  2.1這裏是 Logging.java 文件的內容。這實際上是 aspect 模塊的一個示例,它定義了在各個點調用的方法。

 1 package com.spring.aop2;
 2 
 3 import org.aspectj.lang.annotation.AfterReturning;
 4 import org.aspectj.lang.annotation.AfterThrowing;
 5 import org.aspectj.lang.annotation.Aspect;
 6 import org.aspectj.lang.annotation.Before;
 7 import org.aspectj.lang.annotation.Pointcut;
 8 
 9 @Aspect // 定義切面
10 public class Logging {
11     @Pointcut("execution(* com.spring.aop2.Student.*(..))") // 定義切點
12     private void selectAll() {
13 
14     }
15     /**
16      * 定義通知方法
17      */
18 
19     @Before("selectAll()")
20     public void beforeAdvice() {
21         System.out.println("----------beforeAdvice-----------");
22 
23     }
24 
25     @AfterReturning(pointcut = "selectAll()", returning = "retVal")
26     public void afterReturningAdvice(Object retVal) {
27         System.out.println("Returning:" + retVal.toString());
28     }
29 
30     @AfterThrowing(pointcut = "selectAll()", throwing = "ex")
31     public void AfterThrowingAdvice(IllegalArgumentException ex) {
32         System.out.println("There has been an exception: " + ex.toString());
33     }
34 
35 }

  2.2下面是 Student.java 文件的內容:

 1 package com.spring.aop2;
 2 
 3 public class Student {
 4     /**
 5      * 定義學生類
 6      */
 7     private String name;
 8     private Integer age;
 9 
10     public String getName() {
11         return name;
12     }
13 
14     public void setName(String name) {
15         this.name = name;
16     }
17 
18     public Integer getAge() {
19         return age;
20     }
21 
22     public void setAge(Integer age) {
23         this.age = age;
24     }
25 
26     public void printAdvice() {
27         System.out.println("name:" + name + ",age:" + age);
28 
29     }
30 
31 }

  2.3下面是 MainApp.java 文件的內容:

 1 package com.spring.aop2;
 2 
 3 
 4 
 5 import org.springframework.context.ApplicationContext;
 6 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 7 import org.springframework.context.support.AbstractApplicationContext;
 8 import org.springframework.context.support.ClassPathXmlApplicationContext;
 9 
10 public class Main {
11     /**
12      * Spring aop基於註解    配置文件springAop.xml
13      * author:
14      * mail:[email protected]
15      * 時間:
16      */
17     public static void main(String[] args) {
18         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring_xml/springAop.xml");
19         Student student=(Student)applicationContext.getBean("Student");
20         student.printAdvice();
21     }
22 
23 }

3.第三步:創建bean文件(上面的頭文件在上個例子當中有,這裏就省略了)

下面是配置文件 Beans.xml

 1     
 2     <!-- 開啓註解      通過aop命名空間的<aop:aspectj-autoproxy 
 3 />聲明自動爲spring容器中那些配置@aspectJ切面的bean創建代理,織入切面-->
 4     <aop:aspectj-autoproxy/>
 5     <!-- 定義切面bean -->
 6     <bean id="Logging" class="com.spring.aop2.Logging"></bean>
 7     
 8     <!-- 配置bean -->
 9     <bean id="Student" class="com.spring.aop2.Student">
10         <property name="name" value="張三"></property>
11         <property name="age" value="23"></property>
12     </bean>

 

讓我們運行一下應用程序。如果你的應用程序一切都正常的話,這將會輸出以下消息(其中包含了異常通知):

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章