鍍金池/ 教程/ Java/ Spring AOP基于XML的切入點
Spring AOP基于XML的After Advice
Spring AOP基于XML的Before Advice
Spring AOP基于XML的After Returning Advice
Spring AOP基于注解的切入點
Spring AOP基于XML的After Throwing Advice
Spring AOP實現(xiàn)
Spring AOP基于注解的Around通知
Spring AOP環(huán)境安裝設(shè)置
Spring AOP自定義注解
Spring AOP代理
Spring AOP基于XML的切入點
Spring AOP基于XML的Around Advice
Spring AOP通知類型
Spring AOP基于XML的應(yīng)用程序
Spring AOP基于注解的After Advice
Spring AOP基于注解的Before Advice
Spring AOP基于注解的AfterThrowing
Spring AOP教程
Spring AOP基于注解的After Returning Advice
Spring AOP核心概念
Spring AOP基于注解的應(yīng)用

Spring AOP基于XML的切入點

切入點(JoinPoint)代表示您的應(yīng)用程序中可以插入AOP方面的某個點。 您也可以說,這是應(yīng)用程序中使用Spring AOP框架執(zhí)行操作的實際位置。 請考慮以下示例:

  • 包中包含的所有方法類。
  • 一個類的特定方法。

切入點

切入點(PointCut)是一組一個或多個連接點,其中它用來執(zhí)行通知??梢允褂帽磉_式或模式指定切入點,我們將在下面AOP示例中看到。 在Spring,切入點有助于使用特定的連接點來應(yīng)用通知。 請考慮以下示例:

  • expression="execution(* com.yiibai.*.*(..))"
  • expression="execution(* com.yiibai.Student.getName(..))"

語法

   <aop:config>
   <aop:aspect id="log" ref="adviceClass">
      <aop:pointcut id="pointcut-id" expression="execution( expression )"/>    
   </aop:aspect>
   </aop:config>

在上面語法中 -

  • adviceClass - 關(guān)聯(lián)(ref)的類包含通知方法。
  • pointcut-id - 切入點的ID。
  • execution( expression) - 涵蓋應(yīng)用通知的方法的表達式。

要了解上面提到的連接點(JoinPoint)和切入點(PointCut)相關(guān)的概念,我們來寫一個例子來實現(xiàn)一些切入點。為了方便編寫這個例子,讓我們使用Eclipse IDE,并按照以下步驟創(chuàng)建一個Spring應(yīng)用程序:

  1. 更新在Spring AOP應(yīng)用實例中創(chuàng)建過的Student項目。
  2. 更新bean配置并運行應(yīng)用程序,如下所述。

這是Logging.java文件的內(nèi)容。這實際上是一個Aspect模塊的樣本,它定義了在各個點被調(diào)用的方法。

package com.yiibai;

public class Logging {

   /** 
    * This is the method which I would like to execute
    * before a selected method execution.
    */
   public void beforeAdvice(){
      System.out.println("[beforeAdvice]Going to setup student profile.");
   }   
}

以下是 Student.java 文件的內(nèi)容:

package com.yiibai;

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 文件的內(nèi)容:

package com.yiibai;

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();     
   }
}

以下是 Beans.xml 文件的內(nèi)容:

<?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:config>
      <aop:aspect id="log" ref="logging">
         <aop:pointcut id="selectAll" 
         expression="execution(* com.yiibai.*.*(..))"/>
         <aop:before pointcut-ref="selectAll" method="beforeAdvice"/>         
      </aop:aspect>
   </aop:config>

   <!-- Definition for student bean -->
   <bean id="student" class="com.yiibai.Student">
      <property name="name"  value="Maxsu" />
      <property name="age"  value="24"/>      
   </bean>

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

</beans>

完成創(chuàng)建源和bean配置文件后,讓我們運行應(yīng)用程序。 如果您的應(yīng)用程序一切正常,這將打印以下消息:

四月 12, 2017 5:56:10 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@300ffa5d: startup date [Wed Apr 12 05:56:10 CST 2017]; root of context hierarchy
四月 12, 2017 5:56:10 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [Beans.xml]
[beforeAdvice]Going to setup student profile.
Name : Maxsu
[beforeAdvice]Going to setup student profile.
Age : 23
[beforeAdvice]Going to setup student profile.
Exception raised

讓我解釋一下,上面定義的<aop:pointcut>可以選擇com.yiibai包中定義的所有方法。假設(shè)要在特定方法之前或之后執(zhí)行您的通知,可以通過用切入點定義中的星(*)替換實際的類和方法名稱來定義切入點來縮小執(zhí)行范圍。 下面是一個修改后的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:config>
   <aop:aspect id="log" ref="logging">
      <aop:pointcut id="selectAll" 
      expression="execution(* com.yiibai.Student.getName(..))"/>
      <aop:before pointcut-ref="selectAll" method="beforeAdvice"/>
   </aop:aspect>
   </aop:config>

   <!-- Definition for student bean -->
   <bean id="student" class="com.yiibai.Student">
      <property name="name"  value="Maxsu" />
      <property name="age"  value="24"/>      
   </bean>

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

</beans>

運行項目

完成創(chuàng)建源代碼和配置文件后,運行應(yīng)用程序。右鍵單擊應(yīng)用程序中的MainApp.java,并使用運行方式作為Java應(yīng)用程序命令。 如果您的應(yīng)用程序一切正常,這將打印以下消息:

[beforeAdvice]Going to setup student profile.
Name : Maxsu
Age : 24