use of org.springframework.aop.framework.Advised in project spring-framework by spring-projects.
the class EnableAsyncTests method customAsyncAnnotationIsPropagated.
@Test
public void customAsyncAnnotationIsPropagated() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(CustomAsyncAnnotationConfig.class, CustomAsyncBean.class);
ctx.refresh();
Object bean = ctx.getBean(CustomAsyncBean.class);
assertThat(AopUtils.isAopProxy(bean)).isTrue();
boolean isAsyncAdvised = false;
for (Advisor advisor : ((Advised) bean).getAdvisors()) {
if (advisor instanceof AsyncAnnotationAdvisor) {
isAsyncAdvised = true;
break;
}
}
assertThat(isAsyncAdvised).as("bean was not async advised as expected").isTrue();
ctx.close();
}
use of org.springframework.aop.framework.Advised in project spring-framework by spring-projects.
the class PerThisAspect method perTypeWithinAspect.
@Test
void perTypeWithinAspect() throws Exception {
TestBean target = new TestBean();
int realAge = 65;
target.setAge(realAge);
PerTypeWithinAspectInstanceFactory aif = new PerTypeWithinAspectInstanceFactory();
TestBean itb = (TestBean) createProxy(target, getFixture().getAdvisors(aif), TestBean.class);
assertThat(aif.getInstantiationCount()).as("No method calls").isEqualTo(0);
assertThat(itb.getAge()).as("Around advice must now apply").isEqualTo(0);
Advised advised = (Advised) itb;
// Will be ExposeInvocationInterceptor, synthetic instantiation advisor, 2 method advisors
assertThat(advised.getAdvisors().length).isEqualTo(4);
ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor sia = (ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) advised.getAdvisors()[1];
assertThat(sia.getPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null)).isTrue();
InstantiationModelAwarePointcutAdvisorImpl imapa = (InstantiationModelAwarePointcutAdvisorImpl) advised.getAdvisors()[2];
LazySingletonAspectInstanceFactoryDecorator maaif = (LazySingletonAspectInstanceFactoryDecorator) imapa.getAspectInstanceFactory();
assertThat(maaif.isMaterialized()).isTrue();
// Check that the perclause pointcut is valid
assertThat(maaif.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null)).isTrue();
assertThat(imapa.getPointcut()).isNotSameAs(imapa.getDeclaredPointcut());
// Hit the method in the per clause to instantiate the aspect
itb.getSpouse();
assertThat(maaif.isMaterialized()).isTrue();
assertThat(imapa.getDeclaredPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getAge"), null)).isTrue();
assertThat(itb.getAge()).as("Around advice must still apply").isEqualTo(1);
assertThat(itb.getAge()).as("Around advice must still apply").isEqualTo(2);
TestBean itb2 = (TestBean) createProxy(target, getFixture().getAdvisors(aif), TestBean.class);
assertThat(aif.getInstantiationCount()).isEqualTo(1);
assertThat(itb2.getAge()).as("Around advice be independent for second instance").isEqualTo(0);
assertThat(aif.getInstantiationCount()).isEqualTo(2);
}
use of org.springframework.aop.framework.Advised in project spring-framework by spring-projects.
the class PerThisAspect method perTargetAspect.
@Test
void perTargetAspect() throws Exception {
TestBean target = new TestBean();
int realAge = 65;
target.setAge(realAge);
TestBean itb = (TestBean) createProxy(target, getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new PerTargetAspect(), "someBean")), TestBean.class);
assertThat(itb.getAge()).as("Around advice must NOT apply").isEqualTo(realAge);
Advised advised = (Advised) itb;
ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor sia = (ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) advised.getAdvisors()[1];
assertThat(sia.getPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null)).isTrue();
InstantiationModelAwarePointcutAdvisorImpl imapa = (InstantiationModelAwarePointcutAdvisorImpl) advised.getAdvisors()[3];
LazySingletonAspectInstanceFactoryDecorator maaif = (LazySingletonAspectInstanceFactoryDecorator) imapa.getAspectInstanceFactory();
assertThat(maaif.isMaterialized()).isFalse();
// Check that the perclause pointcut is valid
assertThat(maaif.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null)).isTrue();
assertThat(imapa.getPointcut()).isNotSameAs(imapa.getDeclaredPointcut());
// Hit the method in the per clause to instantiate the aspect
itb.getSpouse();
assertThat(maaif.isMaterialized()).isTrue();
assertThat(itb.getAge()).as("Around advice must apply").isEqualTo(0);
assertThat(itb.getAge()).as("Around advice must apply").isEqualTo(1);
}
use of org.springframework.aop.framework.Advised in project spring-framework by spring-projects.
the class RegexpMethodPointcutAdvisorIntegrationTests method testSerialization.
@Test
public void testSerialization() throws Throwable {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(CONTEXT);
// This is a CGLIB proxy, so we can proxy it to the target class
Person p = (Person) bf.getBean("serializableSettersAdvised");
// Interceptor behind regexp advisor
NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor");
assertThat(nop.getCount()).isEqualTo(0);
int newAge = 12;
// Not advised
assertThat(p.getAge()).isEqualTo(0);
assertThat(nop.getCount()).isEqualTo(0);
// This is proxied
p.setAge(newAge);
assertThat(nop.getCount()).isEqualTo(1);
p.setAge(newAge);
assertThat(p.getAge()).isEqualTo(newAge);
// Only setter fired
assertThat(nop.getCount()).isEqualTo(2);
// Serialize and continue...
p = SerializationTestUtils.serializeAndDeserialize(p);
assertThat(p.getAge()).isEqualTo(newAge);
// Remembers count, but we need to get a new reference to nop...
nop = (SerializableNopInterceptor) ((Advised) p).getAdvisors()[0].getAdvice();
assertThat(nop.getCount()).isEqualTo(2);
assertThat(p.getName()).isEqualTo("serializableSettersAdvised");
p.setAge(newAge + 1);
assertThat(nop.getCount()).isEqualTo(3);
assertThat(p.getAge()).isEqualTo((newAge + 1));
}
use of org.springframework.aop.framework.Advised in project spring-framework by spring-projects.
the class TestNamespaceHandler method testChainedDecorators.
@Test
public void testChainedDecorators() throws Exception {
ITestBean bean = (ITestBean) this.beanFactory.getBean("chainedTestBean");
assertTestBean(bean);
assertThat(AopUtils.isAopProxy(bean)).isTrue();
Advisor[] advisors = ((Advised) bean).getAdvisors();
assertThat(advisors.length).as("Incorrect number of advisors").isEqualTo(2);
assertThat(advisors[0].getAdvice().getClass()).as("Incorrect advice class").isEqualTo(DebugInterceptor.class);
assertThat(advisors[1].getAdvice().getClass()).as("Incorrect advice class").isEqualTo(NopInterceptor.class);
}
Aggregations