Skip to content

Commit 576f109

Browse files
committed
Use JDK proxy for introduction interface without target
Closes gh-33985
1 parent 384dc2a commit 576f109

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,12 @@ public class DefaultAopProxyFactory implements AopProxyFactory, Serializable {
6060
public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
6161
if (config.isOptimize() || config.isProxyTargetClass() || !config.hasUserSuppliedInterfaces()) {
6262
Class<?> targetClass = config.getTargetClass();
63-
if (targetClass == null) {
63+
if (targetClass == null && config.getProxiedInterfaces().length == 0) {
6464
throw new AopConfigException("TargetSource cannot determine target class: " +
6565
"Either an interface or a target is required for proxy creation.");
6666
}
67-
if (targetClass.isInterface() || Proxy.isProxyClass(targetClass) || ClassUtils.isLambdaClass(targetClass)) {
67+
if (targetClass == null || targetClass.isInterface() ||
68+
Proxy.isProxyClass(targetClass) || ClassUtils.isLambdaClass(targetClass)) {
6869
return new JdkDynamicAopProxy(config);
6970
}
7071
return new ObjenesisCglibAopProxy(config);

spring-aop/src/test/java/org/springframework/aop/framework/ProxyFactoryTests.java

+12
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,18 @@ void proxyTargetClassInCaseOfIntroducedInterface() {
340340
assertThat(AopProxyUtils.ultimateTargetClass(proxy)).isEqualTo(MyDate.class);
341341
}
342342

343+
@Test
344+
void proxyInterfaceInCaseOfIntroducedInterfaceOnly() {
345+
ProxyFactory pf = new ProxyFactory();
346+
pf.addInterface(TimeStamped.class);
347+
TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor(0L);
348+
pf.addAdvisor(new DefaultIntroductionAdvisor(ti, TimeStamped.class));
349+
Object proxy = pf.getProxy();
350+
assertThat(AopUtils.isJdkDynamicProxy(proxy)).as("Proxy is a JDK proxy").isTrue();
351+
assertThat(proxy).isInstanceOf(TimeStamped.class);
352+
assertThat(AopProxyUtils.ultimateTargetClass(proxy)).isEqualTo(proxy.getClass());
353+
}
354+
343355
@Test
344356
void proxyInterfaceInCaseOfNonTargetInterface() {
345357
ProxyFactory pf = new ProxyFactory();

0 commit comments

Comments
 (0)