使用Spring配置文件实现AOP

来源:互联网 发布:seo工程师薪资待遇 编辑:程序博客网 时间:2024/04/28 01:38

 前面给大家介绍了怎样使用注解的方式来开发AOP应用,现在学习一下使用XML方式怎样来开发AOP应用。
  如果使用XML方式开发AOP应用的话,我们就不再需要提供注解的配置,我们只需要一个普通的Java类对象
MyInterceptor.java

Java代码 复制代码
  1. package cn.itcast.service;   
  2.   
  3. import org.aspectj.lang.ProceedingJoinPoint;   
  4. /**  
  5.  * 切面  
  6.  */  
  7. public class MyInterceptor {   
  8.     public void doAccessCheck() {   
  9.         System.out.println("前置通知");   
  10.     }   
  11.   
  12.     public void doAfterReturning() {   
  13.         System.out.println("后置通知");   
  14.     }   
  15.   
  16.     public void doAfter() {   
  17.         System.out.println("最终通知");   
  18.     }   
  19.   
  20.     public void doAfterThrowing() {   
  21.         System.out.println("例外通知");   
  22.     }   
  23.   
  24.     public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {   
  25.         System.out.println("进入方法");   
  26.         Object result = pjp.proceed();   
  27.         System.out.println("退出方法");   
  28.         return result;   
  29.     }   
  30.   
  31. }  



可以看到,在这个切面里面,这只是一个普普通通的Java类,里面没有任何的注解。如果我们采用基于XML方式来开发AOP应用的话,我们是要在配置文件中对切面进行配置的。现在看一下切面该如何配置

基于基于XML配置方式声明切面
--------------------------------------------------------------------

Xml代码 复制代码
  1. <bean id="orderservice" class="cn.itcast.service.OrderServiceBean"/>  
  2. <bean id="log" class="cn.itcast.service.LogPrint"/>  
  3. <aop:config>  
  4.   <aop:aspect id="myaop" ref="log">  
  5.     <aop:pointcut id="mycut" expression="execution(* cn.itcast.service..*.*(..))"/>  
  6.     <aop:before pointcut-ref="mycut" method="doAccessCheck"/>  
  7.     <aop:after-returning pointcut-ref="mycut" method="doReturnCheck "/>  
  8.     <aop:after-throwing pointcut-ref="mycut" method="doExceptionAction"/>  
  9.     <aop:after pointcut-ref="mycut" method=“doReleaseAction"/>  
  10.     <aop:around pointcut-ref="mycut" method="doBasicProfiling"/>  
  11.   </aop:aspect>  
  12. </aop:config>  


首先要把用做切面的类交给Spring管理,然后通过<aop:config>配置元素,首先配置引用这个切面<aop:aspect id="myaop" ref="log">。然后在切面底下,我们可以定义一些像切入点,前置通知,后置通知,例外通知,最终通知和环绕通知。这里有个参数pointcut-ref(切入点引用),那么引入的切入点就是前面定义的切入点<aop:pointcut id="mycut" expression="execution(* cn.itcast.service..*.*(..))"/>。 
当方法被拦截到后呢,它就会应用这个method所指定的方法。

beans.xml

Xml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:context="http://www.springframework.org/schema/context"    
  5.        xmlns:aop="http://www.springframework.org/schema/aop"         
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans   
  7.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  8.            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd   
  9.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
  10.         <aop:aspectj-autoproxy/>    
  11.         <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean>  
  12.         <bean id="aspetbean" class="cn.itcast.service.MyInterceptor"/>  
  13.         <aop:config>  
  14.             <aop:aspect id="asp" ref="aspetbean">  
  15.                 <aop:pointcut id="mycut" expression="execution(* cn.itcast.service.impl.PersonServiceBean.*(..))"/>  
  16.                 <aop:before pointcut-ref="mycut" method="doAccessCheck"/>  
  17.                 <aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>  
  18.                 <aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/>  
  19.                 <aop:after pointcut-ref="mycut" method="doAfter"/>  
  20.                 <aop:around pointcut-ref="mycut" method="doBasicProfiling"/>  
  21.             </aop:aspect>  
  22.         </aop:config>  
  23. </beans>  


PersonServiceBean.java

Java代码 复制代码
  1. package cn.itcast.service.impl;   
  2.   
  3. import cn.itcast.service.PersonService;   
  4.   
  5. public class PersonServiceBean implements PersonService {   
  6.   
  7.     public String getPersonName(Integer id) {   
  8.         System.out.println("我是getPersonName()方法");   
  9.         return "xxx";   
  10.     }   
  11.   
  12.     public void save(String name) {   
  13.         //throw new RuntimeException("我爱例外");   
  14.         System.out.println("我是save()方法");   
  15.     }   
  16.   
  17.     public void update(String name, Integer id) {   
  18.         System.out.println("我是update()方法");   
  19.     }   
  20.   
  21. }  



SpringAOPTest.java

Java代码 复制代码
  1. package junit.test;   
  2.   
  3. import org.junit.BeforeClass;   
  4. import org.junit.Test;   
  5. import org.springframework.context.ApplicationContext;   
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  7.   
  8. import cn.itcast.service.PersonService;   
  9.   
  10. public class SpringAOPTest {   
  11.   
  12.     @BeforeClass  
  13.     public static void setUpBeforeClass() throws Exception {   
  14.     }   
  15.   
  16.     @Test public void interceptorTest(){   
  17.         ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");   
  18.         PersonService personService = (PersonService)cxt.getBean("personService");   
  19.         personService.save("yyy");   
  20.     }   
  21. }  


现在看一下,我们采用基于XML方式开发AOP应用是否能够成功,在这里,目前除了例外通知不会执行外,其它通知都会执行,
运行单元测试代码,控制台输出
前置通知
进入方法
我是save()方法
后置通知
最终通知
退出方法


改动下代码,看下例外通知是否被执行
PersonServiceBean.java

Java代码 复制代码
  1. package cn.itcast.service.impl;   
  2.   
  3. import cn.itcast.service.PersonService;   
  4.   
  5. public class PersonServiceBean implements PersonService {   
  6.   
  7.     public String getPersonName(Integer id) {   
  8.         System.out.println("我是getPersonName()方法");   
  9.         return "xxx";   
  10.     }   
  11.   
  12.     public void save(String name) {   
  13.         throw new RuntimeException("我爱例外");   
  14.         //System.out.println("我是save()方法");   
  15.     }   
  16.   
  17.     public void update(String name, Integer id) {   
  18.         System.out.println("我是update()方法");   
  19.     }   
  20.   
  21. }  


运行单元测试代码:控制台输出
前置通知
进入方法
例外通知
最终通知

在JUnit里,有例外抛出
java.lang.RuntimeException: 我爱例外
at cn.itcast.service.impl.PersonServiceBean.save(PersonServiceBean.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        ...................................................

例外通知也被执行了。
大家看到,基于XML方式开发AOP应用也是挺简单的,和前面基于注解方式开发AOP应用不同之处是MyInterceptor.java这个类已经没有了注解的定义,它是一个纯粹的,普通的java bean对象,没有任何注解。  配置信息都是在XML配置文件里定义的。

我们采用AOP编程,一般都是用来做权限系统,运行期监控。。。这些系统