Browse Source

DefaultAopProxyFactory falls back to JdkDynamicAopProxy when encountering JDK proxy as target

Issue: SPR-12870
pull/759/merge
Juergen Hoeller 10 years ago
parent
commit
f9c2d1d171
  1. 12
      spring-aop/src/main/java/org/springframework/aop/framework/AopProxyUtils.java
  2. 6
      spring-aop/src/main/java/org/springframework/aop/framework/DefaultAopProxyFactory.java
  3. 71
      spring-context/src/test/java/org/springframework/aop/framework/autoproxy/AutoProxyCreatorTests.java

12
spring-aop/src/main/java/org/springframework/aop/framework/AopProxyUtils.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
package org.springframework.aop.framework;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import org.springframework.aop.SpringProxy;
@ -83,8 +84,13 @@ public abstract class AopProxyUtils { @@ -83,8 +84,13 @@ public abstract class AopProxyUtils {
if (specifiedInterfaces.length == 0) {
// No user-specified interfaces: check whether target class is an interface.
Class<?> targetClass = advised.getTargetClass();
if (targetClass != null && targetClass.isInterface()) {
specifiedInterfaces = new Class<?>[] {targetClass};
if (targetClass != null) {
if (targetClass.isInterface()) {
specifiedInterfaces = new Class<?>[] {targetClass};
}
else if (Proxy.isProxyClass(targetClass)) {
specifiedInterfaces = targetClass.getInterfaces();
}
}
}
boolean addSpringProxy = !advised.isInterfaceProxied(SpringProxy.class);

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,6 +17,7 @@ @@ -17,6 +17,7 @@
package org.springframework.aop.framework;
import java.io.Serializable;
import java.lang.reflect.Proxy;
import org.springframework.aop.SpringProxy;
@ -53,7 +54,7 @@ public class DefaultAopProxyFactory implements AopProxyFactory, Serializable { @@ -53,7 +54,7 @@ public class DefaultAopProxyFactory implements AopProxyFactory, Serializable {
throw new AopConfigException("TargetSource cannot determine target class: " +
"Either an interface or a target is required for proxy creation.");
}
if (targetClass.isInterface()) {
if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {
return new JdkDynamicAopProxy(config);
}
return new ObjenesisCglibAopProxy(config);
@ -72,4 +73,5 @@ public class DefaultAopProxyFactory implements AopProxyFactory, Serializable { @@ -72,4 +73,5 @@ public class DefaultAopProxyFactory implements AopProxyFactory, Serializable {
Class<?>[] interfaces = config.getProxiedInterfaces();
return (interfaces.length == 0 || (interfaces.length == 1 && SpringProxy.class.equals(interfaces[0])));
}
}

71
spring-context/src/test/java/org/springframework/aop/framework/autoproxy/AutoProxyCreatorTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,6 +17,8 @@ @@ -17,6 +17,8 @@
package org.springframework.aop.framework.autoproxy;
import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import org.aopalliance.intercept.MethodInterceptor;
@ -42,6 +44,7 @@ import org.springframework.tests.sample.beans.ITestBean; @@ -42,6 +44,7 @@ import org.springframework.tests.sample.beans.ITestBean;
import org.springframework.tests.sample.beans.IndexedTestBean;
import org.springframework.tests.sample.beans.TestBean;
import org.springframework.tests.sample.beans.factory.DummyFactory;
import org.springframework.util.ReflectionUtils;
import static org.junit.Assert.*;
@ -207,6 +210,46 @@ public final class AutoProxyCreatorTests { @@ -207,6 +210,46 @@ public final class AutoProxyCreatorTests {
assertEquals(2, tapc.testInterceptor.nrOfInvocations);
}
@Test
public void testAutoProxyCreatorWithFallbackToDynamicProxy() {
StaticApplicationContext sac = new StaticApplicationContext();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("proxyFactoryBean", "false");
sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class, pvs);
sac.registerSingleton("noInterfaces", NoInterfaces.class);
sac.registerSingleton("containerCallbackInterfacesOnly", ContainerCallbackInterfacesOnly.class);
sac.registerSingleton("singletonNoInterceptor", CustomProxyFactoryBean.class);
sac.registerSingleton("singletonToBeProxied", CustomProxyFactoryBean.class);
sac.registerPrototype("prototypeToBeProxied", CustomProxyFactoryBean.class);
sac.refresh();
MessageSource messageSource = (MessageSource) sac.getBean("messageSource");
NoInterfaces noInterfaces = (NoInterfaces) sac.getBean("noInterfaces");
ContainerCallbackInterfacesOnly containerCallbackInterfacesOnly =
(ContainerCallbackInterfacesOnly) sac.getBean("containerCallbackInterfacesOnly");
ITestBean singletonNoInterceptor = (ITestBean) sac.getBean("singletonNoInterceptor");
ITestBean singletonToBeProxied = (ITestBean) sac.getBean("singletonToBeProxied");
ITestBean prototypeToBeProxied = (ITestBean) sac.getBean("prototypeToBeProxied");
assertFalse(AopUtils.isCglibProxy(messageSource));
assertTrue(AopUtils.isCglibProxy(noInterfaces));
assertTrue(AopUtils.isCglibProxy(containerCallbackInterfacesOnly));
assertFalse(AopUtils.isCglibProxy(singletonNoInterceptor));
assertFalse(AopUtils.isCglibProxy(singletonToBeProxied));
assertFalse(AopUtils.isCglibProxy(prototypeToBeProxied));
TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
assertEquals(0, tapc.testInterceptor.nrOfInvocations);
singletonNoInterceptor.getName();
assertEquals(0, tapc.testInterceptor.nrOfInvocations);
singletonToBeProxied.getAge();
assertEquals(1, tapc.testInterceptor.nrOfInvocations);
prototypeToBeProxied.getSpouse();
assertEquals(2, tapc.testInterceptor.nrOfInvocations);
}
@Test
public void testAutoProxyCreatorWithFactoryBean() {
StaticApplicationContext sac = new StaticApplicationContext();
@ -404,4 +447,30 @@ public final class AutoProxyCreatorTests { @@ -404,4 +447,30 @@ public final class AutoProxyCreatorTests {
}
}
public static class CustomProxyFactoryBean implements FactoryBean<ITestBean> {
private final TestBean tb = new TestBean();
@Override
public ITestBean getObject() {
return (ITestBean) Proxy.newProxyInstance(CustomProxyFactoryBean.class.getClassLoader(), new Class<?>[]{ITestBean.class}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return ReflectionUtils.invokeMethod(method, tb, args);
}
});
}
@Override
public Class<?> getObjectType() {
return ITestBean.class;
}
@Override
public boolean isSingleton() {
return false;
}
}
}

Loading…
Cancel
Save