Spring框架: 面向切面编程(AOP)基于@AspectJ

作者: Arvin Chen 分类: Java 来源: Break易站(www.breakyizhan.com)

@AspectJ引用一种将方面声明为Java 5注释中标注的常规Java类的样式。通过在基于XML模式的配置文件中包含以下元素来启用@AspectJ支持。

<aop:aspectj-autoproxy/>

您还需要在应用程序的类路径中使用以下AspectJ库。这些库可在AspectJ安装的'lib'目录中找到,否则你可以从网上下载它们。

  • aspectjrt.jar
  • aspectjweaver.jar
  • aspectj.jar
  • aopalliance.jar

声明一个切面Aspect

Aspect类与任何其他正常的bean类似,并且可能像任何其他类一样拥有方法和字段,但它们将使用@Aspect注释,如下所示 -

package org.xyz;

import org.aspectj.lang.annotation.Aspect;

@Aspect
public class AspectModule {
}

它们将像以下任何其他bean一样在XML中进行配置 -

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

声明一个切入点

一个切入点有助于确定不同的意见执行感兴趣的连接点(即方法)。在使用基于@ AspectJ的配置时,切入点声明包含两部分 -

  • 切入点表达式确切地确定我们感兴趣的方法执行。
  • 包含名称和任意数量参数的切入点签名。该方法的实际内容是不相关的,实际上应该是空的。

以下示例定义了一个名为'businessService'的切入点,该切入点将匹配包com.xyz.myapp.service下的类中可用的每个方法的执行 -

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.breakyizhan.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 advice的内联切入点的示例 -

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

基于@AspectJ的AOP示例

为了理解上述与基于@AspectJ的AOP相关的概念,让我们编写一个实现几个建议的例子。为了编写我们的例子,我们提供了很少的建议,让我们使用Eclipse IDE,并采取以下步骤来创建一个Spring应用程序 -

描述
1 创建一个名称为SpringExample的项目,并在创建的项目的src文件夹下创建一个com.breakyizhan包。
2 按照Spring Hello World Example章节的介绍,使用Add External JARs选项添加所需的Spring库。
3 在项目中添加Spring AOP特定的库aspectjrt.jar,aspectjweaver.jaraspectj.jar
4 com.breakyizhan包下创建Java类LoggingStudentMainApp
5 src文件夹下创建Beans配置文件Beans.xml
6 最后一步是创建所有Java文件和Bean配置文件的内容并按照下面的说明运行应用程序。

这里是Logging.java文件的内容。这实际上是一个方面模块的样本,它定义了在各个点上被调用的方法。

package com.breakyizhan;

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.breakyizhan;

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.breakyizhan;

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.breakyizhan.Student">
      <property name = "name" value = "Zara" />
      <property name = "age" value = "11"/>      
   </bean>

   <!-- Definition for logging aspect -->
   <bean id = "logging" class = "com.breakyizhan.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

本文来自:Spring框架: 面向方面编程(AOP)基于@Aspect

  •   本文标题:Spring框架: 面向切面编程(AOP)基于@AspectJ - Break易站
    转载请保留页面地址:https://www.breakyizhan.com/java/3180.html

    发表笔记

    电子邮件地址不会被公开。 必填项已用*标注

    更多阅读