引入(Introduction):
也被称为内部类型声明(inter-type declaration)。为已有的类声明额外的方法或者某个类型的字段。 Spring允许引入新的接口(以及一个对应的实现)到任何被代理的对象。 例如,你可以使用一个引入来使bean实现 IsModified 接口,以便简化缓存机制。
前置通知(Before advice):
在某连接点之前执行的通知,但这个通知不能阻止连接点前的执行(除非它抛出一个异常)。
返回后通知(After returning advice):
在某连接点正常完成后执行的通知:例如,一个方法没有抛出任何异常,正常返回。
抛出异常后通知(After throwing advice):
在方法抛出异常退出时执行的通知。
后通知(After (finally) advice):
当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)。
环绕通知(Around Advice):
包围一个连接点的通知,如方法调用。这是最强大的一种通知类型。 环绕通知可以在方法调用前后完成自定义的行为。它也会选择是否继续执行连接点或直接返回它们自己的返回值或抛出异常来结束执行。

构建环境和前面一样
定义一个接口如下:
java代码:
查看复制到剪贴板打印
package cn.javass.Spring3.aop;
public interface Api {
public String testAop();
}
写一个类实现这个接口
java代码:
查看复制到剪贴板打印
public class Impl implements Api{
public String testAop() {
System.out.println("test aop");
return "aop is ok";
}
}
写一个类作为Before的Advice,没有任何特殊要求,就是一个普通类
java代码:
查看复制到剪贴板打印
public class MyBefore {
public void b1(){
System.out.println("now befoer--------->");
}
}
配置文件,要注意配置的时候要保证一定要有命名空间aop的定义,如下:
java代码:
查看复制到剪贴板打印
< xml version="1.0" encoding="UTF-8" >
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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
">
method="b1"/>
客户端如下:
java代码:
查看复制到剪贴板打印
public class Client {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"applicationContext.xml"});
Api api = (Api)context.getBean("testAopApi");
String s = api.testAop();
System.out.println("s=="+s);
}
}
测试结果如下:
now befoer--------->
test aop
s==aop is ok
作者:jinnianshilongnian