回眸一笑百媚生,六宫粉黛无颜色。
在前四篇,我们已经实现了使用Cglib
实现了aop
动态代理。但是在spring
中如果代理对象实现了接口,则默认使用jdk
动态代理,也可以通过配置强制使用cglib
代理。本篇,我们使用jdk
动态代理来完善aop
新增接口类,因为使用jdk
动态代理,代理对象必须实现接口。
package com.niocoder.service.v6;
/**
* 测试 JDK 动态代理
*/
public interface INioCoderService {
void placeOrder();
}
实现INioCoderService
接口。
@Component(value = "nioCoder")
public class NioCoderService implements INioCoderService {
public NioCoderService() {
}
public void placeOrder() {
System.out.println("place order");
MessageTracker.addMsg("place order");
}
public void placeOrderV2() {
System.out.println("no interception");
}
}
设置Pointcut
为v6
包下面的placeOrder
方法。
<?xml version="1.0" encoding="UTF-8"?>
<!-- 增加namespace-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop">
<!-- 扫描哪个包下面的文件 -->
<context:component-scan base-package="com.niocoder.service.v6">
</context:component-scan>
<!-- 模拟 TransactionManager-->
<bean id="tx" class="com.niocoder.tx.TransactionManager"/>
<!-- aop 配置-->
<aop:config>
<!-- aop 核心配置 依赖tx-->
<aop:aspect ref="tx">
<!-- 切入点配置 包下面的placeOrder 方法-->
<aop:pointcut id="placeOrder"
expression="execution(* com.niocoder.service.v6.*.placeOrder(..))"/>
<!-- 通知配置,-->
<aop:before pointcut-ref="placeOrder" method="start"/>
<aop:after-returning pointcut-ref="placeOrder" method="commit"/>
<aop:after-throwing pointcut-ref="placeOrder" method="rollback"/>
</aop:aspect>
</aop:config>
</beans>
根据类图aop 动态获取代理实例 创建JdkAopProxyFactory
代理工厂。
package com.niocoder.aop.framework;
import com.niocoder.aop.Advice;
import com.niocoder.util.Assert;
import com.niocoder.util.ClassUtils;
import lombok.extern.java.Log;
import org.aopalliance.intercept.MethodInterceptor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;
/**
* @author zhenglongfei
*/
@Log
public class JdkAopProxyFactory implements AopProxyFactory, InvocationHandler {
private final AopConfig config;
public JdkAopProxyFactory(AopConfig config) throws AopConfigException {
Assert.notNull(config, "AdvisedSupport must not be null");
if (config.getAdvices().size() == 0) {
throw new AopConfigException("No advices specified");
}
this.config = config;
}
@Override
public Object getProxy() {
return getProxy(ClassUtils.getDefaultClassLoader());
}
@Override
public Object getProxy(ClassLoader classLoader) {
log.info("Creating JDK dynamic proxy: target source is " + this.config.getTargetObject());
Class<?>[] proxiedInterfaces = config.getProxiedInterfaces();
return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object target = this.config.getTargetObject();
Object retVal;
List<Advice> chain = this.config.getAdvices(method);
if (chain.isEmpty()) {
retVal = method.invoke(target, args);
} else {
List<MethodInterceptor> interceptors = new ArrayList<MethodInterceptor>();
interceptors.addAll(chain);
// We need to create a method invocation...
retVal = new ReflectiveMethodInvocation(target, method, args, interceptors).proceed();
}
return retVal;
}
}
在AspectJAutoProxyCreator
的createProxy
方法中添加JDK
代理支持。
public class AspectJAutoProxyCreator implements BeanPostProcessor {
......
protected Object createProxy(List<Advice> advices, Object bean) {
AopConfigSupport config = new AopConfigSupport();
for (Advice advice : advices) {
config.addAdvice(advice);
}
Set<Class> targetInterfaces = ClassUtils.getAllInterfacesForClassAsSet(bean.getClass());
for (Class<?> targetInterface : targetInterfaces) {
config.addInterface(targetInterface);
}
config.setTargetObject(bean);
AopProxyFactory proxyFactory = null;
if (config.getProxiedInterfaces().length == 0) {
proxyFactory = new CglibProxyFactory(config);
} else {
// JDK 代理
proxyFactory = new JdkAopProxyFactory(config);
}
return proxyFactory.getProxy();
}
......
}
测试jdk
动态代理。
public class ApplicationContextTest {
@Before
public void setUp() {
MessageTracker.clearMsgs();
}
@Test
public void testGetBeanProperty() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-v6.xml");
INioCoderService nioCoderService = (INioCoderService) ctx.getBean("nioCoder");
nioCoderService.placeOrder();
List<String> msgs = MessageTracker.getMsgs();
Assert.assertEquals(3, msgs.size());
Assert.assertEquals("start tx", msgs.get(0));
Assert.assertEquals("place order", msgs.get(1));
Assert.assertEquals("commit tx", msgs.get(2));
}
}
输出:
start tx
place order
commit tx