spring基础知识(三)

spring基础知识(三)——aop面向切面编程


 

1、概念术语  

  aop面向切面编程(Aspect ariented Programming)

  在开始之前,需要理解Spring aop 的一些基本的概念术语(总结的个人理解,并非Spring官方定义):

  切面(aspect):用来切插业务方法的类。

  连接点(joinpoint):是切面类和业务类的连接点,其实就是封装了业务方法的一些基本属性,作为通知的参数来解析。

  通知(advice):在切面类中,声明对业务方法做额外处理的方法。

  切入点(pointcut):业务类中指定的方法,作为切面切入的点。其实就是指定某个方法作为切面切的地方。

  目标对象(target object):被代理对象。

  AOP代理(aop proxy):代理对象。

  通知:

  前置通知(before advice):在切入点之前执行。

  后置通知(after returning advice):在切入点执行完成后,执行通知。

  环绕通知(around advice):包围切入点,调用方法前后完成自定义行为。

  异常通知(after throwing advice):在切入点抛出异常后,执行通知。

导入相关jar包http://pan.baidu.com/s/1o7VLcFo

下面一个测试小案列

src目录如下

 

新建个类 Banboo.java

package aop.classes;

public class Banboo {
     public void testBanboo(String name){
         System.out.println("测试类方法1...");
     }
    
     
     private void testBanBoo(){
         System.out.println("测试类方法2,大写...");
     }
}

 

 切面类Cutor

package aop.aspect;

public class Cutor {
    public void addLogs(){
        System.out.println("在方法前面...添加相应的日志记录....");
    }
    
    
    public void  endAddLogs(){
        System.out.println("在方法最后,添加相应的日志记录....");
    }
}

 

配置xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    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
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd    
    ">

     <bean id="cc" class="aop.classes.Car"></bean>
    <bean id="bb" class="aop.classes.Banboo"></bean>
    
    
    <bean id="cutor" class="aop.aspect.Cutor"></bean>
    <aop:config>
      <aop:aspect id="mylog" ref="cutor" order="1">
          <aop:pointcut id="testcut" expression="execution( void aop.classes.*.test*(..))"/>  
          <aop:pointcut id="banboocut" expression="execution(* aop.classes.Banboo.*(..))"/>   
          <aop:pointcut id="noParmCut" expression="execution(* aop.classes.*.*())"/>
          <aop:before pointcut-ref="testcut" method="addLogs"/>
          <aop:after pointcut-ref="banboocut" method="endAddLogs"/>
      </aop:aspect>
    </aop:config>

</beans>

 

测试类 

package aop.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import aop.classes.Banboo;


public class Test {
    public static void main(String[] args) {
        ApplicationContext ac = new  ClassPathXmlApplicationContext("applicationContext.xml");
        
         Banboo bb =(Banboo)ac.getBean("bb");
         bb.testBanboo(null);
    }
}

 

测试结果

 

posted on 2017-05-17 19:21  虫儿aqa  阅读(185)  评论(0编辑  收藏  举报

导航