Skip to content

Commit 846fae2

Browse files
committed
Fixed fallback mode in ObjenesisCglibAopProxy
Issue: SPR-13131
1 parent d08c066 commit 846fae2

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

spring-aop/src/main/java/org/springframework/aop/framework/CglibAopProxy.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -102,9 +102,9 @@ class CglibAopProxy implements AopProxy, Serializable {
102102
/** The configuration used to configure this proxy */
103103
protected final AdvisedSupport advised;
104104

105-
private Object[] constructorArgs;
105+
protected Object[] constructorArgs;
106106

107-
private Class<?>[] constructorArgTypes;
107+
protected Class<?>[] constructorArgTypes;
108108

109109
/** Dispatcher used for methods on Advised */
110110
private final transient AdvisedDispatcher advisedDispatcher;

spring-aop/src/main/java/org/springframework/aop/framework/ObjenesisCglibAopProxy.java

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,10 +26,11 @@
2626
import org.springframework.objenesis.ObjenesisStd;
2727

2828
/**
29-
* Objenesis based extension of {@link CglibAopProxy} to create proxy instances without
30-
* invoking the constructor of the class.
29+
* Objenesis-based extension of {@link CglibAopProxy} to create proxy instances
30+
* without invoking the constructor of the class.
3131
*
3232
* @author Oliver Gierke
33+
* @author Juergen Hoeller
3334
* @since 4.0
3435
*/
3536
@SuppressWarnings("serial")
@@ -41,8 +42,8 @@ class ObjenesisCglibAopProxy extends CglibAopProxy {
4142

4243

4344
/**
44-
* Creates a new {@link ObjenesisCglibAopProxy} using the given {@link AdvisedSupport}.
45-
* @param config must not be {@literal null}.
45+
* Create a new ObjenesisCglibAopProxy for the given AOP configuration.
46+
* @param config the AOP configuration as AdvisedSupport object
4647
*/
4748
public ObjenesisCglibAopProxy(AdvisedSupport config) {
4849
super(config);
@@ -53,18 +54,32 @@ public ObjenesisCglibAopProxy(AdvisedSupport config) {
5354
@Override
5455
@SuppressWarnings("unchecked")
5556
protected Object createProxyClassAndInstance(Enhancer enhancer, Callback[] callbacks) {
57+
Class<?> proxyClass = enhancer.createClass();
58+
Object proxyInstance = null;
59+
5660
try {
57-
Factory factory = (Factory) this.objenesis.newInstance(enhancer.createClass());
58-
factory.setCallbacks(callbacks);
59-
return factory;
61+
proxyInstance = this.objenesis.newInstance(proxyClass);
6062
}
6163
catch (ObjenesisException ex) {
62-
// Fallback to regular proxy construction on unsupported JVMs
63-
if (logger.isDebugEnabled()) {
64-
logger.debug("Unable to instantiate proxy using Objenesis, falling back to regular proxy construction", ex);
64+
logger.debug("Unable to instantiate proxy using Objenesis, " +
65+
"falling back to regular proxy construction", ex);
66+
}
67+
68+
if (proxyInstance == null) {
69+
// Regular instantiation via default constructor...
70+
try {
71+
proxyInstance = (this.constructorArgs != null ?
72+
proxyClass.getConstructor(this.constructorArgTypes).newInstance(this.constructorArgs) :
73+
proxyClass.newInstance());
74+
}
75+
catch (Exception ex) {
76+
throw new AopConfigException("Unable to instantiate proxy using Objenesis, " +
77+
"and regular proxy instantiation via default constructor fails as well", ex);
6578
}
66-
return super.createProxyClassAndInstance(enhancer, callbacks);
6779
}
80+
81+
((Factory) proxyInstance).setCallbacks(callbacks);
82+
return proxyInstance;
6883
}
6984

7085
}

0 commit comments

Comments
 (0)