Add support for Objenesis proxy creation.
Extended DefaultAopProxyFactory to create Objenesis based proxies if the library is on the classpath. This allows classes without a default constructor being CGLib proxied. We're now falling back to original CGLib based behavior in case the proxy creation using Objenesis fails. Objenesis 2.0 is now inlined into spring-core to avoid interfering with other Objenesis versions on the classpath. Issue: SPR-10594
This commit is contained in:
committed by
Phillip Webb
parent
41fffdcf7b
commit
1f9e8f68d4
@@ -16,13 +16,6 @@
|
||||
|
||||
package org.springframework.aop.framework;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.aopalliance.intercept.MethodInterceptor;
|
||||
@@ -33,7 +26,6 @@ import org.springframework.aop.MethodMatcher;
|
||||
import org.springframework.aop.Pointcut;
|
||||
import org.springframework.aop.support.AopUtils;
|
||||
import org.springframework.aop.support.DefaultPointcutAdvisor;
|
||||
import org.springframework.cglib.core.CodeGenerationException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextException;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
@@ -43,6 +35,8 @@ import org.springframework.tests.sample.beans.ITestBean;
|
||||
import org.springframework.tests.sample.beans.TestBean;
|
||||
|
||||
import test.mixin.LockMixinAdvisor;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Additional and overridden tests for the CGLIB proxy.
|
||||
@@ -135,31 +129,6 @@ public final class CglibProxyTests extends AbstractAopProxyTests implements Seri
|
||||
assertEquals(32, tb.getAge());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCglibProxyingGivesMeaningfulExceptionIfAskedToProxyNonvisibleClass() {
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
class YouCantSeeThis {
|
||||
void hidden() {
|
||||
}
|
||||
}
|
||||
|
||||
YouCantSeeThis mine = new YouCantSeeThis();
|
||||
try {
|
||||
ProxyFactory pf = new ProxyFactory(mine);
|
||||
pf.getProxy();
|
||||
fail("Shouldn't be able to proxy non-visible class with CGLIB");
|
||||
}
|
||||
catch (AopConfigException ex) {
|
||||
// Check that stack trace is preserved
|
||||
assertTrue(ex.getCause() instanceof CodeGenerationException ||
|
||||
ex.getCause() instanceof IllegalArgumentException);
|
||||
// Check that error message is helpful
|
||||
assertTrue(ex.getMessage().indexOf("final") != -1);
|
||||
assertTrue(ex.getMessage().indexOf("visible") != -1);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMethodInvocationDuringConstructor() {
|
||||
CglibTestBean bean = new CglibTestBean();
|
||||
@@ -348,6 +317,7 @@ public final class CglibProxyTests extends AbstractAopProxyTests implements Seri
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("resource")
|
||||
public void testWithDependencyChecking() {
|
||||
ApplicationContext ctx =
|
||||
new ClassPathXmlApplicationContext(DEPENDENCY_CHECK_CONTEXT, getClass());
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.aop.framework;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@Component
|
||||
public class ClassWithComplexConstructor {
|
||||
|
||||
private final Dependency dependency;
|
||||
|
||||
@Autowired
|
||||
public ClassWithComplexConstructor(Dependency dependency) {
|
||||
Assert.notNull(dependency);
|
||||
this.dependency = dependency;
|
||||
}
|
||||
|
||||
public Dependency getDependency() {
|
||||
return dependency;
|
||||
}
|
||||
|
||||
public void method() {
|
||||
this.dependency.method();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.aop.framework;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component class Dependency {
|
||||
|
||||
private int value = 0;
|
||||
|
||||
public void method() {
|
||||
value++;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
|
||||
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
|
||||
|
||||
<context:component-scan base-package="org.springframework.aop.framework" />
|
||||
|
||||
<aop:config>
|
||||
<aop:advisor advice-ref="debugInterceptor" pointcut="bean(classWithComplexConstructor)" />
|
||||
</aop:config>
|
||||
|
||||
|
||||
<bean id="debugInterceptor"
|
||||
class="org.springframework.aop.interceptor.DebugInterceptor" />
|
||||
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.aop.framework;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.aop.interceptor.DebugInterceptor;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Integration test for Objenesis proxy creation.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class ObjenesisProxyTests {
|
||||
|
||||
@Test
|
||||
public void appliesAspectToClassWithComplexConstructor() {
|
||||
|
||||
@SuppressWarnings("resource")
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("ObjenesisProxyTests-context.xml", getClass());
|
||||
|
||||
ClassWithComplexConstructor bean = context.getBean(ClassWithComplexConstructor.class);
|
||||
bean.method();
|
||||
|
||||
DebugInterceptor interceptor = context.getBean(DebugInterceptor.class);
|
||||
assertThat(interceptor.getCount(), is(1L));
|
||||
assertThat(bean.getDependency().getValue(), is(1));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user