Spring入门(六)基于 AOP 的 XML架构

理解aop执行过程

 <aop:config>
      <aop:aspect id="log" ref="logging">
         <aop:pointcut id="selectAll" 
         expression="execution(* com.tutorialspoint.*.*(..))"/>
         <aop:before pointcut-ref="selectAll" method="beforeAdvice"/>
         <aop:after pointcut-ref="selectAll" method="afterAdvice"/>
         <aop:after-returning pointcut-ref="selectAll" 
                              returning="retVal"
                              method="afterReturningAdvice"/>
         <aop:after-throwing pointcut-ref="selectAll" 
                             throwing="ex"
                             method="AfterThrowingAdvice"/>
      </aop:aspect>
   </aop:config>

   <!-- 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"/> 

这是xml种的aop配置文件

expression="execution(* com.tutorialspoint.*.*(..))"/>

该切入点可以执行 com.tutorialspoint报下的所有类的任意含参方法。

aop:before这行是在一个方法执行之前执行的方法

aop:after这行是在一个方法执行之后执行的方法

aop:after-returing这行是在一个方法返回之后不考虑报错等情况执行的方法,return "retVal",retVal必须与该方法的参数名相同,因为该方法会拦截他

aop:aftere-throwing这行是在一个方法报错之后执行的方法throw "ex" ex 必须与该方法的参数名相同,因为该方法会拦截他

如果想在一个特定的方法执行的时候执行该建议,可以修改expression;比如设置为Student的getName()方法

expression="execution(* com.tutorialspoint.Student.getName(..))"/>

注意该方法不能是logging里面的方法!!!

并且after after-returning方法执行的顺序和xml文件配置的顺序有关。

参考的:

package com.tutorialspoint;
public class Logging {
   /** 
    * This is the method which I would like to execute
    * before a selected method execution.
    */
   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.
    */
   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.
    */
   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.
    */
   public void AfterThrowingAdvice(IllegalArgumentException ex){
      System.out.println("There has been an exception: " + ex.toString());   
   }  
}
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();
	   }
	}
<?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.tutorialspoint.*.*(..))"/>
         <aop:before pointcut-ref="selectAll" method="beforeAdvice"/>
         <aop:after pointcut-ref="selectAll" method="afterAdvice"/>
         <aop:after-returning pointcut-ref="selectAll" 
                              returning="retVal"
                              method="afterReturningAdvice"/>
         <aop:after-throwing pointcut-ref="selectAll" 
                             throwing="ex"
                             method="AfterThrowingAdvice"/>
      </aop:aspect>
   </aop:config>

   <!-- 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>
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
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();
   }
}

猜你喜欢

转载自blog.csdn.net/qq_40883132/article/details/81410942
今日推荐