use of org.springframework.aop.Advisor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testCloneInvocationToProceedThreeTimes.
/**
* There are times when we want to call proceed() twice.
* We can do this if we clone the invocation.
*/
@Test
public void testCloneInvocationToProceedThreeTimes() throws Throwable {
TestBean tb = new TestBean();
ProxyFactory pc = new ProxyFactory(tb);
pc.addInterface(ITestBean.class);
MethodInterceptor twoBirthdayInterceptor = mi -> {
// Clone the invocation to proceed three times
// "The Moor's Last Sigh": this technology can cause premature aging
MethodInvocation clone1 = ((ReflectiveMethodInvocation) mi).invocableClone();
MethodInvocation clone2 = ((ReflectiveMethodInvocation) mi).invocableClone();
clone1.proceed();
clone2.proceed();
return mi.proceed();
};
@SuppressWarnings("serial") StaticMethodMatcherPointcutAdvisor advisor = new StaticMethodMatcherPointcutAdvisor(twoBirthdayInterceptor) {
@Override
public boolean matches(Method m, @Nullable Class<?> targetClass) {
return "haveBirthday".equals(m.getName());
}
};
pc.addAdvisor(advisor);
ITestBean it = (ITestBean) createProxy(pc);
final int age = 20;
it.setAge(age);
assertThat(it.getAge()).isEqualTo(age);
// Should return the age before the third, AOP-induced birthday
assertThat(it.haveBirthday()).isEqualTo((age + 2));
// Return the final age produced by 3 birthdays
assertThat(it.getAge()).isEqualTo((age + 3));
}
use of org.springframework.aop.Advisor in project spring-framework by spring-projects.
the class SimpleBeforeAdviceInterceptor method getAdviceImpl.
private SimpleBeforeAdviceImpl getAdviceImpl(ITestBean tb) {
Advised advised = (Advised) tb;
Advisor advisor = advised.getAdvisors()[0];
return (SimpleBeforeAdviceImpl) advisor.getAdvice();
}
use of org.springframework.aop.Advisor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testMultiAdvice.
@Test
public void testMultiAdvice() throws Throwable {
CountingMultiAdvice cca = new CountingMultiAdvice();
@SuppressWarnings("serial") Advisor matchesNoArgs = new StaticMethodMatcherPointcutAdvisor(cca) {
@Override
public boolean matches(Method m, @Nullable Class<?> targetClass) {
return m.getParameterCount() == 0 || "exceptional".equals(m.getName());
}
};
TestBean target = new TestBean();
target.setAge(80);
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvice(new NopInterceptor());
pf.addAdvisor(matchesNoArgs);
assertThat(pf.getAdvisors()[1]).as("Advisor was added").isEqualTo(matchesNoArgs);
ITestBean proxied = (ITestBean) createProxy(pf);
assertThat(cca.getCalls()).isEqualTo(0);
assertThat(cca.getCalls("getAge")).isEqualTo(0);
assertThat(proxied.getAge()).isEqualTo(target.getAge());
assertThat(cca.getCalls()).isEqualTo(2);
assertThat(cca.getCalls("getAge")).isEqualTo(2);
assertThat(cca.getCalls("setAge")).isEqualTo(0);
// Won't be advised
proxied.setAge(26);
assertThat(cca.getCalls()).isEqualTo(2);
assertThat(proxied.getAge()).isEqualTo(26);
assertThat(cca.getCalls()).isEqualTo(4);
assertThatExceptionOfType(SpecializedUncheckedException.class).as("Should have thrown CannotGetJdbcConnectionException").isThrownBy(() -> proxied.exceptional(new SpecializedUncheckedException("foo", (SQLException) null)));
assertThat(cca.getCalls()).isEqualTo(6);
}
use of org.springframework.aop.Advisor in project spring-framework by spring-projects.
the class CountingAspectJAdvice method testIsProxy.
@Test
public void testIsProxy() throws Exception {
ITestBean bean = getTestBean();
assertThat(AopUtils.isAopProxy(bean)).as("Bean is not a proxy").isTrue();
// check the advice details
Advised advised = (Advised) bean;
Advisor[] advisors = advised.getAdvisors();
assertThat(advisors.length > 0).as("Advisors should not be empty").isTrue();
}
use of org.springframework.aop.Advisor in project spring-framework by spring-projects.
the class EnableCachingIntegrationTests method assertCacheProxying.
private void assertCacheProxying(AnnotationConfigApplicationContext ctx) {
FooRepository repo = ctx.getBean(FooRepository.class);
boolean isCacheProxy = false;
if (AopUtils.isAopProxy(repo)) {
for (Advisor advisor : ((Advised) repo).getAdvisors()) {
if (advisor instanceof BeanFactoryCacheOperationSourceAdvisor) {
isCacheProxy = true;
break;
}
}
}
assertTrue("FooRepository is not a cache proxy", isCacheProxy);
}
Aggregations