• 笔记10 在XML中声明切面(1)


    1.无注解的Audience

     1 package XMLconcert;
     2 
     3 public class Audience {
     4 
     5     public void silenceCellPhones() {
     6         System.out.println("Silencing cell phone");
     7     }
     8 
     9     public void takeSeats() {
    10         System.out.println("Taking seats");
    11     }
    12 
    13     public void applause() {
    14         System.out.println("CLAP CLAP CLAP");
    15     }
    16 
    17     public void demandRefund() {
    18         System.out.println("Demanding a refund");
    19     }
    20 
    21 }

    2.通过XML将无注解的Audience声明为切面

     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:aop="http://www.springframework.org/schema/aop"
     5     xmlns:tx="http://www.springframework.org/schema/tx"
     6     xmlns:context="http://www.springframework.org/schema/context"
     7     xmlns:c="http://www.springframework.org/schema/c"
     8     xsi:schemaLocation="
     9    http://www.springframework.org/schema/beans
    10    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    11    http://www.springframework.org/schema/aop
    12    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    13    http://www.springframework.org/schema/tx
    14    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    15    http://www.springframework.org/schema/context     
    16    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    17         <bean id="audience" class="XMLconcert.Audience"></bean>
    18         <bean class="XMLconcert.Classcial"></bean>
    19           <aop:config>
    20                   <aop:aspect ref="audience">
    21                       <aop:before pointcut="execution(* XMLconcert.Performance.perform(..))" method="silenceCellPhones"/>
    22                       <aop:before pointcut="execution(* XMLconcert.Performance.perform(..))" method="takeSeats"/>
    23                       <aop:after-returning pointcut="execution(* XMLconcert.Performance.perform(..))" method="applause"/>
    24                       <aop:after-throwing pointcut="execution(* XMLconcert.Performance.perform(..))" method="demandRefund"/>
    25                   </aop:aspect>
    26           </aop:config>
    27            
    28    </beans>

    或者

    1           <aop:config>
    2                   <aop:aspect ref="audience">
    3                       <aop:pointcut expression="execution(* XMLconcert.Performance.perform(..))" id="performance"/>
    4                       <aop:before pointcut-ref="performance" method="silenceCellPhones"/>
    5                       <aop:before pointcut-ref="performance" method="takeSeats"/>
    6                       <aop:after-returning pointcut-ref="performance" method="applause"/>
    7                       <aop:after-throwing pointcut-ref="performance" method="demandRefund"/>
    8                   </aop:aspect>
    9           </aop:config>

    或者

    替换Audience中的四个方法

     1     public void watchPerformance(ProceedingJoinPoint jp) {
     2         try {
     3             System.out.println("Silencing cell phone");
     4             System.out.println("Taking seats");
     5             jp.proceed();
     6             System.out.println("CLAP CLAP CLAP");
     7         } catch (Throwable e) {
     8             System.out.println("Demanding a refund");
     9         }
    10     }
    1           <aop:config>
    2                   <aop:aspect ref="audience">
    3                       <aop:pointcut expression="execution(* XMLconcert.Performance.perform(..))" id="performance"/>
    4                       <aop:around method="watchPerformance" pointcut-ref="performance"/>
    5                   </aop:aspect>
    6           </aop:config>

    3.结果

  • 相关阅读:
    【转】 mysql反引号的使用(防冲突)
    【百度一键分享功能】百度一键分享插件
    【WEB2.0】 网页调用QQ聊天(PC+M站)
    python : takes 0 positional arguments but 1 was given
    python : 设计模式之外观模式(Facade Pattern)
    Tosca : 扩展dll动态语言 识别点击下拉, 识别成table
    Tosca : 把 inner text 放到变量里,定义变量,使用变量
    Tosca:键盘输入字符串
    Tosca 给定义变量,取内容放到变量里
    Tosca :配置环境参数
  • 原文地址:https://www.cnblogs.com/lyj-gyq/p/8886545.html
Copyright © 2020-2023  润新知