zoukankan      html  css  js  c++  java
  • spring-AOP-基于Schema切面的小例子

    前言:

    如果一个项目没有 jdk 5.0 , 那就无法使用基于@AspectJ 注解 的切面.

    但是使用AspectJ的表达式的大门还是可以使用的。

    我们可以用java提供的Schema配置方法,来替代基于AspectJ注解声明切面这种方式。

      其实很容易理解,@AspectJ注解配置 和 Schema配置 是两个不同的实现手法,它们都能实现说明切面和增强的类型。

     以下是一些标签的说明:

    <aop:config>标签可以定义多个切面

    <aop:aspect id="" ref=""> //标签用于定义一个切面,其内部可以定义多个增强
     <aop:pointcut expression="" id=""/> //定义切点
     <aop:before method="" pointcut-ref=""/>  //前置增强
     <aop:after-returning method="" pointcut-ref=""/>  //后置增强
     <aop:after-throwing method="" pointcut-ref=""/>  //异常增强
     <aop:after method="" pointcut-ref=""/>  //Final增强
    <aop:around method="" pointcut-ref=""/> //环绕增强

    <aop:declare-parents types-matching="com.imooc.aop.schema.advice.biz.*(+)"  //引介增强
    implement-interface="com.imooc.aop.schema.advice.Fit"
    default-impl="com.imooc.aop.schema.advice.FitImpl"/>

    代码:

    SayHelloBeforemale.java

    //增强类
    //
    不需要实现增强类的接口 , 只需要在 schema 的xml中定义就行啦 public class SayHelloBeforemale { public void before() { System.out.println("hello"); } }

     waiter.java

    //目标类
    public class waiter {
        public void say() {
            // TODO Auto-generated method stub
            System.out.println("先生");
        }
    
    }

    spring-aop-schema-advice.xml

    <bean id="advicemethods" class="test4.SayHelloBeforemale"/>
                <bean id="waiter" class="test4.waiter"/>
                  <aop:config proxy-target-class="true"> <!-- 当状态为true 开启了CGLib 目标类不需要实现接口 -->
                  <aop:pointcut expression="execution(* test4.waiter.*(..))" id="pointcutt"/>  <!-- 定义切点 -->
                      <aop:aspect id="advicemethods" ref="advicemethods">  <!-- 定义切面  -->
                          <aop:before method="before" pointcut-ref="pointcutt" />         <!-- 把切面切点连接起来 -->
                      </aop:aspect>
                  </aop:config>
                  

    UnitTest.java

    //测试类
    public
    class UnitTest { @Test //基于Schema配置方式 public void test3(){ ApplicationContext a = new ClassPathXmlApplicationContext("test4/spring-aop-schema-advice.xml"); waiter w = (waiter)a.getBean("waiter"); w.say(); } }
  • 相关阅读:
    linux试题
    linux常用脚本
    nagios
    lvs/nginx/haproxy 负载均衡优缺点分析讲解
    一次SSLPeerUnverifiedException,SSLHandshakeException问题的分析
    [转]【安卓笔记】AsyncTask源码剖析
    linux下查看进程占用端口和端口占用进程命令
    which framework or library is best to use WebRTC
    [转]svn diff 替代工具
    [转]使用Subversion进行版本控制
  • 原文地址:https://www.cnblogs.com/tjc1996/p/spring.html
Copyright © 2011-2022 走看看