1、创建 Swimable接口、实现子类Tortoise、SwimAdvices类
1.1 Swimable接口
package com.mu.aop.schema;
public interface Swimable {
void swim();
}
1.2 Tortoise类
package com.mu.aop.schema;
public class Tortoise implements Swimable {
[@Override](https://my.oschina.net/u/1162528)
public void swim() {
System.out.println("乌龟在水里玩游戏");
}
public String toString(){
System.out.println("["+this.getClass().getCanonicalName()+": toString()]");
return "Tortoise["+Integer.toHexString(System.identityHashCode(this))+"]";
}
}
1.2 SwimAdvices类
package com.mu.aop.schema;
public class SwimAdvices {
public void start(){
System.out.println("游戏开始,请准备");
}
public void end(){
System.out.println("游戏结束,退出系统");
}
}
2、创建Spring的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 确定需要 添加的代码所在的 bean -->
<bean id="advices" class="com.mu.aop.schema.SwimAdvices" />
<bean id="catAdvices" class="com.mu.aop.schema.CatAdvices" />
<!-- 确定代理目标(谁将被代理) -->
<bean id="tortoise" class="com.mu.aop.schema.Tortoise" />
<bean id="cat" class="com.mu.aop.schema.Cat" p:name="汤姆" />
<!-- 提供 AOP 配置 -->
<aop:config>
<!-- 在 aop:aspect 标签中可以使用 ref 来引用 Advice 对应的 bean -->
<aop:aspect ref="advices">
<!-- 通过 aop:pointcut 来声明一个【切点】 ( 这个切点在 aop:aspect 内部,所以是局部切点 ) -->
<!-- 通过 aop:pointcut 标签的 expression 来指定切点表达式 -->
<aop:pointcut id="firstPointcut" expression="execution(* com.mu.aop.schema.Tortoise.*())" />
<!-- 在 firstPointcut 所选择 【连接点】( 指定的 执行点 执行之前 ) 处 加入 advices 中的 before 方法 对应的代码 -->
<!-- 在 firstPointcut 所选择的那些方法之前前 先执行 advices 对象 before 方法-->
<aop:before pointcut-ref="firstPointcut" method="abc" />
<aop:after pointcut-ref="firstPointcut" method="xyz" />
</aop:aspect>
</aop:config>
</beans>
3、测试类
package com.itlaobing.aop.schema;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestTortoise {
public static void main(String[] args) {
// 指定 configuration metadata
String configLocations = "classpath*:com/**/schema/aop-schema.xml" ;
// 创建 Spring IoC 容器
AbstractApplicationContext container = new ClassPathXmlApplicationContext( configLocations );
// 从容器中获取 名称是 tortoise 的 bean
Object proxy = container.getBean( "tortoise" );
// 因为配置了 <aop:config> 并且通过 切点表达式 选中了 Tortoise 类的所有方法
// 因此 从容器中获取的 tortoise 将是 <bean id="tortoise" class="com.itlaobing.aop.schema.Tortoise" /> 的 代理对象
System.out.println( "Class => " + proxy.getClass().getCanonicalName() );
// 调用 代理对象 的 toString 方法 ,Spring 会将该调用指派给 代理目标 的 toString
System.out.println( proxy.toString() );
// 因为 动态产生的代理类 也实现了 Swimmable 接口,因此这里需要将 proxy 强制类型转换为 Swimmable 类型
if( proxy instanceof Swimmable ) {
Swimmable s = (Swimmable) proxy ;
s.swim(); // 调用 代理对象的 swim 方法 最终会指派给 代理目标的 swim
}
container.close();
}
}