我们开发小组跟网易的合作项目终于上线了,我自己的一些私人琐事也基本上告一段落,停更了快3周的博客继续开始更新~
这章我们来看看怎么样利用Spring的AOP框架来实现Wall-E送礼物的功能。
首先我们应该像上一篇的InvocationHandler方式实现AOP一样,先定义一个ISpeaker接口:
package com.iteye.bolide74.impl;
public interface ISpeaker {
public void say(String msg);
}
然后呢,就是最熟悉的Robot类,这里我们暂时只采用set注入方式:
package com.iteye.bolide74.action;
import com.iteye.bolide74.impl.ISpeaker;
public class Robot implements ISpeaker {
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void say(String msg) {
System.out.println("到达邻居家,对邻居说:" + msg + ",我是" + this.name);
}
}
以上内容与前两篇的AOP方式基本上没有区别,除了我刻意删除掉了Robot的构造方法,这是因为在Spring的AOP框架中还是比较推荐使用set注入。
下面的GiftBySpring类,就是与之前两种AOP有所区别的地方了:
package com.iteye.bolide74.action;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class GiftBySpring implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation arg0) throws Throwable {
Object result = null;
getGift();
result = arg0.proceed();
giveGift();
return result;
}
private void getGift() {
System.out.println("获取了一个礼物");
}
private void giveGift() {
System.out.println("赠予一个礼物");
}
}
GiftBySpring类实现了
org.aopalliance.intercept.MethodInterceptor接口,
这个接口跟前面一篇的
java.lang.reflect.InvocationHandler接口很相似,都有一个invoke重写方法,但是这两个invoke方法的形参并不一样,这一点一定要区分清楚了。
GiftBySpring类的invoke方法内容与前一篇的GiftDynamicProxy类的invoke方法内容也非常的相似,主要区别在于以下两点:
1、最明显的区别,MethodInterceptor接口的invoke形参是MethodInvocation类型的,它实现代理的方法为:
arg0.proceed();
而前一篇的InvocationHandler接口的invoke方法是利用反射的
method.invoke(this.target, args);方法来实现代理。
2、前一篇的
InvocationHandler接口想要实现自动代理的筛选条件(筛选什么时候要自动送礼、什么时候不需要送礼),是需要在invoke方法内用条件判断来实现的;
而这里的
MethodInterceptor接口不需要再加条件判断了,只需要关注get/giveGift方法就可以,筛选条件我们可以在后面用更方便的方式来实现。
接下来我们需要在项目的ClassPath,也就是
src目录下新建一个XML配置文件来配置Spring的AOP容器:
config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="Robot" class="com.iteye.bolide74.action.Robot">
<!-- set注入Robot的name属性 -->
<property name="name" value="Wall-E" />
</bean>
<bean id="GiftBySpring" class="com.iteye.bolide74.action.GiftBySpring" />
<!-- 牢记这个bean的路径和全名,以后经常要用到,也很好记,直译成中文就是代理工厂Bean -->
<bean id="GiftProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 注入被代理的目标类的接口 -->
<property name="proxyInterfaces">
<value>com.iteye.bolide74.impl.ISpeaker</value>
</property>
<!-- 注入被代理的目标类,也就是Pointcut,注意这里注入的值是ref,而不是value -->
<property name="target">
<ref bean="Robot" />
</property>
<!-- 这个就是自动代理的get/giveGift类,也就是Advice -->
<property name="interceptorNames">
<list>
<value>GiftBySpring</value>
</list>
</property>
</bean>
</beans>
大功告成,这个配置内容
仅仅实现了最基础的送礼物功能,没有实现筛选,也没有实现无需定义接口的自动代理,这个我们要放到后面再着重介绍,下面我们在应用代码里实现用Spring的AOP框架来实现的送礼物功能:
package com.iteye.bolide74.tester;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.iteye.bolide74.impl.ISpeaker;
public class GiftBySpringTester {
public static void main(String[] args) {
// 由于config.xml是放在ClassPath目录下的,所以这里就直接用ClassPathXmlApplicationContext来配置AOP容器
ApplicationContext actx = new ClassPathXmlApplicationContext(
"config.xml");
//注意,这里getBean获取的是GiftProxy这个bean,而不是Robot
ISpeaker WallE = (ISpeaker) actx.getBean("GiftProxy");
WallE.say("你好");
}
}
输出结果:
引用
获取了一个礼物
到达邻居家,对邻居说:你好,我是Wall-E
赠予一个礼物
下一章:Spring温故知新(七)Advice通知的5种类型
http://bolide74.iteye.com/blog/1038865
上一章:Spring温故知新(六)AOP面向切面编程 <2>
http://bolide74.iteye.com/blog/1007828