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