本章将主要讲解Aopsupprot 和JDkDynamicProxy,写到这里这个简易的aop框架也就要结束了,真个项目我也上传地址为https://github.com/skybluehhx/MyAop.git.
首先看一下AopSupport的实现
package com.support;
import com.MethodConfig;
import com.ProxyConfig;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by zoujianglin
* 2018/8/28 0028.
* 维持着一个类的所有代理配置
*/
public final class AopSupport extends ProxyConfig {
//保持着方法名和该方法的配置的映射关系
private Map<String, MethodConfig> methodConfigMap;
//被代理的对象
private Object target;
//被代理的接口
private List<Class<?>> interfaces;
public AopSupport() {
methodConfigMap = new HashMap<String, MethodConfig>();
interfaces = new ArrayList<Class<?>>();
}
// 添加方法配置器
public void addMethodConfig(String methodName, MethodConfig methodConfig) {
methodConfigMap.put(methodName, methodConfig);
}
public MethodConfig getMethodConfig(String methodName) {
return methodConfigMap.get(methodName);
}
public Object getTarget() {
return target;
}
public void setTarget(Object target) {
this.target = target;
}
public List<Class<?>> getInterfaces() {
return interfaces;
}
public void setInterfaces(List<Class<?>> interfaces) {
this.interfaces = interfaces;
}
//添加代理接口
public void addInterfaces(Class<?>[] interClass) {
for (int i = 0; i < interClass.length; i++) {
if (interClass[i].isInterface()) {
interfaces.add(interClass[i]);
}
}
}
}
其中ProxyConfig定义如下,在实现过程中我并未对一下参数作为处理,纯属参考spring自带Aop的实现,为以后扩展
package com;
/**
* Created by zoujianglin
* 2018/8/28 0028.
*/
public class ProxyConfig {
private boolean proxyTargetClass = false;
private boolean optimize = false;
boolean opaque = false;
boolean exposeProxy = false;
private boolean frozen = false;
public ProxyConfig() {
}
public void setProxyTargetClass(boolean proxyTargetClass) {
this.proxyTargetClass = proxyTargetClass;
}
public boolean isProxyTargetClass() {
return this.proxyTargetClass;
}
public void setOptimize(boolean optimize) {
this.optimize = optimize;
}
public boolean isOptimize() {
return this.optimize;
}
public void setOpaque(boolean opaque) {
this.opaque = opaque;
}
public boolean isOpaque() {
return this.opaque;
}
public void setExposeProxy(boolean exposeProxy) {
this.exposeProxy = exposeProxy;
}
public boolean isExposeProxy() {
return this.exposeProxy;
}
public void setFrozen(boolean frozen) {
this.frozen = frozen;
}
public boolean isFrozen() {
return this.frozen;
}
public void copyFrom(ProxyConfig other) {
if (other == null) {
throw new RuntimeException("other is null");
}
// Assert.notNull(other, "Other ProxyConfig object must not be null");
this.proxyTargetClass = other.proxyTargetClass;
this.optimize = other.optimize;
this.exposeProxy = other.exposeProxy;
this.frozen = other.frozen;
this.opaque = other.opaque;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("proxyTargetClass=").append(this.proxyTargetClass).append("; ");
sb.append("optimize=").append(this.optimize).append("; ");
sb.append("opaque=").append(this.opaque).append("; ");
sb.append("exposeProxy=").append(this.exposeProxy).append("; ");
sb.append("frozen=").append(this.frozen);
return sb.toString();
}
}
下面看我们最后一个涉及到的一个重要类,首先我们定义一个接口,表示获取代理对象的功能,这样就不需要理会时cglib实现代理还是jdk的动态代理实现了
其中AopProxy的定义如下
package com.autoproxy;
/**
* Created by zoujianglin
* 2018/8/28 0028.
*/
public interface AopProxy {
//获取代理
Object getProxy();
Object getProxy(ClassLoader var1);
}
其中jdk动态代理实现的方式如下 JDKDynamicProxy ,实现起来也比较简单
package com.autoproxy;
import com.support.AopSupport;
import com.MethodConfig;
import com.AopMethodInvocation;
import org.aopalliance.intercept.MethodInvocation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* Created by zoujianglin
* 2018/8/29 0029.
*/
public class JDKDynamicProxy implements AopProxy, InvocationHandler {
//被代理类的所有配置信息
private AopSupport aopSupport;
public JDKDynamicProxy(AopSupport aopSupport) {
this.aopSupport = aopSupport;
}
public Object getProxy() {
Class<?>[] proxiedInterfaces = new Class<?>[aopSupport.getInterfaces().size()];
for (int i = 0; i < aopSupport.getInterfaces().size(); i++) {
proxiedInterfaces[i] = aopSupport.getInterfaces().get(i);
}
//返回代理对象
return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), proxiedInterfaces, this);
}
public Object getProxy(ClassLoader var1) {
return null;
}
//具体的调用过程
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
MethodConfig methodConfig = aopSupport.getMethodConfig(method.getName());
MethodInvocation methodInvocation = new AopMethodInvocation(methodConfig.getTarget(), method,
args, methodConfig.getMethodInterceptors());
return methodInvocation.proceed();
}
public AopSupport getAopSupport() {
return aopSupport;
}
}
最后给出我在aop项目中使用的Assert工具类
package com.util;
/**
* Created by zoujianglin
* 2018/8/29 0029.
*/
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.util.Collection;
import java.util.Map;
import java.util.Collection;
import java.util.Map;
public abstract class Assert {
public Assert() {
}
public static void isTrue(boolean expression, String message) {
if(!expression) {
throw new IllegalArgumentException(message);
}
}
public static void isTrue(boolean expression) {
isTrue(expression, "[Assertion failed] - this expression must be true");
}
public static void isNull(Object object, String message) {
if(object != null) {
throw new IllegalArgumentException(message);
}
}
public static void isNull(Object object) {
isNull(object, "[Assertion failed] - the object argument must be null");
}
public static void notNull(Object object, String message) {
if(object == null) {
throw new IllegalArgumentException(message);
}
}
public static void notNull(Object object) {
notNull(object, "[Assertion failed] - this argument is required; it must not be null");
}
public static void hasLength(String text, String message) {
if(!StringUtils.hasLength(text)) {
throw new IllegalArgumentException(message);
}
}
public static void hasLength(String text) {
hasLength(text, "[Assertion failed] - this String argument must have length; it must not be null or empty");
}
public static void hasText(String text, String message) {
if(!StringUtils.hasText(text)) {
throw new IllegalArgumentException(message);
}
}
public static void hasText(String text) {
hasText(text, "[Assertion failed] - this String argument must have text; it must not be null, empty, or blank");
}
public static void doesNotContain(String textToSearch, String substring, String message) {
if(StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) && textToSearch.contains(substring)) {
throw new IllegalArgumentException(message);
}
}
public static void doesNotContain(String textToSearch, String substring) {
doesNotContain(textToSearch, substring, "[Assertion failed] - this String argument must not contain the substring [" + substring + "]");
}
public static void notEmpty(Object[] array, String message) {
if(ObjectUtils.isEmpty(array)) {
throw new IllegalArgumentException(message);
}
}
public static void notEmpty(Object[] array) {
notEmpty(array, "[Assertion failed] - this array must not be empty: it must contain at least 1 element");
}
public static void noNullElements(Object[] array, String message) {
if(array != null) {
Object[] var2 = array;
int var3 = array.length;
for(int var4 = 0; var4 < var3; ++var4) {
Object element = var2[var4];
if(element == null) {
throw new IllegalArgumentException(message);
}
}
}
}
public static void noNullElements(Object[] array) {
noNullElements(array, "[Assertion failed] - this array must not contain any null elements");
}
public static void notEmpty(Collection<?> collection, String message) {
if(CollectionUtils.isEmpty(collection)) {
throw new IllegalArgumentException(message);
}
}
public static void notEmpty(Collection<?> collection) {
notEmpty(collection, "[Assertion failed] - this collection must not be empty: it must contain at least 1 element");
}
public static void notEmpty(Map<?, ?> map, String message) {
if(CollectionUtils.isEmpty(map)) {
throw new IllegalArgumentException(message);
}
}
public static void notEmpty(Map<?, ?> map) {
notEmpty(map, "[Assertion failed] - this map must not be empty; it must contain at least one entry");
}
public static void isInstanceOf(Class<?> clazz, Object obj) {
isInstanceOf(clazz, obj, "");
}
public static void isInstanceOf(Class<?> type, Object obj, String message) {
notNull(type, "Type to check against must not be null");
if(!type.isInstance(obj)) {
throw new IllegalArgumentException((StringUtils.hasLength(message)?message + " ":"") + "Object of class [" + (obj != null?obj.getClass().getName():"null") + "] must be an instance of " + type);
}
}
public static void isAssignable(Class<?> superType, Class<?> subType) {
isAssignable(superType, subType, "");
}
public static void isAssignable(Class<?> superType, Class<?> subType, String message) {
notNull(superType, "Type to check against must not be null");
if(subType == null || !superType.isAssignableFrom(subType)) {
throw new IllegalArgumentException((StringUtils.hasLength(message)?message + " ":"") + subType + " is not assignable to " + superType);
}
}
public static void state(boolean expression, String message) {
if(!expression) {
throw new IllegalStateException(message);
}
}
public static void state(boolean expression) {
state(expression, "[Assertion failed] - this state invariant must be true");
}
}
其中写过之后,你会发现实现一个aop框架就是这么简单!,重要的是有想法,以及如何将想法转化为到具体的实现!。