以下是通过搜索+看源码分析后的个人理解:
看到这个问题,第一反应是很矛盾的,又不是量子态。所以是不是版本的问题?官网有Spring3AOP的说明:
里面提到使用CGLIB的时候被代理对象构造器会被调用两次,两个对象被创建,一个是被代理对象,一个是实现了切面的子类实例,在使用JDK的动态代理的时候是不会有这种问题的。
而在Spring4开始,Spring解决了这个问题,通过Objenesis库创建了CGLIB代理对象,不再使用构造器。相关类:ObjenesisCglibAopProxy.java,源码如下:
@Override
protected Object createProxyClassAndInstance(Enhancer enhancer, Callback[] callbacks) {
Class<?> proxyClass = enhancer.createClass();
Object proxyInstance = null;
// 首先就是尝试使用objenesis库去创建代理实例
if (objenesis.isWorthTrying()) {
try {
proxyInstance = objenesis.newInstance(proxyClass, enhancer.getUseCache());
}
catch (Throwable ex) {
logger.debug("Unable to instantiate proxy using Objenesis, " +
"falling back to regular proxy construction", ex);
}
}
// objenesis未能创建或未启用才会走和Spring3一样的逻辑
if (proxyInstance == null) {
// Regular instantiation via default constructor...
try {
Constructor<?> ctor = (this.constructorArgs != null ?
proxyClass.getDeclaredConstructor(this.constructorArgTypes) :
proxyClass.getDeclaredConstructor());
ReflectionUtils.makeAccessible(ctor);
proxyInstance = (this.constructorArgs != null ?
ctor.newInstance(this.constructorArgs) : ctor.newInstance());
}
catch (Throwable ex) {
throw new AopConfigException("Unable to instantiate proxy using Objenesis, " +
"and regular proxy instantiation via default constructor fails as well", ex);
}
}
((Factory) proxyInstance).setCallbacks(callbacks);
return proxyInstance;
}