Spring--AOP

Spring 框架的 AOP

Spring 框架的一个关键组件是面向方面的编程(AOP)框架。java

面向切面的编程须要把程序逻辑分解成不一样的部分称为所谓的关注点。跨一个应用程序的多个点的功能被称为横切关注点。如日志记录、审计、声明式事务、安全性和缓存等。spring

Spring AOP 模块提供拦截器来拦截一个应用程序,例如,当执行一个方法时,你能够在方法执行以前或以后添加额外的功能。express

AOP 术语

在咱们开始使用 AOP 工做以前,让咱们熟悉一下 AOP 概念和术语。这些术语并不特定于 Spring,而是与 AOP 有关的。编程

描述
Aspect 一个模块具备一组提供横切需求的 APIs。例如,一个日志模块为了记录日志将被 AOP 方面调用。应用程序能够拥有任意数量的方面,这取决于需求。
Join point 在你的应用程序中它表明一个点,你能够在插件 AOP 方面。你也能说,它是在实际的应用程序中,其中一个操做将使用 Spring AOP 框架。
Advice 这是实际行动以前或以后执行的方法。这是在程序执行期间经过 Spring AOP 框架实际被调用的代码。
Pointcut 这是一组一个或多个链接点,通知应该被执行。你能够使用表达式或模式指定切入点正如咱们将在 AOP 的例子中看到的。
Introduction 引用容许你添加新方法或属性到现有的类中。
Target object 被一个或者多个方面所通知的对象,这个对象永远是一个被代理对象。也称为被通知对象。
Weaving Weaving 把方面链接到其它的应用程序类型或者对象上,并建立一个被通知的对象。这些能够在编译时,类加载时和运行时完成。

为了在本节的描述中使用aop命名空间标签,你须要导入spring-aop j架构,以下所述缓存

<?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 ">

   <!-- bean definition & AOP specific configuration -->

</beans>

声明一个方面

<aop:config>
   <aop:aspect id="myAspect" ref="aBean">
   ...
   </aop:aspect>
</aop:config>
<bean id="aBean" class="...">
...
</bean>

声明建议

你能够使用<aop:{ADVICE NAME}>元素在一个中声明五个建议中的任何一个,以下所示:安全

<aop:config>
   <aop:aspect id="myAspect" ref="aBean">
      <aop:pointcut id="businessService"
         expression="execution(* com.xyz.myapp.service.*.*(..))"/>
      <!-- a before advice definition -->
      <aop:before pointcut-ref="businessService" 
         method="doRequiredTask"/>
      <!-- an after advice definition -->
      <aop:after pointcut-ref="businessService" 
         method="doRequiredTask"/>
      <!-- an after-returning advice definition -->
      <!--The doRequiredTask method must have parameter named retVal -->
      <aop:after-returning pointcut-ref="businessService"
         returning="retVal"
         method="doRequiredTask"/>
      <!-- an after-throwing advice definition -->
      <!--The doRequiredTask method must have parameter named ex -->
      <aop:after-throwing pointcut-ref="businessService"
         throwing="ex"
         method="doRequiredTask"/>
      <!-- an around advice definition -->
      <aop:around pointcut-ref="businessService" 
         method="doRequiredTask"/>
   ...
   </aop:aspect>
</aop:config>
<bean id="aBean" class="...">
...
</bean>

Spring中基于AOP的@AspectJ

@AspectJ做为经过Java 5注释的普通的Java类,它指的是声明方面的一种风格。经过在你的基于架构的XML配置文件中包含如下元素,@ AspectJ支持是可用的。架构

<aop:aspectj-autoproxy/>

 

@Aspect
public class Logging {

   //此方法返回一个对象,bean表明切入点
   @Pointcut("execution(* com.tutorialspoint.*.*(..))")
   private void selectAll(){}
  
   @Before("selectAll()")
   public void beforeAdvice(){
      System.out.println("Going to setup student profile.");
   }

   @After("selectAll()")
   public void afterAdvice(){
      System.out.println("Student profile has been setup.");
   }
 
   @AfterReturning(pointcut = "selectAll()", returning="retVal")
   public void afterReturningAdvice(Object retVal){
      System.out.println("Returning:" + retVal.toString() );
   }
  
   @AfterThrowing(pointcut = "selectAll()", throwing = "ex")
   public void AfterThrowingAdvice(IllegalArgumentException ex){
      System.out.println("There has been an exception: " + ex.toString());   
   }  
}
<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"/>
相关文章
相关标签/搜索