类JdkDynamicAopProxy
package org.springframework.aop.framework;
import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.AopInvocationException;
import org.springframework.aop.RawTargetAccess;
import org.springframework.aop.TargetSource;
import org.springframework.aop.support.AopUtils;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/**
* springAOP框架的基于JDK的AopProxy实现
* 创建一个动态代理,实现AopProxy暴露的接口。动态代理不能用于代理方法,这些方法在类中定义,而不是在接口定义
* 该类型的对象必须通过代理工厂获取,根据AdvisedSupport进行配置。
* 该类是springAOP框架的内部类,不能直接供客户端代码使用。
* 如果目标类是线程安全,那用该类创建的代理也是线程安全的。
* 只要顾问(包括通知和切入点)和目标源是序列化的,这些代理也是序列化的
*/
final class JdkDynamicAopProxy implements AopProxy, InvocationHandler, Serializable {
/** use serialVersionUID from Spring 1.2 for interoperability */
private static final long serialVersionUID = 5531744639992436476 L;
/** We use a static Log to avoid serialization issues */
private static final Log logger = LogFactory.getLog(JdkDynamicAopProxy.class);
/** 代理配置 */
private final AdvisedSupport advised;
/**
* 代理接口是否定义了equals方法?
*/
private boolean equalsDefined;
/**
* 代理接口是否定义了hashCode方法?
*/
private boolean hashCodeDefined;
/**
* 构造一个新的JdkDynamicAopProxy,根据给定的AOP配置。
* @param config AdvisedSupport配置类
* @throws AopConfigException 如果配置是失效的,我们抛出一个有相关信息的异常,而不是一个未知的失败
*/
public JdkDynamicAopProxy (AdvisedSupport config) throws AopConfigException {
Assert.notNull(config, "AdvisedSupport must not be null" );
if (config.getAdvisors().length == 0 && config.getTargetSource() == AdvisedSupport.EMPTY_TARGET_SOURCE) {
throw new AopConfigException("No advisors and no TargetSource specified" );
}
this .advised = config;
}
@Override
public Object getProxy () {
return getProxy(ClassUtils.getDefaultClassLoader());
}
@Override
public Object getProxy (ClassLoader classLoader) {
if (logger.isDebugEnabled()) {
logger.debug("Creating JDK dynamic proxy: target source is " + this .advised.getTargetSource());
}
Class>[] proxiedInterfaces = AopProxyUtils.completeProxiedInterfaces(this .advised);
findDefinedEqualsAndHashCodeMethods(proxiedInterfaces);
return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this );
}
/**
* 在提供的接口中查找已定义的equals或hashCode方法
* @param proxiedInterfaces 查找的接口
*/
private void findDefinedEqualsAndHashCodeMethods (Class>[] proxiedInterfaces) {
for (Class> proxiedInterface : proxiedInterfaces) {
Method[] methods = proxiedInterface.getDeclaredMethods();
for (Method method : methods) {
if (AopUtils.isEqualsMethod(method)) {
this .equalsDefined = true ;
}
if (AopUtils.isHashCodeMethod(method)) {
this .hashCodeDefined = true ;
}
if (this .equalsDefined && this .hashCodeDefined) {
return ;
}
}
}
}
/**
* InvocationHandler.invoke的实现。调用者将会看到由目标对象抛出明确的异常,除非一个锚定的方法抛出异常。
*/
@Override
public Object invoke (Object proxy, Method method, Object[] args) throws Throwable {
MethodInvocation invocation;
Object oldProxy = null ;
boolean setProxyContext = false ;
TargetSource targetSource = this .advised.targetSource;
Class> targetClass = null ;
Object target = null ;
try {
if (!this .equalsDefined && AopUtils.isEqualsMethod(method)) {
return equals(args[0 ]);
}
if (!this .hashCodeDefined && AopUtils.isHashCodeMethod(method)) {
return hashCode();
}
if (!this .advised.opaque && method.getDeclaringClass().isInterface() &&
method.getDeclaringClass().isAssignableFrom(Advised.class)) {
return AopUtils.invokeJoinpointUsingReflection(this .advised, method, args);
}
Object retVal;
if (this .advised.exposeProxy) {
oldProxy = AopContext.setCurrentProxy(proxy);
setProxyContext = true ;
}
target = targetSource.getTarget();
if (target != null ) {
targetClass = target.getClass();
}
List chain = this .advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);
if (chain.isEmpty()) {
retVal = AopUtils.invokeJoinpointUsingReflection(target, method, args);
}
else {
invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);
retVal = invocation.proceed();
}
Class> returnType = method.getReturnType();
if (retVal != null && retVal == target && returnType.isInstance(proxy) &&
!RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) {
retVal = proxy;
}
else if (retVal == null && returnType != Void.TYPE && returnType.isPrimitive()) {
throw new AopInvocationException(
"Null return value from advice does not match primitive return type for: " + method);
}
return retVal;
}
finally {
if (target != null && !targetSource.isStatic()) {
targetSource.releaseTarget(target);
}
if (setProxyContext) {
AopContext.setCurrentProxy(oldProxy);
}
}
}
/**
* 接口、顾问、目标源是否相等。
* 被比较的对象可能为JdkDynamicAopProxy实例,或者为动态代理的JdkDynamicAopProxy实例
*/
@Override
public boolean equals (Object other) {
if (other == this ) {
return true ;
}
if (other == null ) {
return false ;
}
JdkDynamicAopProxy otherProxy;
if (other instanceof JdkDynamicAopProxy) {
otherProxy = (JdkDynamicAopProxy) other;
}
else if (Proxy.isProxyClass(other.getClass())) {
InvocationHandler ih = Proxy.getInvocationHandler(other);
if (!(ih instanceof JdkDynamicAopProxy)) {
return false ;
}
otherProxy = (JdkDynamicAopProxy) ih;
}
else {
return false ;
}
return AopProxyUtils.equalsInProxy(this .advised, otherProxy.advised);
}
/**
* Proxy uses the hash code of the TargetSource.
*/
@Override
public int hashCode () {
return JdkDynamicAopProxy.class.hashCode() * 13 + this .advised.getTargetSource().hashCode();
}
}
类ObjenesisCglibAopProxy
package org.springframework.aop.framework;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.cglib.proxy.Callback;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.Factory;
import org.springframework.objenesis.ObjenesisException;
import org.springframework.objenesis.ObjenesisStd;
/**
* 基于Objenesis的CglibAopProxy扩展,用于创建代理实例,没有调用类的构造器
*/
@SuppressWarnings ("serial" )
class ObjenesisCglibAopProxy extends CglibAopProxy {
private static final Log logger = LogFactory.getLog(ObjenesisCglibAopProxy.class );
private final ObjenesisStd objenesis;
/**
* 根据给定的AdvisedSupport创建一个新的ObjenesisCglibAopProxy
* @param config 不能为空
*/
public ObjenesisCglibAopProxy(AdvisedSupport config) {
super (config);
this .objenesis = new ObjenesisStd(true );
}
@Override
@SuppressWarnings ("unchecked" )
protected Object createProxyClassAndInstance(Enhancer enhancer, Callback[] callbacks) {
try {
Factory factory = (Factory) this .objenesis.newInstance(enhancer.createClass());
factory.setCallbacks(callbacks);
return factory;
}
catch (ObjenesisException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Unable to instantiate proxy using Objenesis, falling back to regular proxy construction" , ex);
}
return super .createProxyClassAndInstance(enhancer, callbacks);
}
}
}
类DefaultAdvisorChainFactory
package org.springframework.aop.framework;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.aopalliance.intercept.Interceptor;
import org.aopalliance.intercept.MethodInterceptor;
import org.springframework.aop.Advisor;
import org.springframework.aop.IntroductionAdvisor;
import org.springframework.aop.MethodMatcher;
import org.springframework.aop.PointcutAdvisor;
import org.springframework.aop.framework.adapter.AdvisorAdapterRegistry;
import org.springframework.aop.framework.adapter.GlobalAdvisorAdapterRegistry;
import org.springframework.aop.support.MethodMatchers;
/**
* 一个简单但明确的方法去一个方法(Method)的通知链,根据Advised对象。总是重建每个通知链,子类提供缓存。
*/
@SuppressWarnings ("serial" )
public class DefaultAdvisorChainFactory implements AdvisorChainFactory , Serializable {
@Override
public List getInterceptorsAndDynamicInterceptionAdvice (
Advised config, Method method, Class> targetClass) {
List interceptorList = new ArrayList(config.getAdvisors().length);
Class> actualClass = (targetClass != null ? targetClass : method.getDeclaringClass());
boolean hasIntroductions = hasMatchingIntroductions(config, actualClass);
AdvisorAdapterRegistry registry = GlobalAdvisorAdapterRegistry.getInstance();
for (Advisor advisor : config.getAdvisors()) {
if (advisor instanceof PointcutAdvisor) {
PointcutAdvisor pointcutAdvisor = (PointcutAdvisor) advisor;
if (config.isPreFiltered() || pointcutAdvisor.getPointcut().getClassFilter().matches(actualClass)) {
MethodInterceptor[] interceptors = registry.getInterceptors(advisor);
MethodMatcher mm = pointcutAdvisor.getPointcut().getMethodMatcher();
if (MethodMatchers.matches(mm, method, actualClass, hasIntroductions)) {
if (mm.isRuntime()) {
for (MethodInterceptor interceptor : interceptors) {
interceptorList.add(new InterceptorAndDynamicMethodMatcher(interceptor, mm));
}
}
else {
interceptorList.addAll(Arrays.asList(interceptors));
}
}
}
}
else if (advisor instanceof IntroductionAdvisor) {
IntroductionAdvisor ia = (IntroductionAdvisor) advisor;
if (config.isPreFiltered() || ia.getClassFilter().matches(actualClass)) {
Interceptor[] interceptors = registry.getInterceptors(advisor);
interceptorList.addAll(Arrays.asList(interceptors));
}
}
else {
Interceptor[] interceptors = registry.getInterceptors(advisor);
interceptorList.addAll(Arrays.asList(interceptors));
}
}
return interceptorList;
}
/**
* 检查顾问是否匹配说明
*/
private static boolean hasMatchingIntroductions (Advised config, Class> actualClass) {
for (int i = 0 ; i < config.getAdvisors().length; i++) {
Advisor advisor = config.getAdvisors()[i];
if (advisor instanceof IntroductionAdvisor) {
IntroductionAdvisor ia = (IntroductionAdvisor) advisor;
if (ia.getClassFilter().matches(actualClass)) {
return true ;
}
}
}
return false ;
}
}
类DefaultAopProxyFactory
package org.springframework.aop.framework;
import java.io.Serializable;
import org.springframework.aop.SpringProxy;
/**
* 默认的AopProxyFactory实现,创建CGLIB代理或者JDK动态代理
* 创建一个CGLIB代理,如果AdvisedSupport实例的其中一个属性为true:
* 1. optimize 2.proxyTargetClass 3.没有明确的代理接口
* 一般情况下,明确的proxyTargetClass会强制生成CGLIB代理,或者一个或多个的明确接口生成JDK动态代理
*/
@SuppressWarnings ("serial" )
public class DefaultAopProxyFactory implements AopProxyFactory , Serializable {
@Override
public AopProxy createAopProxy (AdvisedSupport config) throws AopConfigException {
if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {
Class> targetClass = config.getTargetClass();
if (targetClass == null ) {
throw new AopConfigException("TargetSource cannot determine target class: " +
"Either an interface or a target is required for proxy creation." );
}
if (targetClass.isInterface()) {
return new JdkDynamicAopProxy(config);
}
return new ObjenesisCglibAopProxy(config);
}
else {
return new JdkDynamicAopProxy(config);
}
}
/**
* 判断AdvisedSupport是否有明确的SpringProxy接口,或者没有明确的代理接口
*/
private boolean hasNoUserSuppliedProxyInterfaces (AdvisedSupport config) {
Class>[] interfaces = config.getProxiedInterfaces();
return (interfaces.length == 0 || (interfaces.length == 1 && SpringProxy.class.equals(interfaces[0 ])));
}
}
类InterceptorAndDynamicMethodMatcher
package org.springframework.aop.framework;
import org.aopalliance.intercept.MethodInterceptor;
import org.springframework.aop.MethodMatcher;
/**
* 内部框架类,由MethodInterceptor实例组成,该实例利用MethodMatcher匹配通知链的元素
* @author Rod Johnson
*/
class InterceptorAndDynamicMethodMatcher {
final MethodInterceptor interceptor;
final MethodMatcher methodMatcher;
public InterceptorAndDynamicMethodMatcher (MethodInterceptor interceptor, MethodMatcher methodMatcher) {
this .interceptor = interceptor;
this .methodMatcher = methodMatcher;
}
}