Skip to content

Commit bc87910

Browse files
committed
Test against CGLIB memory leak for method injection
This commit introduces a test in XmlBeanFactoryTests that verifies that CGLIB generated subclasses for method injected beans are reused across bean factories for identical bean definitions. In other words, by verifying that the same CGLIB generated class is reused for identical bean definitions, we can be certain that Spring is no longer generating identical, duplicate classes that consume memory in the VM. Issue: SPR-10785, SPR-11420 (cherry picked from commit f2a4537)
1 parent 9f77ef4 commit bc87910

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

spring-context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests.java

Lines changed: 33 additions & 3 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-2014 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.
@@ -27,8 +27,8 @@
2727
import java.util.Map;
2828

2929
import org.apache.commons.logging.LogFactory;
30+
3031
import org.junit.Test;
31-
import org.xml.sax.InputSource;
3232

3333
import org.springframework.aop.framework.ProxyFactory;
3434
import org.springframework.aop.support.AopUtils;
@@ -61,20 +61,24 @@
6161
import org.springframework.tests.sample.beans.ResourceTestBean;
6262
import org.springframework.tests.sample.beans.TestBean;
6363
import org.springframework.tests.sample.beans.factory.DummyFactory;
64+
import org.springframework.util.ClassUtils;
6465
import org.springframework.util.FileCopyUtils;
6566
import org.springframework.util.SerializationTestUtils;
6667
import org.springframework.util.StopWatch;
6768

69+
import org.xml.sax.InputSource;
70+
6871
import static org.hamcrest.CoreMatchers.*;
6972
import static org.junit.Assert.*;
7073

7174
/**
7275
* Miscellaneous tests for XML bean definitions.
73-
*
76+
*
7477
* @author Juergen Hoeller
7578
* @author Rod Johnson
7679
* @author Rick Evans
7780
* @author Chris Beams
81+
* @author Sam Brannen
7882
*/
7983
public final class XmlBeanFactoryTests {
8084

@@ -1283,6 +1287,31 @@ public void testRecursiveImport() {
12831287
}
12841288
}
12851289

1290+
/**
1291+
* @since 3.2.8 and 4.0.2
1292+
* @see <a href="https://jira.springsource.org/browse/SPR-10785">SPR-10785</a> and <a
1293+
* href="https://jira.springsource.org/browse/SPR-11420">SPR-11420</a>
1294+
*/
1295+
@Test
1296+
public void methodInjectedBeanMustBeOfSameEnhancedCglibSubclassTypeAcrossBeanFactories() {
1297+
Class<?> firstClass = null;
1298+
1299+
for (int i = 1; i <= 10; i++) {
1300+
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
1301+
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(OVERRIDES_CONTEXT);
1302+
1303+
final Class<?> currentClass = bf.getBean("overrideOneMethod").getClass();
1304+
assertTrue("Method injected bean class [" + currentClass + "] must be a CGLIB enhanced subclass.",
1305+
ClassUtils.isCglibProxyClass(currentClass));
1306+
1307+
if (firstClass == null) {
1308+
firstClass = currentClass;
1309+
}
1310+
else {
1311+
assertEquals(firstClass, currentClass);
1312+
}
1313+
}
1314+
}
12861315

12871316
@Test
12881317
public void testLookupOverrideMethodsWithSetterInjection() {
@@ -1915,6 +1944,7 @@ public LenientDependencyTestBean(DerivedTestBean tb) {
19151944
this.tb = tb;
19161945
}
19171946

1947+
@SuppressWarnings("rawtypes")
19181948
public LenientDependencyTestBean(Map[] m) {
19191949
throw new IllegalStateException("Don't pick this constructor");
19201950
}

0 commit comments

Comments
 (0)