Introductions(引用),在 Aspect 中称为类型间的声明,使切面能够声明被通知的对象(拦截的对象)实现给定的接口,并提供该接口的实现。

  简单点说可以将一个类的实现方法复制到未实现的类中。

  通过 @DeclareParents 注解进行声明,声明在一个父类型的属性上(比如接口),其中注解的属性 value 指定对哪些路径(包)下的类进行类方法扩展,defaultImpl 指定以哪个类为模板。

  如下案例:

  1)切面类:

@Aspect
@Component
public class LogAspects {
@DeclareParents(value = "com.hrh.aop.*", defaultImpl = IndexDao.class)
public static Dao dao; @Pointcut("execution(public * com.hrh.aop.*.*(..))")
public void pointcut() { } @Before("pointcut()")
public void LogStart() {
System.out.println("LogStart...");
}
}

  2)接口和实现类:

public interface Dao{
void query();
} @Repository
public class IndexDao implements Dao{
public void query(){
System.out.println("query:"+this.getClass().getSimpleName());
}
}

  3)配置类:

@Configuration
@EnableAspectJAutoProxy
@ComponentScan("com.hrh.aop")
public class Config {
}

  4)空类:

@Repository("orderDao")
public class OrderDao{}

  5)测试和结果:

    public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
Dao bean = (Dao) context.getBean("indexDao");
bean.query();
System.out.println("----------");
Dao dao = (Dao) context.getBean("orderDao");
dao.query();
}
=========结果==========
LogStart...
query:IndexDao
----------
query:IndexDao

  根据前文Spring笔记(3) - SpringAOP基础详解和源码探究可以得知如下:

  • @EnableAspectJAutoProxy(proxyTargetClass=false),表示使用JDK动态代理(默认);
  • @EnableAspectJAutoProxy(proxyTargetClass=true),表示使用CGLIB代理;

  debug运行后查看beanFactory的OrderDao和IndexDao代理后的对象,如下图:

  从上面可以看到IndexDao的对象是使用JDK代理的,而OrderDao的对象是CGLIB代理的。

  那么当设置@EnableAspectJAutoProxy(proxyTargetClass=true)时,是如下图:

  由此可以判定@DeclareParents注解给拦截到的类进行代理使用的是CGLIB代理(其实OrderDao没有实现任何接口,所以最终实现的是CGLIB),那么可以查看下代理类生成的内容是什么,可以在context前面加一行代码将代理类class文件存入本地磁盘:

        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "xxx");//xxx是存放代理类class的本地路径
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);

  下面是代理类class反编译后的完整代码(感兴趣可以完全打开,完全的很多内容,包含了各种父类的方法,比如toString、clone、equals、hashCode):

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
// package com.hrh.aop; import java.lang.reflect.Method;
import java.lang.reflect.UndeclaredThrowableException;
import org.aopalliance.aop.Advice;
import org.springframework.aop.Advisor;
import org.springframework.aop.SpringProxy;
import org.springframework.aop.TargetClassAware;
import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.framework.AopConfigException;
import org.springframework.cglib.core.ReflectUtils;
import org.springframework.cglib.core.Signature;
import org.springframework.cglib.proxy.Callback;
import org.springframework.cglib.proxy.Dispatcher;
import org.springframework.cglib.proxy.Factory;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import org.springframework.cglib.proxy.NoOp; public class OrderDao$$EnhancerBySpringCGLIB$$ee33136 extends OrderDao implements Dao, SpringProxy, Advised, Factory {
private boolean CGLIB$BOUND;
public static Object CGLIB$FACTORY_DATA;
private static final ThreadLocal CGLIB$THREAD_CALLBACKS;
private static final Callback[] CGLIB$STATIC_CALLBACKS;
private MethodInterceptor CGLIB$CALLBACK_0;
private MethodInterceptor CGLIB$CALLBACK_1;
private NoOp CGLIB$CALLBACK_2;
private Dispatcher CGLIB$CALLBACK_3;
private Dispatcher CGLIB$CALLBACK_4;
private MethodInterceptor CGLIB$CALLBACK_5;
private MethodInterceptor CGLIB$CALLBACK_6;
private static Object CGLIB$CALLBACK_FILTER;
private static final Method CGLIB$equals$0$Method;
private static final MethodProxy CGLIB$equals$0$Proxy;
private static final Object[] CGLIB$emptyArgs;
private static final Method CGLIB$toString$1$Method;
private static final MethodProxy CGLIB$toString$1$Proxy;
private static final Method CGLIB$hashCode$2$Method;
private static final MethodProxy CGLIB$hashCode$2$Proxy;
private static final Method CGLIB$clone$3$Method;
private static final MethodProxy CGLIB$clone$3$Proxy;
private static final Method CGLIB$query$4$Method;
private static final MethodProxy CGLIB$query$4$Proxy; static void CGLIB$STATICHOOK5() {
CGLIB$THREAD_CALLBACKS = new ThreadLocal();
CGLIB$emptyArgs = new Object[0];
Class var0 = Class.forName("com.hrh.aop.OrderDao$$EnhancerBySpringCGLIB$$ee33136");
Class var1;
Method[] var10000 = ReflectUtils.findMethods(new String[]{"equals", "(Ljava/lang/Object;)Z", "toString", "()Ljava/lang/String;", "hashCode", "()I", "clone", "()Ljava/lang/Object;"}, (var1 = Class.forName("java.lang.Object")).getDeclaredMethods());
CGLIB$equals$0$Method = var10000[0];
CGLIB$equals$0$Proxy = MethodProxy.create(var1, var0, "(Ljava/lang/Object;)Z", "equals", "CGLIB$equals$0");
CGLIB$toString$1$Method = var10000[1];
CGLIB$toString$1$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/String;", "toString", "CGLIB$toString$1");
CGLIB$hashCode$2$Method = var10000[2];
CGLIB$hashCode$2$Proxy = MethodProxy.create(var1, var0, "()I", "hashCode", "CGLIB$hashCode$2");
CGLIB$clone$3$Method = var10000[3];
CGLIB$clone$3$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/Object;", "clone", "CGLIB$clone$3");
CGLIB$query$4$Method = ReflectUtils.findMethods(new String[]{"query", "()V"}, (var1 = Class.forName("com.hrh.aop.Dao")).getDeclaredMethods())[0];
CGLIB$query$4$Proxy = MethodProxy.create(var1, var0, "()V", "query", "CGLIB$query$4");
} final boolean CGLIB$equals$0(Object var1) {
return super.equals(var1);
} public final boolean equals(Object var1) {
try {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_5;
if (this.CGLIB$CALLBACK_5 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_5;
} if (var10000 != null) {
Object var4 = var10000.intercept(this, CGLIB$equals$0$Method, new Object[]{var1}, CGLIB$equals$0$Proxy);
return var4 == null ? false : (Boolean)var4;
} else {
return super.equals(var1);
}
} catch (Error | RuntimeException var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
} final String CGLIB$toString$1() {
return super.toString();
} public final String toString() {
try {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (this.CGLIB$CALLBACK_0 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
} return var10000 != null ? (String)var10000.intercept(this, CGLIB$toString$1$Method, CGLIB$emptyArgs, CGLIB$toString$1$Proxy) : super.toString();
} catch (Error | RuntimeException var1) {
throw var1;
} catch (Throwable var2) {
throw new UndeclaredThrowableException(var2);
}
} final int CGLIB$hashCode$2() {
return super.hashCode();
} public final int hashCode() {
try {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_6;
if (this.CGLIB$CALLBACK_6 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_6;
} if (var10000 != null) {
Object var3 = var10000.intercept(this, CGLIB$hashCode$2$Method, CGLIB$emptyArgs, CGLIB$hashCode$2$Proxy);
return var3 == null ? 0 : ((Number)var3).intValue();
} else {
return super.hashCode();
}
} catch (Error | RuntimeException var1) {
throw var1;
} catch (Throwable var2) {
throw new UndeclaredThrowableException(var2);
}
} final Object CGLIB$clone$3() throws CloneNotSupportedException {
return super.clone();
} protected final Object clone() throws CloneNotSupportedException {
try {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (this.CGLIB$CALLBACK_0 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
} return var10000 != null ? var10000.intercept(this, CGLIB$clone$3$Method, CGLIB$emptyArgs, CGLIB$clone$3$Proxy) : super.clone();
} catch (Error | CloneNotSupportedException | RuntimeException var1) {
throw var1;
} catch (Throwable var2) {
throw new UndeclaredThrowableException(var2);
}
} final void CGLIB$query$4() {
super.query();
} public final void query() {
try {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (this.CGLIB$CALLBACK_0 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
} if (var10000 != null) {
var10000.intercept(this, CGLIB$query$4$Method, CGLIB$emptyArgs, CGLIB$query$4$Proxy);
} else {
super.query();
}
} catch (Error | RuntimeException var1) {
throw var1;
} catch (Throwable var2) {
throw new UndeclaredThrowableException(var2);
}
} public static MethodProxy CGLIB$findMethodProxy(Signature var0) {
String var10000 = var0.toString();
switch(var10000.hashCode()) {
case -1166709331:
if (var10000.equals("query()V")) {
return CGLIB$query$4$Proxy;
}
break;
case -508378822:
if (var10000.equals("clone()Ljava/lang/Object;")) {
return CGLIB$clone$3$Proxy;
}
break;
case 1826985398:
if (var10000.equals("equals(Ljava/lang/Object;)Z")) {
return CGLIB$equals$0$Proxy;
}
break;
case 1913648695:
if (var10000.equals("toString()Ljava/lang/String;")) {
return CGLIB$toString$1$Proxy;
}
break;
case 1984935277:
if (var10000.equals("hashCode()I")) {
return CGLIB$hashCode$2$Proxy;
}
} return null;
} public final int indexOf(Advice var1) {
try {
Dispatcher var10000 = this.CGLIB$CALLBACK_4;
if (this.CGLIB$CALLBACK_4 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_4;
} return ((Advised)var10000.loadObject()).indexOf(var1);
} catch (Error | RuntimeException var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
} public final int indexOf(Advisor var1) {
try {
Dispatcher var10000 = this.CGLIB$CALLBACK_4;
if (this.CGLIB$CALLBACK_4 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_4;
} return ((Advised)var10000.loadObject()).indexOf(var1);
} catch (Error | RuntimeException var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
} public final boolean isFrozen() {
try {
Dispatcher var10000 = this.CGLIB$CALLBACK_4;
if (this.CGLIB$CALLBACK_4 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_4;
} return ((Advised)var10000.loadObject()).isFrozen();
} catch (Error | RuntimeException var1) {
throw var1;
} catch (Throwable var2) {
throw new UndeclaredThrowableException(var2);
}
} public final void setTargetSource(TargetSource var1) {
try {
Dispatcher var10000 = this.CGLIB$CALLBACK_4;
if (this.CGLIB$CALLBACK_4 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_4;
} ((Advised)var10000.loadObject()).setTargetSource(var1);
} catch (Error | RuntimeException var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
} public final Advisor[] getAdvisors() {
try {
Dispatcher var10000 = this.CGLIB$CALLBACK_4;
if (this.CGLIB$CALLBACK_4 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_4;
} return ((Advised)var10000.loadObject()).getAdvisors();
} catch (Error | RuntimeException var1) {
throw var1;
} catch (Throwable var2) {
throw new UndeclaredThrowableException(var2);
}
} public final boolean isProxyTargetClass() {
try {
Dispatcher var10000 = this.CGLIB$CALLBACK_4;
if (this.CGLIB$CALLBACK_4 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_4;
} return ((Advised)var10000.loadObject()).isProxyTargetClass();
} catch (Error | RuntimeException var1) {
throw var1;
} catch (Throwable var2) {
throw new UndeclaredThrowableException(var2);
}
} public final void addAdvice(Advice var1) throws AopConfigException {
try {
Dispatcher var10000 = this.CGLIB$CALLBACK_4;
if (this.CGLIB$CALLBACK_4 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_4;
} ((Advised)var10000.loadObject()).addAdvice(var1);
} catch (Error | AopConfigException | RuntimeException var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
} public final void addAdvice(int var1, Advice var2) throws AopConfigException {
try {
Dispatcher var10000 = this.CGLIB$CALLBACK_4;
if (this.CGLIB$CALLBACK_4 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_4;
} ((Advised)var10000.loadObject()).addAdvice(var1, var2);
} catch (Error | AopConfigException | RuntimeException var3) {
throw var3;
} catch (Throwable var4) {
throw new UndeclaredThrowableException(var4);
}
} public final boolean replaceAdvisor(Advisor var1, Advisor var2) throws AopConfigException {
try {
Dispatcher var10000 = this.CGLIB$CALLBACK_4;
if (this.CGLIB$CALLBACK_4 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_4;
} return ((Advised)var10000.loadObject()).replaceAdvisor(var1, var2);
} catch (Error | AopConfigException | RuntimeException var3) {
throw var3;
} catch (Throwable var4) {
throw new UndeclaredThrowableException(var4);
}
} public final void addAdvisor(int var1, Advisor var2) throws AopConfigException {
try {
Dispatcher var10000 = this.CGLIB$CALLBACK_4;
if (this.CGLIB$CALLBACK_4 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_4;
} ((Advised)var10000.loadObject()).addAdvisor(var1, var2);
} catch (Error | AopConfigException | RuntimeException var3) {
throw var3;
} catch (Throwable var4) {
throw new UndeclaredThrowableException(var4);
}
} public final void addAdvisor(Advisor var1) throws AopConfigException {
try {
Dispatcher var10000 = this.CGLIB$CALLBACK_4;
if (this.CGLIB$CALLBACK_4 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_4;
} ((Advised)var10000.loadObject()).addAdvisor(var1);
} catch (Error | AopConfigException | RuntimeException var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
} public final boolean isPreFiltered() {
try {
Dispatcher var10000 = this.CGLIB$CALLBACK_4;
if (this.CGLIB$CALLBACK_4 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_4;
} return ((Advised)var10000.loadObject()).isPreFiltered();
} catch (Error | RuntimeException var1) {
throw var1;
} catch (Throwable var2) {
throw new UndeclaredThrowableException(var2);
}
} public final boolean removeAdvice(Advice var1) {
try {
Dispatcher var10000 = this.CGLIB$CALLBACK_4;
if (this.CGLIB$CALLBACK_4 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_4;
} return ((Advised)var10000.loadObject()).removeAdvice(var1);
} catch (Error | RuntimeException var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
} public final void removeAdvisor(int var1) throws AopConfigException {
try {
Dispatcher var10000 = this.CGLIB$CALLBACK_4;
if (this.CGLIB$CALLBACK_4 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_4;
} ((Advised)var10000.loadObject()).removeAdvisor(var1);
} catch (Error | AopConfigException | RuntimeException var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
} public final boolean removeAdvisor(Advisor var1) {
try {
Dispatcher var10000 = this.CGLIB$CALLBACK_4;
if (this.CGLIB$CALLBACK_4 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_4;
} return ((Advised)var10000.loadObject()).removeAdvisor(var1);
} catch (Error | RuntimeException var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
} public final TargetSource getTargetSource() {
try {
Dispatcher var10000 = this.CGLIB$CALLBACK_4;
if (this.CGLIB$CALLBACK_4 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_4;
} return ((Advised)var10000.loadObject()).getTargetSource();
} catch (Error | RuntimeException var1) {
throw var1;
} catch (Throwable var2) {
throw new UndeclaredThrowableException(var2);
}
} public final void setPreFiltered(boolean var1) {
try {
Dispatcher var10000 = this.CGLIB$CALLBACK_4;
if (this.CGLIB$CALLBACK_4 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_4;
} ((Advised)var10000.loadObject()).setPreFiltered(var1);
} catch (Error | RuntimeException var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
} public final boolean isExposeProxy() {
try {
Dispatcher var10000 = this.CGLIB$CALLBACK_4;
if (this.CGLIB$CALLBACK_4 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_4;
} return ((Advised)var10000.loadObject()).isExposeProxy();
} catch (Error | RuntimeException var1) {
throw var1;
} catch (Throwable var2) {
throw new UndeclaredThrowableException(var2);
}
} public final void setExposeProxy(boolean var1) {
try {
Dispatcher var10000 = this.CGLIB$CALLBACK_4;
if (this.CGLIB$CALLBACK_4 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_4;
} ((Advised)var10000.loadObject()).setExposeProxy(var1);
} catch (Error | RuntimeException var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
} public final Class[] getProxiedInterfaces() {
try {
Dispatcher var10000 = this.CGLIB$CALLBACK_4;
if (this.CGLIB$CALLBACK_4 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_4;
} return ((Advised)var10000.loadObject()).getProxiedInterfaces();
} catch (Error | RuntimeException var1) {
throw var1;
} catch (Throwable var2) {
throw new UndeclaredThrowableException(var2);
}
} public final boolean isInterfaceProxied(Class var1) {
try {
Dispatcher var10000 = this.CGLIB$CALLBACK_4;
if (this.CGLIB$CALLBACK_4 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_4;
} return ((Advised)var10000.loadObject()).isInterfaceProxied(var1);
} catch (Error | RuntimeException var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
} public final String toProxyConfigString() {
try {
Dispatcher var10000 = this.CGLIB$CALLBACK_4;
if (this.CGLIB$CALLBACK_4 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_4;
} return ((Advised)var10000.loadObject()).toProxyConfigString();
} catch (Error | RuntimeException var1) {
throw var1;
} catch (Throwable var2) {
throw new UndeclaredThrowableException(var2);
}
} public final Class getTargetClass() {
try {
Dispatcher var10000 = this.CGLIB$CALLBACK_4;
if (this.CGLIB$CALLBACK_4 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_4;
} return ((TargetClassAware)var10000.loadObject()).getTargetClass();
} catch (Error | RuntimeException var1) {
throw var1;
} catch (Throwable var2) {
throw new UndeclaredThrowableException(var2);
}
} public OrderDao$$EnhancerBySpringCGLIB$$ee33136() {
try {
super();
CGLIB$BIND_CALLBACKS(this);
} catch (Error | RuntimeException var1) {
throw var1;
} catch (Throwable var2) {
throw new UndeclaredThrowableException(var2);
}
} public static void CGLIB$SET_THREAD_CALLBACKS(Callback[] var0) {
CGLIB$THREAD_CALLBACKS.set(var0);
} public static void CGLIB$SET_STATIC_CALLBACKS(Callback[] var0) {
CGLIB$STATIC_CALLBACKS = var0;
} private static final void CGLIB$BIND_CALLBACKS(Object var0) {
OrderDao$$EnhancerBySpringCGLIB$$ee33136 var1 = (OrderDao$$EnhancerBySpringCGLIB$$ee33136)var0;
if (!var1.CGLIB$BOUND) {
var1.CGLIB$BOUND = true;
Object var10000 = CGLIB$THREAD_CALLBACKS.get();
if (var10000 == null) {
var10000 = CGLIB$STATIC_CALLBACKS;
if (CGLIB$STATIC_CALLBACKS == null) {
return;
}
} Callback[] var10001 = (Callback[])var10000;
var1.CGLIB$CALLBACK_6 = (MethodInterceptor)((Callback[])var10000)[6];
var1.CGLIB$CALLBACK_5 = (MethodInterceptor)var10001[5];
var1.CGLIB$CALLBACK_4 = (Dispatcher)var10001[4];
var1.CGLIB$CALLBACK_3 = (Dispatcher)var10001[3];
var1.CGLIB$CALLBACK_2 = (NoOp)var10001[2];
var1.CGLIB$CALLBACK_1 = (MethodInterceptor)var10001[1];
var1.CGLIB$CALLBACK_0 = (MethodInterceptor)var10001[0];
} } public Object newInstance(Callback[] var1) {
try {
CGLIB$SET_THREAD_CALLBACKS(var1);
OrderDao$$EnhancerBySpringCGLIB$$ee33136 var10000 = new OrderDao$$EnhancerBySpringCGLIB$$ee33136();
CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
return var10000;
} catch (Error | RuntimeException var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
} public Object newInstance(Callback var1) {
try {
throw new IllegalStateException("More than one callback object required");
} catch (Error | RuntimeException var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
} public Object newInstance(Class[] var1, Object[] var2, Callback[] var3) {
try {
CGLIB$SET_THREAD_CALLBACKS(var3);
OrderDao$$EnhancerBySpringCGLIB$$ee33136 var10000 = new OrderDao$$EnhancerBySpringCGLIB$$ee33136;
switch(var1.length) {
case 0:
var10000.<init>();
CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
return var10000;
default:
throw new IllegalArgumentException("Constructor not found");
}
} catch (Error | RuntimeException var4) {
throw var4;
} catch (Throwable var5) {
throw new UndeclaredThrowableException(var5);
}
} public Callback getCallback(int var1) {
try {
CGLIB$BIND_CALLBACKS(this);
Object var10000;
switch(var1) {
case 0:
var10000 = this.CGLIB$CALLBACK_0;
break;
case 1:
var10000 = this.CGLIB$CALLBACK_1;
break;
case 2:
var10000 = this.CGLIB$CALLBACK_2;
break;
case 3:
var10000 = this.CGLIB$CALLBACK_3;
break;
case 4:
var10000 = this.CGLIB$CALLBACK_4;
break;
case 5:
var10000 = this.CGLIB$CALLBACK_5;
break;
case 6:
var10000 = this.CGLIB$CALLBACK_6;
break;
default:
var10000 = null;
} return (Callback)var10000;
} catch (Error | RuntimeException var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
} public void setCallback(int var1, Callback var2) {
try {
switch(var1) {
case 0:
this.CGLIB$CALLBACK_0 = (MethodInterceptor)var2;
break;
case 1:
this.CGLIB$CALLBACK_1 = (MethodInterceptor)var2;
break;
case 2:
this.CGLIB$CALLBACK_2 = (NoOp)var2;
break;
case 3:
this.CGLIB$CALLBACK_3 = (Dispatcher)var2;
break;
case 4:
this.CGLIB$CALLBACK_4 = (Dispatcher)var2;
break;
case 5:
this.CGLIB$CALLBACK_5 = (MethodInterceptor)var2;
break;
case 6:
this.CGLIB$CALLBACK_6 = (MethodInterceptor)var2;
} } catch (Error | RuntimeException var3) {
throw var3;
} catch (Throwable var4) {
throw new UndeclaredThrowableException(var4);
}
} public Callback[] getCallbacks() {
try {
CGLIB$BIND_CALLBACKS(this);
return new Callback[]{this.CGLIB$CALLBACK_0, this.CGLIB$CALLBACK_1, this.CGLIB$CALLBACK_2, this.CGLIB$CALLBACK_3, this.CGLIB$CALLBACK_4, this.CGLIB$CALLBACK_5, this.CGLIB$CALLBACK_6};
} catch (Error | RuntimeException var1) {
throw var1;
} catch (Throwable var2) {
throw new UndeclaredThrowableException(var2);
}
} public void setCallbacks(Callback[] var1) {
try {
this.CGLIB$CALLBACK_0 = (MethodInterceptor)var1[0];
this.CGLIB$CALLBACK_1 = (MethodInterceptor)var1[1];
this.CGLIB$CALLBACK_2 = (NoOp)var1[2];
this.CGLIB$CALLBACK_3 = (Dispatcher)var1[3];
this.CGLIB$CALLBACK_4 = (Dispatcher)var1[4];
this.CGLIB$CALLBACK_5 = (MethodInterceptor)var1[5];
this.CGLIB$CALLBACK_6 = (MethodInterceptor)var1[6];
} catch (Error | RuntimeException var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
} static {
CGLIB$STATICHOOK6();
CGLIB$STATICHOOK5();
} static void CGLIB$STATICHOOK6() {
try {
;
} catch (Error | RuntimeException var0) {
throw var0;
} catch (Throwable var1) {
throw new UndeclaredThrowableException(var1);
}
}
}

  从上面可以看到代理类里面有query方法,代理类继承了OrderDao和实现了Dao接口:

public class OrderDao$$EnhancerBySpringCGLIB$$ee33136 extends OrderDao implements Dao, SpringProxy, Advised, Factory {
............................ final void CGLIB$query$4() {
super.query();
} public final void query() {
try {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (this.CGLIB$CALLBACK_0 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
} if (var10000 != null) {
var10000.intercept(this, CGLIB$query$4$Method, CGLIB$emptyArgs, CGLIB$query$4$Proxy);
} else {
super.query();
}
} catch (Error | RuntimeException var1) {
throw var1;
} catch (Throwable var2) {
throw new UndeclaredThrowableException(var2);
}
} ............................
}

  从前文Spring笔记(3) - SpringAOP基础详解和源码探究可以得知在 AbstractAutoProxyCreator#wrapIfNecessary()中会对bean进行包装增强(代理),下面打下断点debug跟踪下流程:

  当执行到wrapIfNecessary方法下面的代码时,bean还是原对象(未被代理):

Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);

  当上面的代码执行完后,可以看到specificInterceptors数组包含了advice(通知)、introducedInterface(引用接口)、typePatternClassFilter(class过滤规则,重点),其中advice包含了defaultImplType(默认实现类型,上面代码中定义的),可以看到是IndexDao,interfaceType(接口类型)可以看到是Dao,以上信息是通过解析@DeclareParents注解而来的:

  下面typePatternClassFilter里面的详情,可以看到,上面的信息又包含在typePatternClassFilter里面,同时里面包含了过滤规则:

  最后通过DefaultAopProxyFactory#createAopProxy方法创建的对象如下图:

  当执行query方法时,CglibAopProxy.DynamicAdvisedInterceptor#intercept方法进行拦截时,会执行父类的方法ReflectiveMethodInvocation#ReflectiveMethodInvocation:

  执行完会跳转到ReflectiveMethodInvocation#proceed执行方法的执行:

    public Object proceed() throws Throwable {
// We start with an index of -1 and increment early.
if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
return invokeJoinpoint();
}
//获取通知
Object interceptorOrInterceptionAdvice =
this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);
if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) {
// Evaluate dynamic method matcher here: static part will already have
// been evaluated and found to match.
InterceptorAndDynamicMethodMatcher dm =
(InterceptorAndDynamicMethodMatcher) interceptorOrInterceptionAdvice;
if (dm.methodMatcher.matches(this.method, this.targetClass, this.arguments)) {
return dm.interceptor.invoke(this);
}
else {
// Dynamic matching failed.
// Skip this interceptor and invoke the next in the chain.
return proceed();
}
}
else {
// It's an interceptor, so we just invoke it: The pointcut will have
// been evaluated statically before this object was constructed.
return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);//执行方法
}
}

  下面执行delegate对象获取到IndexDao,然后AopUtils.invokeJoinpointUsingReflection执行IndexDao的query实现方法:

  其中getIntroductionDelegateFor方法中的delegate通过反射对默认实现类型进行构造方法实例化后调用实例化的方法:

  总结:Introductions(引用)通过 @DeclareParents 注解实现,可以给一个空白类进行方法复制,它通过解析@DeclareParents 注解信息,代理时将定义的静态接口变量作为它的父类,并实现它的方法,方法执行时调用的是defaultImpl 指定的类模板的实现方法;

Spring(11) - Introductions进行类扩展方法的更多相关文章

  1. MBProgressHUD 的类扩展方法用法

    #import "MBProgressHUD.h" @interface MBProgressHUD (Add) + (void)showError:(NSString *)err ...

  2. C#语法糖: 扩展方法(常用)

    今天继续分享C#4.0语法糖的扩展方法,这个方法也是我本人比较喜欢的方法.大家先想想比如我们以前写的原始类型不能满足现在的需求,而需要在该类型中添加新的方法来实现时大家会怎么做.我先说一下我没有学习到 ...

  3. .NET 扩展方法

    .NET 的扩展方法是在.NET 3.0引入的,MSDN给出的定义是:扩展方法使你能够向现有类型“添加”方法(包括你自定义的类型和对象噢),而无需创建新的派生类型.重新编译或以其他方式修改原始类型.扩 ...

  4. Linq之扩展方法

    目录 写在前面 系列文章 扩展方法 总结 写在前面 上篇文章介绍了隐式类型,自动属性,初始化器,匿名类的相关概念,及通过反编译的方式查看了编译器帮我们做了那些事.本篇文章将介绍扩展方法的知识点,及如何 ...

  5. MVC中使用HTML Helper类扩展HTML控件

    文章摘自:http://www.cnblogs.com/zhangziqiu/archive/2009/03/18/1415005.html MVC在view页面,经常需要用到很多封装好的HTML控件 ...

  6. C#语法糖之第四篇: 扩展方法

    今天继续分享C#4.0语法糖的扩展方法,这个方法也是我本人比较喜欢的方法.大家先想想比如我们以前写的原始类型不能满足现在的需求,而需要在该类型中添加新的方法来实现时大家会怎么做.我先说一下我没有学习到 ...

  7. 将基于Nullable<T>的类型转换实现在扩展方法中

    三.将基于Nullable<T>的类型转换实现在扩展方法中 从上面的介绍我们可以得出这样的结论:如果类型T1和T2能够相互兼容,我们可以借助Convert将T1类型对象转换成T2类型,然后 ...

  8. OC分类(类目/类别) 和 类扩展 - 全解析

    OC分类(类目/类别) 和 类扩展 - 全解析   具体见: oschina -> MyDemo -> 011.FoundationLog-OC分类剖析 http://blog.csdn. ...

  9. C#扩展方法学习

    扩展方法的本质是什么,详细见此文 C#扩展方法,爱你在心口难开 重点如下:扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型.扩展方法是一种特殊的静态方法 ...

随机推荐

  1. 重量级锁synchronized的优化----自旋锁、自适应自旋锁、锁消除、锁粗化

    synchronized是重量级锁,效率不高.但在jdk 1.6中对synchronize的实现进行了各种优化,使得它显得不是那么重了.jdk1.6对锁的实现引入了大量的优化,如自旋锁.自适应自旋锁. ...

  2. Java基本概念:类

    一.描述 类是一种抽象的数据类型,它是对某一类事物整体的描述或定义,但是并不能代表某一个具体的事物. 例如,我们生活中所说的词语:动物.植物.手机.电脑等等.这些也都是抽象的概念,而不是指的某一个 具 ...

  3. 一文了解Python的迭代器的实现

    本文对迭代器的解释参考自:https://www.programiz.com/python-programming/iterator 最后自己使用迭代器实现一个公平洗牌类. 博主认为,理论来自实践,假 ...

  4. POJ-2752(KMP算法+前缀数组的应用)

    Seek the Name, Seek the Fame POJ-2752 本题使用的算法还是KMP 最主要的片段就是前缀数组pi的理解,这里要求解的纸盒pi[n-1]有关,但是还是需要使用一个循环来 ...

  5. CCF(元素选择器:50分):字符串+模拟

    元素选择器 201809-3 这里我只考虑了没有后代选择器的情况 #include<iostream> #include<cstdio> #include<cstring ...

  6. Java 集合框架 03

    集合框架·HashSet 和 TreeSet HashSet存储字符串并遍历 * A:Set集合概述及特点 * 通过API查看即可 * 无索引,不可以重复,无序 * B:案例演示 * HashSet存 ...

  7. 进阶宝典一|SqlServer数据库自动备份设置

    很多人都没机会接触到数据库备份,经常操作的要么是数据库管理员,要么是项目负责人.那是不是说数据库备份就不用学了? 不,其实作为开发人员应该要了解数据备份,数据备份的手段有很多:软件备份.脚本备份.其他 ...

  8. FreeBSD 12.2 已经发布 从现有版本更新到12

    #freebsd-update -r 12.2-RELEASE upgrade 如果提示更新第三方软件后,再执行freebsd-update install , 请输入 #pkg update &am ...

  9. 001-HashMap源码分析

    HashMap源码分析 哈希表(hash table)也叫散列表,是一种非常重要的数据结构,应用场景及其丰富,许多缓存技术(比如 memcached)的核心其实就是在内存中维护一张大的哈希表. 一.什 ...

  10. 循环3n加1问题

    package 第二章; import java.util.Scanner; //int 32位整数 /*  * 猜想:对于任意大于一的自然数n,若n为奇数,则将n变为3n+1,否则变为n的一半 经过 ...