zoukankan      html  css  js  c++  java
  • 一步一步深入spring(6)--使用基于XML配置的spring实现的AOP

    上节我们提到了使用基于注解实现的AOP,这节我们将用基于xml配置的方式来实现的AOP。

    1.首先建立一个类,作为切面类,这个类主要用来实现注解中各种通知要实现的方法。

     1 package com.yangyang.aop;
     2 
     3 import org.aspectj.lang.ProceedingJoinPoint;
     4 /**
     5  * 切面类
     6  * @author Administer
     7  *
     8  */
     9 public class MyInterceptorForXml {
    10     //前置通知
    11     public void doAccessCheck(){
    12         System.out.println("前置通知");
    13     }
    14     
    15     //后置通知
    16     public void doAfterReturning(){
    17         System.out.println("后置通知:");
    18     }
    19     
    20     //例外通知
    21     public void doAfterThrowing(){
    22         System.out.println("例外通知");
    23     }
    24     
    25     //最终通知
    26     public void doAfter(){
    27         System.out.println("最终通知");
    28     }
    29     
    30     //环绕通知(特别适合做权限系统)
    31     public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{
    32         System.out.println("环绕通知进入方法");
    33         Object object=pjp.proceed();
    34         System.out.println("环绕通知退出方法");
    35         return object;
    36     }
    37 }

    2.在spring的配置文件中配置aop的相关操作:

     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 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
     7             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
     8             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
     9  <!-- 加上aop的命名空间以及DTD验证 -->
    10 
    11      <bean id="myInterceptorForXml" class="com.yangyang.aop.MyInterceptorForXml"></bean>
    12      <aop:config>
    13          <aop:aspect id="aspect" ref="myInterceptorForXml"><!-- 定义一个切面 -->
    14              <!-- 配置切入点 -->
    15              <aop:pointcut  id="mycut" expression="execution (* com.yangyang.service..*.*(..))"/> 
    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      
    24     <bean id="personService" class="com.yangyang.service.impl.PersonServiceImpl">
    25     </bean>
    26     
    27 </beans>

    特别要提到的是此处

    <aop:pointcut  id="mycut" expression="execution (* com.yangyang.service..*.*(..))"/> 中的execution 的表达式中* 与com.yangyang....之间要有一个空格。

    3.同理进行单元测试,得到与之前相同的结果,这样基于XML配置耳朵AOP也就实现了
  • 相关阅读:
    博弈论(SG函数):HNOI 2007 分裂游戏
    博弈论(二分图匹配):NOI 2011 兔兔与蛋蛋游戏
    博弈论(男人八题):POJ 1740 A New Stone Game
    动态规划(树形DP):HDU 5834 Magic boy Bi Luo with his excited tree
    杂项(最小表示法):HZOI 2015 Glass Beads
    如何避免死锁
    死锁的四个必要条件
    线程安全和可重入函数之间的区别和联系
    信号量 sem_undo设置
    linux管道的容量和内部组织方式
  • 原文地址:https://www.cnblogs.com/shunyang/p/3302122.html
Copyright © 2011-2022 走看看