Consistent treatment of proxy classes and interfaces for introspection

Issue: SPR-16675
Issue: SPR-16677
This commit is contained in:
Juergen Hoeller
2018-04-03 02:42:41 +02:00
parent 65fdd0efeb
commit 6102715b8d
9 changed files with 138 additions and 48 deletions

View File

@@ -16,6 +16,8 @@
package org.springframework.aop.aspectj.autoproxy;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
import org.apache.commons.logging.Log;
@@ -57,7 +59,6 @@ import org.springframework.tests.sample.beans.NestedTestBean;
import org.springframework.tests.sample.beans.TestBean;
import org.springframework.util.StopWatch;
import static java.lang.String.format;
import static org.junit.Assert.*;
/**
@@ -73,15 +74,11 @@ public class AspectJAutoProxyCreatorTests {
private static final Log factoryLog = LogFactory.getLog(DefaultListableBeanFactory.class);
private static void assertStopWatchTimeLimit(final StopWatch sw, final long maxTimeMillis) {
final long totalTimeMillis = sw.getTotalTimeMillis();
assertTrue("'" + sw.getLastTaskName() + "' took too long: expected less than<" + maxTimeMillis
+ "> ms, actual<" + totalTimeMillis + "> ms.", totalTimeMillis < maxTimeMillis);
}
@Test
public void testAspectsAreApplied() {
ClassPathXmlApplicationContext bf = newContext("aspects.xml");
ITestBean tb = (ITestBean) bf.getBean("adrian");
assertEquals(68, tb.getAge());
MethodInvokingFactoryBean factoryBean = (MethodInvokingFactoryBean) bf.getBean("&factoryBean");
@@ -92,6 +89,7 @@ public class AspectJAutoProxyCreatorTests {
@Test
public void testMultipleAspectsWithParameterApplied() {
ClassPathXmlApplicationContext bf = newContext("aspects.xml");
ITestBean tb = (ITestBean) bf.getBean("adrian");
tb.setAge(10);
assertEquals(20, tb.getAge());
@@ -100,6 +98,7 @@ public class AspectJAutoProxyCreatorTests {
@Test
public void testAspectsAreAppliedInDefinedOrder() {
ClassPathXmlApplicationContext bf = newContext("aspectsWithOrdering.xml");
ITestBean tb = (ITestBean) bf.getBean("adrian");
assertEquals(71, tb.getAge());
}
@@ -107,6 +106,7 @@ public class AspectJAutoProxyCreatorTests {
@Test
public void testAspectsAndAdvisorAreApplied() {
ClassPathXmlApplicationContext ac = newContext("aspectsPlusAdvisor.xml");
ITestBean shouldBeWeaved = (ITestBean) ac.getBean("adrian");
doTestAspectsAndAdvisorAreApplied(ac, shouldBeWeaved);
}
@@ -115,7 +115,9 @@ public class AspectJAutoProxyCreatorTests {
public void testAspectsAndAdvisorAppliedToPrototypeIsFastEnough() {
Assume.group(TestGroup.PERFORMANCE);
Assume.notLogging(factoryLog);
ClassPathXmlApplicationContext ac = newContext("aspectsPlusAdvisor.xml");
StopWatch sw = new StopWatch();
sw.start("Prototype Creation");
for (int i = 0; i < 10000; i++) {
@@ -135,7 +137,9 @@ public class AspectJAutoProxyCreatorTests {
public void testAspectsAndAdvisorNotAppliedToPrototypeIsFastEnough() {
Assume.group(TestGroup.PERFORMANCE);
Assume.notLogging(factoryLog);
ClassPathXmlApplicationContext ac = newContext("aspectsPlusAdvisor.xml");
StopWatch sw = new StopWatch();
sw.start("Prototype Creation");
for (int i = 0; i < 100000; i++) {
@@ -155,7 +159,9 @@ public class AspectJAutoProxyCreatorTests {
public void testAspectsAndAdvisorNotAppliedToManySingletonsIsFastEnough() {
Assume.group(TestGroup.PERFORMANCE);
Assume.notLogging(factoryLog);
GenericApplicationContext ac = new GenericApplicationContext();
new XmlBeanDefinitionReader(ac).loadBeanDefinitions(new ClassPathResource(qName("aspectsPlusAdvisor.xml"),
getClass()));
for (int i = 0; i < 10000; i++) {
@@ -174,6 +180,7 @@ public class AspectJAutoProxyCreatorTests {
@Test
public void testAspectsAndAdvisorAreAppliedEvenIfComingFromParentFactory() {
ClassPathXmlApplicationContext ac = newContext("aspectsPlusAdvisor.xml");
GenericApplicationContext childAc = new GenericApplicationContext(ac);
// Create a child factory with a bean that should be woven
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
@@ -336,8 +343,9 @@ public class AspectJAutoProxyCreatorTests {
}
@Test
public void testRetryAspect() throws Exception {
public void testRetryAspect() {
ClassPathXmlApplicationContext bf = newContext("retryAspect.xml");
UnreliableBean bean = (UnreliableBean) bf.getBean("unreliableBean");
RetryAspect aspect = (RetryAspect) bf.getBean("retryAspect");
int attempts = bean.unreliable();
@@ -347,6 +355,15 @@ public class AspectJAutoProxyCreatorTests {
assertEquals(1, aspect.getCommitCalls());
}
@Test
public void testWithBeanNameAutoProxyCreator() {
ClassPathXmlApplicationContext bf = newContext("withBeanNameAutoProxyCreator.xml");
ITestBean tb = (ITestBean) bf.getBean("adrian");
assertEquals(68, tb.getAge());
}
/**
* Returns a new {@link ClassPathXmlApplicationContext} for the file ending in <var>fileSuffix</var>.
*/
@@ -360,7 +377,13 @@ public class AspectJAutoProxyCreatorTests {
* 'AspectJAutoProxyCreatorTests-foo.xml'
*/
private String qName(String fileSuffix) {
return format("%s-%s", getClass().getSimpleName(), fileSuffix);
return String.format("%s-%s", getClass().getSimpleName(), fileSuffix);
}
private void assertStopWatchTimeLimit(final StopWatch sw, final long maxTimeMillis) {
long totalTimeMillis = sw.getTotalTimeMillis();
assertTrue("'" + sw.getLastTaskName() + "' took too long: expected less than<" + maxTimeMillis +
"> ms, actual<" + totalTimeMillis + "> ms.", totalTimeMillis < maxTimeMillis);
}
}
@@ -409,7 +432,6 @@ class AdviceUsingThisJoinPoint {
public void entryTrace(JoinPoint jp) {
this.lastEntry = jp.toString();
}
}
@Aspect
@@ -419,7 +441,6 @@ class DummyAspect {
public Object test(ProceedingJoinPoint pjp) throws Throwable {
return pjp.proceed();
}
}
@Aspect
@@ -435,7 +456,7 @@ class DummyAspectWithParameter {
class DummyFactoryBean implements FactoryBean<Object> {
@Override
public Object getObject() throws Exception {
public Object getObject() {
throw new UnsupportedOperationException();
}
@@ -460,7 +481,6 @@ class IncreaseReturnValue {
int result = (Integer) pjp.proceed();
return result + 3;
}
}
@Aspect
@@ -484,7 +504,49 @@ class MultiplyReturnValue {
int result = (Integer) pjp.proceed();
return result * this.multiple;
}
}
@Retention(RetentionPolicy.RUNTIME)
@interface Marker {
}
@Aspect
class MultiplyReturnValueForMarker {
private int multiple = 2;
public int invocations;
public void setMultiple(int multiple) {
this.multiple = multiple;
}
public int getMultiple() {
return this.multiple;
}
@Around("@annotation(org.springframework.aop.aspectj.autoproxy.Marker)")
public Object doubleReturnValue(ProceedingJoinPoint pjp) throws Throwable {
++this.invocations;
int result = (Integer) pjp.proceed();
return result * this.multiple;
}
}
interface IMarkerTestBean extends ITestBean {
@Marker
@Override
int getAge();
}
class MarkerTestBean extends TestBean implements IMarkerTestBean {
@Marker
@Override
public int getAge() {
return super.getAge();
}
}
@Aspect
@@ -538,7 +600,6 @@ class RetryAspect {
public int getRollbackCalls() {
return this.rollbackCalls;
}
}
@SuppressWarnings("serial")

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2018 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,8 +16,6 @@
package org.springframework.aop.framework.autoproxy;
import java.io.IOException;
import org.junit.Before;
import org.junit.Test;
import test.mixin.Lockable;
@@ -45,14 +43,15 @@ public class BeanNameAutoProxyCreatorTests {
private BeanFactory beanFactory;
@Before
public void setUp() throws IOException {
public void setup() {
// Note that we need an ApplicationContext, not just a BeanFactory,
// for post-processing and hence auto-proxying to work.
beanFactory =
new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-context.xml", getClass());
beanFactory = new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-context.xml", getClass());
}
@Test
public void testNoProxy() {
TestBean tb = (TestBean) beanFactory.getBean("noproxy");
@@ -171,6 +170,7 @@ public class BeanNameAutoProxyCreatorTests {
assertTrue(((Advised)testBean).isFrozen());
}
private void jdkAssertions(ITestBean tb, int nopInterceptorCount) {
NopInterceptor nop = (NopInterceptor) beanFactory.getBean("nopInterceptor");
assertEquals(0, nop.getCount());