日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

中國最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2

wkspring教程

Spring 中基于 AOP 的 @AspectJ

閱讀 (2219)

Spring 中基于 AOP 的 @AspectJ

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

<aop:aspectj-autoproxy/>

你還需要在你的應用程序的 CLASSPATH 中使用以下 AspectJ 庫文件。這些庫文件在一個 AspectJ 裝置的 ‘lib’ 目錄中是可用的,否則你可以在 Internet 中下載它們。

  • aspectjrt.jar

  • aspectjweaver.jar

  • aspectj.jar

  • aopalliance.jar

 聲明一個 aspect

Aspects 類和其他任何正常的 bean 一樣,除了它們將會用 @AspectJ 注釋之外,它和其他類一樣可能有方法和字段,如下所示:

package org.xyz;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class AspectModule {
}

它們將在 XML 中按照如下進行配置,就和其他任何 bean 一樣:

<bean id="myAspect" class="org.xyz.AspectModule">
   <!-- configure properties of aspect here as normal -->
</bean>

聲明一個切入點

一個切入點有助于確定使用不同建議執行的感興趣的連接點(即方法)。在處理基于配置的 XML 架構時,切入點的聲明有兩個部分:

  • 一個切入點表達式決定了我們感興趣的哪個方法會真正被執行。

  • 一個切入點標簽包含一個名稱和任意數量的參數。方法的真正內容是不相干的,并且實際上它應該是空的。

下面的示例中定義了一個名為 ‘businessService’ 的切入點,該切入點將與 com.tutorialspoint 包下的類中可用的每一個方法相匹配:

import org.aspectj.lang.annotation.Pointcut;
@Pointcut("execution(* com.xyz.myapp.service.*.*(..))") // expression 
private void businessService() {}  // signature

下面的示例中定義了一個名為 ‘getname’ 的切入點,該切入點將與 com.tutorialspoint 包下的 Student 類中的 getName() 方法相匹配:

import org.aspectj.lang.annotation.Pointcut;
@Pointcut("execution(* com.tutorialspoint.Student.getName(..))") 
private void getname() {}

聲明建議

你可以使用 @{ADVICE-NAME} 注釋聲明五個建議中的任意一個,如下所示。這假設你已經定義了一個切入點標簽方法 businessService():

@Before("businessService()")
public void doBeforeTask(){
 ...
}
@After("businessService()")
public void doAfterTask(){
 ...
}
@AfterReturning(pointcut = "businessService()", returning="retVal")
public void doAfterReturnningTask(Object retVal){
  // you can intercept retVal here.
  ...
}
@AfterThrowing(pointcut = "businessService()", throwing="ex")
public void doAfterThrowingTask(Exception ex){
  // you can intercept thrown exception here.
  ...
}
@Around("businessService()")
public void doAroundTask(){
 ...
}

你可以為任意一個建議定義你的切入點內聯。下面是在建議之前定義內聯切入點的一個示例:

@Before("execution(* com.xyz.myapp.service.*.*(..))")
public doBeforeTask(){
 ...
}

基于 AOP 的 @AspectJ 示例

為了理解上面提到的關于基于 AOP 的 @AspectJ 的概念,讓我們編寫一個示例,可以實現幾個建議。為了在我們的示例中使用幾個建議,讓我們使 Eclipse IDE 處于工作狀態,然后按照如下步驟創建一個 Spring 應用程序:

步驟 描述
1 創建一個名為 SpringExample 的項目,并且在所創建項目的 src 文件夾下創建一個名為 com.tutorialspoint 的包。
2 使用 Add External JARs 選項添加所需的 Spring 庫文件,就如在 Spring Hello World Example 章節中解釋的那樣。
3 在項目中添加 Spring AOP 指定的庫文件 aspectjrt.jar, aspectjweaver.jaraspectj.jar
4 com.tutorialspoint 包下創建 Java 類 LoggingStudentMainApp
5 src 文件夾下創建 Beans 配置文件 Beans.xml
6 最后一步是創建所有 Java 文件和 Bean 配置文件的內容,并且按如下解釋的那樣運行應用程序。

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

package com.tutorialspoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
@Aspect
public class Logging {
   /** Following is the definition for a pointcut to select
    *  all the methods available. So advice will be called
    *  for all the methods.
    */
   @Pointcut("execution(* com.tutorialspoint.*.*(..))")
   private void selectAll(){}
   /** 
    * This is the method which I would like to execute
    * before a selected method execution.
    */
   @Before("selectAll()")
   public void beforeAdvice(){
      System.out.println("Going to setup student profile.");
   }
   /** 
    * This is the method which I would like to execute
    * after a selected method execution.
    */
   @After("selectAll()")
   public void afterAdvice(){
      System.out.println("Student profile has been setup.");
   }
   /** 
    * This is the method which I would like to execute
    * when any method returns.
    */
   @AfterReturning(pointcut = "selectAll()", returning="retVal")
   public void afterReturningAdvice(Object retVal){
      System.out.println("Returning:" + retVal.toString() );
   }
   /**
    * This is the method which I would like to execute
    * if there is an exception raised by any method.
    */
   @AfterThrowing(pointcut = "selectAll()", throwing = "ex")
   public void AfterThrowingAdvice(IllegalArgumentException ex){
      System.out.println("There has been an exception: " + ex.toString());   
   }  
}

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

package com.tutorialspoint;
public class Student {
   private Integer age;
   private String name;
   public void setAge(Integer age) {
      this.age = age;
   }
   public Integer getAge() {
      System.out.println("Age : " + age );
      return age;
   }
   public void setName(String name) {
      this.name = name;
   }
   public String getName() {
      System.out.println("Name : " + name );
      return name;
   }
   public void printThrowException(){
      System.out.println("Exception raised");
      throw new IllegalArgumentException();
   }
}

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

package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = 
             new ClassPathXmlApplicationContext("Beans.xml");
      Student student = (Student) context.getBean("student");
      student.getName();
      student.getAge();     
      student.printThrowException();
   }
}

下面是配置文件 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/>

   <!-- Definition for student bean -->
   <bean id="student" class="com.tutorialspoint.Student">
      <property name="name"  value="Zara" />
      <property name="age"  value="11"/>      
   </bean>

   <!-- Definition for logging aspect -->
   <bean id="logging" class="com.tutorialspoint.Logging"/> 

</beans>

一旦你已經完成的創建了源文件和 bean 配置文件,讓我們運行一下應用程序。如果你的應用程序一切都正常的話,這將會輸出以下消息:

Going to setup student profile.
Name : Zara
Student profile has been setup.
Returning:Zara
Going to setup student profile.
Age : 11
Student profile has been setup.
Returning:11
Going to setup student profile.
Exception raised
Student profile has been setup.
There has been an exception: java.lang.IllegalArgumentException
.....
other exception content
關閉
程序員人生
主站蜘蛛池模板: 成人免费视频播放器 | 在线视频日韩 | 日韩在线视频一区二区三区 | 99视频这里有精品 | 日韩精品欧美 | 日在线视频 | 日日夜夜亚洲 | 精品一区在线播放 | 成人在线精品 | 91嫩草影视 | 国产精品1区 | 最近中文字幕视频在线观看 | 午夜精品久久久久久久96蜜桃 | 免费视频在线观看网站 | 91日日| www.成人网| av中文字幕在线播放 | 亚洲免费综合 | 久久国产精品免费一区二区三区 | 毛片三级| 在线精品亚洲欧美日韩国产 | 亚洲三区在线 | 国产三区四区 | 精品国产乱码久久久 | 久久一本到 | 一级女性全黄久久生活片免费 | 国产激情网站 | 亚洲aa在线| 天天干天天爽 | 超碰久热| 欧美日韩免费在线观看 | 日本亚洲最大的色成网站www | 成人在线视频一区二区 | 不卡一二三区 | 国产爱视频| 中国大陆高清aⅴ毛片 | 欧美婷婷色| 黄色电影免费看 | 久久久一区二区三区 | 国产乱国产乱300精品 | 日日视频 |