Java Code Examples for org.springframework.aop.framework.Advised#getAdvisors()

The following examples show how to use org.springframework.aop.framework.Advised#getAdvisors() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AmazonS3ProxyFactoryTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
void verifyAddingRedirectAdviceToExistingProxy() {

	AmazonS3 amazonS3 = mock(AmazonS3.class);

	ProxyFactory factory = new ProxyFactory(amazonS3);
	factory.addAdvice(new TestAdvice());
	AmazonS3 proxy1 = (AmazonS3) factory.getProxy();

	assertThat(((Advised) proxy1).getAdvisors().length).isEqualTo(1);

	AmazonS3 proxy2 = AmazonS3ProxyFactory.createProxy(proxy1);
	Advised advised = (Advised) proxy2;

	assertThat(advised.getAdvisors().length).isEqualTo(2);

	List<Class<? extends MethodInterceptor>> advisorClasses = new ArrayList<>();
	for (Advisor advisor : advised.getAdvisors()) {
		advisorClasses.add(((MethodInterceptor) advisor.getAdvice()).getClass());
	}
	assertThat(advisorClasses).contains(TestAdvice.class,
			AmazonS3ProxyFactory.SimpleStorageRedirectInterceptor.class);

}
 
Example 2
Source File: AbstractAspectJAdvisorFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testPerThisAspect() throws SecurityException, NoSuchMethodException {
	TestBean target = new TestBean();
	int realAge = 65;
	target.setAge(realAge);
	TestBean itb = (TestBean) createProxy(target,
			getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new PerThisAspect(), "someBean")),
			TestBean.class);
	assertEquals("Around advice must NOT apply", realAge, itb.getAge());

	Advised advised = (Advised) itb;
	// Will be ExposeInvocationInterceptor, synthetic instantiation advisor, 2 method advisors
	assertEquals(4, advised.getAdvisors().length);
	ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor sia =
			(ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) advised.getAdvisors()[1];
	assertTrue(sia.getPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
	InstantiationModelAwarePointcutAdvisorImpl imapa = (InstantiationModelAwarePointcutAdvisorImpl) advised.getAdvisors()[2];
	LazySingletonAspectInstanceFactoryDecorator maaif =
			(LazySingletonAspectInstanceFactoryDecorator) imapa.getAspectInstanceFactory();
	assertFalse(maaif.isMaterialized());

	// Check that the perclause pointcut is valid
	assertTrue(maaif.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
	assertNotSame(imapa.getDeclaredPointcut(), imapa.getPointcut());

	// Hit the method in the per clause to instantiate the aspect
	itb.getSpouse();

	assertTrue(maaif.isMaterialized());

	assertTrue(imapa.getDeclaredPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getAge"), null));

	assertEquals("Around advice must apply", 0, itb.getAge());
	assertEquals("Around advice must apply", 1, itb.getAge());
}
 
Example 3
Source File: AmazonS3ProxyFactory.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
/**
 * Factory-method to create a proxy using the {@link SimpleStorageRedirectInterceptor}
 * that supports redirects for buckets which are in a different region. This proxy
 * uses the amazonS3 parameter as a "prototype" and re-uses the credentials from the
 * passed in {@link AmazonS3} instance. Proxy implementations uses the
 * {@link AmazonS3ClientFactory} to create region specific clients, which are cached
 * by the implementation on a region basis to avoid unnecessary object creation.
 * @param amazonS3 Fully configured AmazonS3 client, the client can be an immutable
 * instance (created by the {@link com.amazonaws.services.s3.AmazonS3ClientBuilder})
 * as this proxy will not change the underlying implementation.
 * @return AOP-Proxy that intercepts all method calls using the
 * {@link SimpleStorageRedirectInterceptor}
 */
public static AmazonS3 createProxy(AmazonS3 amazonS3) {
	Assert.notNull(amazonS3, "AmazonS3 client must not be null");

	if (AopUtils.isAopProxy(amazonS3)) {

		Advised advised = (Advised) amazonS3;
		for (Advisor advisor : advised.getAdvisors()) {
			if (ClassUtils.isAssignableValue(SimpleStorageRedirectInterceptor.class,
					advisor.getAdvice())) {
				return amazonS3;
			}
		}

		try {
			advised.addAdvice(new SimpleStorageRedirectInterceptor(
					(AmazonS3) advised.getTargetSource().getTarget()));
		}
		catch (Exception e) {
			throw new RuntimeException(
					"Error adding advice for class amazonS3 instance", e);
		}

		return amazonS3;
	}

	ProxyFactory factory = new ProxyFactory(amazonS3);
	factory.setInterfaces(AmazonS3.class);
	factory.addAdvice(new SimpleStorageRedirectInterceptor(amazonS3));

	return (AmazonS3) factory.getProxy();
}
 
Example 4
Source File: ZebraRoutingDataSource.java    From Zebra with Apache License 2.0 5 votes vote down vote up
@Override
protected Object[] getAdvicesAndAdvisorsForBean(Class<?> beanClass, String beanName, TargetSource customTargetSource)
      throws BeansException {
	Method[] declaredMethods = beanClass.getDeclaredMethods();
	for (Method declaredMethod : declaredMethods) {
		if (pointcut.match(declaredMethod, beanClass)) {
			Advised advised = this.advisedMap.get(beanName);
			if (advised != null) {
				Advisor[] advisors = advised.getAdvisors();
				boolean added = false;
				for (int i = 0; i < advisors.length; i++) {
					if (advisors[i].getAdvice() instanceof TransactionInterceptor) {
						advised.addAdvisor(i, this);
						added = true;
						break;
					}
				}
				if (!added) {
					advised.addAdvisor(this);
				}
				return DO_NOT_PROXY;
			}
			return new Object[] { this };
		}
	}
	return DO_NOT_PROXY;
}
 
Example 5
Source File: PersistenceExceptionTranslationPostProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
protected void checkWillTranslateExceptions(Object o) {
	assertTrue(o instanceof Advised);
	Advised a = (Advised) o;
	for (Advisor advisor : a.getAdvisors()) {
		if (advisor instanceof PersistenceExceptionTranslationAdvisor) {
			return;
		}
	}
	fail("No translation");
}
 
Example 6
Source File: AopNamespaceHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testIsProxy() throws Exception {
	ITestBean bean = getTestBean();

	assertTrue("Bean is not a proxy", AopUtils.isAopProxy(bean));

	// check the advice details
	Advised advised = (Advised) bean;
	Advisor[] advisors = advised.getAdvisors();

	assertTrue("Advisors should not be empty", advisors.length > 0);
}
 
Example 7
Source File: AbstractAspectJAdvisorFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testPerThisAspect() throws SecurityException, NoSuchMethodException {
	TestBean target = new TestBean();
	int realAge = 65;
	target.setAge(realAge);
	TestBean itb = (TestBean) createProxy(target,
			getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new PerThisAspect(), "someBean")),
			TestBean.class);
	assertEquals("Around advice must NOT apply", realAge, itb.getAge());

	Advised advised = (Advised) itb;
	// Will be ExposeInvocationInterceptor, synthetic instantiation advisor, 2 method advisors
	assertEquals(4, advised.getAdvisors().length);
	SyntheticInstantiationAdvisor sia = (SyntheticInstantiationAdvisor) advised.getAdvisors()[1];
	assertTrue(sia.getPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
	InstantiationModelAwarePointcutAdvisorImpl imapa = (InstantiationModelAwarePointcutAdvisorImpl) advised.getAdvisors()[2];
	LazySingletonAspectInstanceFactoryDecorator maaif =
			(LazySingletonAspectInstanceFactoryDecorator) imapa.getAspectInstanceFactory();
	assertFalse(maaif.isMaterialized());

	// Check that the perclause pointcut is valid
	assertTrue(maaif.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
	assertNotSame(imapa.getDeclaredPointcut(), imapa.getPointcut());

	// Hit the method in the per clause to instantiate the aspect
	itb.getSpouse();

	assertTrue(maaif.isMaterialized());

	assertTrue(imapa.getDeclaredPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getAge"), null));

	assertEquals("Around advice must apply", 0, itb.getAge());
	assertEquals("Around advice must apply", 1, itb.getAge());
}
 
Example 8
Source File: AbstractAspectJAdvisorFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testPerTargetAspect() throws SecurityException, NoSuchMethodException {
	TestBean target = new TestBean();
	int realAge = 65;
	target.setAge(realAge);
	TestBean itb = (TestBean) createProxy(target,
			getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new PerTargetAspect(), "someBean")),
			TestBean.class);
	assertEquals("Around advice must NOT apply", realAge, itb.getAge());

	Advised advised = (Advised) itb;
	SyntheticInstantiationAdvisor sia = (SyntheticInstantiationAdvisor) advised.getAdvisors()[1];
	assertTrue(sia.getPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
	InstantiationModelAwarePointcutAdvisorImpl imapa = (InstantiationModelAwarePointcutAdvisorImpl) advised.getAdvisors()[3];
	LazySingletonAspectInstanceFactoryDecorator maaif =
			(LazySingletonAspectInstanceFactoryDecorator) imapa.getAspectInstanceFactory();
	assertFalse(maaif.isMaterialized());

	// Check that the perclause pointcut is valid
	assertTrue(maaif.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
	assertNotSame(imapa.getDeclaredPointcut(), imapa.getPointcut());

	// Hit the method in the per clause to instantiate the aspect
	itb.getSpouse();

	assertTrue(maaif.isMaterialized());

	assertEquals("Around advice must apply", 0, itb.getAge());
	assertEquals("Around advice must apply", 1, itb.getAge());
}
 
Example 9
Source File: PersistenceExceptionTranslationPostProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
protected void checkWillTranslateExceptions(Object o) {
	assertTrue(o instanceof Advised);
	Advised a = (Advised) o;
	for (Advisor advisor : a.getAdvisors()) {
		if (advisor instanceof PersistenceExceptionTranslationAdvisor) {
			return;
		}
	}
	fail("No translation");
}
 
Example 10
Source File: AopNamespaceHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testIsProxy() throws Exception {
	ITestBean bean = getTestBean();

	assertTrue("Bean is not a proxy", AopUtils.isAopProxy(bean));

	// check the advice details
	Advised advised = (Advised) bean;
	Advisor[] advisors = advised.getAdvisors();

	assertTrue("Advisors should not be empty", advisors.length > 0);
}
 
Example 11
Source File: AbstractAspectJAdvisorFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testPerTypeWithinAspect() throws SecurityException, NoSuchMethodException {
	TestBean target = new TestBean();
	int realAge = 65;
	target.setAge(realAge);
	PerTypeWithinAspectInstanceFactory aif = new PerTypeWithinAspectInstanceFactory();
	TestBean itb = (TestBean) createProxy(target, getFixture().getAdvisors(aif), TestBean.class);
	assertEquals("No method calls", 0, aif.getInstantiationCount());
	assertEquals("Around advice must now apply", 0, itb.getAge());

	Advised advised = (Advised) itb;
	// Will be ExposeInvocationInterceptor, synthetic instantiation advisor, 2 method advisors
	assertEquals(4, advised.getAdvisors().length);
	ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor sia =
			(ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) advised.getAdvisors()[1];
	assertTrue(sia.getPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
	InstantiationModelAwarePointcutAdvisorImpl imapa = (InstantiationModelAwarePointcutAdvisorImpl) advised.getAdvisors()[2];
	LazySingletonAspectInstanceFactoryDecorator maaif =
			(LazySingletonAspectInstanceFactoryDecorator) imapa.getAspectInstanceFactory();
	assertTrue(maaif.isMaterialized());

	// Check that the perclause pointcut is valid
	assertTrue(maaif.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
	assertNotSame(imapa.getDeclaredPointcut(), imapa.getPointcut());

	// Hit the method in the per clause to instantiate the aspect
	itb.getSpouse();

	assertTrue(maaif.isMaterialized());

	assertTrue(imapa.getDeclaredPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getAge"), null));

	assertEquals("Around advice must still apply", 1, itb.getAge());
	assertEquals("Around advice must still apply", 2, itb.getAge());

	TestBean itb2 = (TestBean) createProxy(target, getFixture().getAdvisors(aif), TestBean.class);
	assertEquals(1, aif.getInstantiationCount());
	assertEquals("Around advice be independent for second instance", 0, itb2.getAge());
	assertEquals(2, aif.getInstantiationCount());
}
 
Example 12
Source File: AbstractAspectJAdvisorFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testPerThisAspect() throws SecurityException, NoSuchMethodException {
	TestBean target = new TestBean();
	int realAge = 65;
	target.setAge(realAge);
	TestBean itb = (TestBean) createProxy(target,
			getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new PerThisAspect(), "someBean")),
			TestBean.class);
	assertEquals("Around advice must NOT apply", realAge, itb.getAge());

	Advised advised = (Advised) itb;
	// Will be ExposeInvocationInterceptor, synthetic instantiation advisor, 2 method advisors
	assertEquals(4, advised.getAdvisors().length);
	ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor sia =
			(ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) advised.getAdvisors()[1];
	assertTrue(sia.getPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
	InstantiationModelAwarePointcutAdvisorImpl imapa = (InstantiationModelAwarePointcutAdvisorImpl) advised.getAdvisors()[2];
	LazySingletonAspectInstanceFactoryDecorator maaif =
			(LazySingletonAspectInstanceFactoryDecorator) imapa.getAspectInstanceFactory();
	assertFalse(maaif.isMaterialized());

	// Check that the perclause pointcut is valid
	assertTrue(maaif.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
	assertNotSame(imapa.getDeclaredPointcut(), imapa.getPointcut());

	// Hit the method in the per clause to instantiate the aspect
	itb.getSpouse();

	assertTrue(maaif.isMaterialized());

	assertTrue(imapa.getDeclaredPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getAge"), null));

	assertEquals("Around advice must apply", 0, itb.getAge());
	assertEquals("Around advice must apply", 1, itb.getAge());
}
 
Example 13
Source File: AbstractAspectJAdvisorFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testPerTargetAspect() throws SecurityException, NoSuchMethodException {
	TestBean target = new TestBean();
	int realAge = 65;
	target.setAge(realAge);
	TestBean itb = (TestBean) createProxy(target,
			getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new PerTargetAspect(), "someBean")),
			TestBean.class);
	assertEquals("Around advice must NOT apply", realAge, itb.getAge());

	Advised advised = (Advised) itb;
	ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor sia =
			(ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) advised.getAdvisors()[1];
	assertTrue(sia.getPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
	InstantiationModelAwarePointcutAdvisorImpl imapa = (InstantiationModelAwarePointcutAdvisorImpl) advised.getAdvisors()[3];
	LazySingletonAspectInstanceFactoryDecorator maaif =
			(LazySingletonAspectInstanceFactoryDecorator) imapa.getAspectInstanceFactory();
	assertFalse(maaif.isMaterialized());

	// Check that the perclause pointcut is valid
	assertTrue(maaif.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
	assertNotSame(imapa.getDeclaredPointcut(), imapa.getPointcut());

	// Hit the method in the per clause to instantiate the aspect
	itb.getSpouse();

	assertTrue(maaif.isMaterialized());

	assertEquals("Around advice must apply", 0, itb.getAge());
	assertEquals("Around advice must apply", 1, itb.getAge());
}
 
Example 14
Source File: AopNamespaceHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testIsProxy() throws Exception {
	ITestBean bean = getTestBean();

	assertTrue("Bean is not a proxy", AopUtils.isAopProxy(bean));

	// check the advice details
	Advised advised = (Advised) bean;
	Advisor[] advisors = advised.getAdvisors();

	assertTrue("Advisors should not be empty", advisors.length > 0);
}
 
Example 15
Source File: AbstractAspectJAdvisorFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testPerTypeWithinAspect() throws SecurityException, NoSuchMethodException {
	TestBean target = new TestBean();
	int realAge = 65;
	target.setAge(realAge);
	PerTypeWithinAspectInstanceFactory aif = new PerTypeWithinAspectInstanceFactory();
	TestBean itb = (TestBean) createProxy(target, getFixture().getAdvisors(aif), TestBean.class);
	assertEquals("No method calls", 0, aif.getInstantiationCount());
	assertEquals("Around advice must now apply", 0, itb.getAge());

	Advised advised = (Advised) itb;
	// Will be ExposeInvocationInterceptor, synthetic instantiation advisor, 2 method advisors
	assertEquals(4, advised.getAdvisors().length);
	ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor sia =
			(ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) advised.getAdvisors()[1];
	assertTrue(sia.getPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
	InstantiationModelAwarePointcutAdvisorImpl imapa = (InstantiationModelAwarePointcutAdvisorImpl) advised.getAdvisors()[2];
	LazySingletonAspectInstanceFactoryDecorator maaif =
			(LazySingletonAspectInstanceFactoryDecorator) imapa.getAspectInstanceFactory();
	assertTrue(maaif.isMaterialized());

	// Check that the perclause pointcut is valid
	assertTrue(maaif.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
	assertNotSame(imapa.getDeclaredPointcut(), imapa.getPointcut());

	// Hit the method in the per clause to instantiate the aspect
	itb.getSpouse();

	assertTrue(maaif.isMaterialized());

	assertTrue(imapa.getDeclaredPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getAge"), null));

	assertEquals("Around advice must still apply", 1, itb.getAge());
	assertEquals("Around advice must still apply", 2, itb.getAge());

	TestBean itb2 = (TestBean) createProxy(target, getFixture().getAdvisors(aif), TestBean.class);
	assertEquals(1, aif.getInstantiationCount());
	assertEquals("Around advice be independent for second instance", 0, itb2.getAge());
	assertEquals(2, aif.getInstantiationCount());
}
 
Example 16
Source File: AbstractAspectJAdvisorFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testPerTargetAspect() throws SecurityException, NoSuchMethodException {
	TestBean target = new TestBean();
	int realAge = 65;
	target.setAge(realAge);
	TestBean itb = (TestBean) createProxy(target,
			getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new PerTargetAspect(), "someBean")),
			TestBean.class);
	assertEquals("Around advice must NOT apply", realAge, itb.getAge());

	Advised advised = (Advised) itb;
	ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor sia =
			(ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) advised.getAdvisors()[1];
	assertTrue(sia.getPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
	InstantiationModelAwarePointcutAdvisorImpl imapa = (InstantiationModelAwarePointcutAdvisorImpl) advised.getAdvisors()[3];
	LazySingletonAspectInstanceFactoryDecorator maaif =
			(LazySingletonAspectInstanceFactoryDecorator) imapa.getAspectInstanceFactory();
	assertFalse(maaif.isMaterialized());

	// Check that the perclause pointcut is valid
	assertTrue(maaif.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
	assertNotSame(imapa.getDeclaredPointcut(), imapa.getPointcut());

	// Hit the method in the per clause to instantiate the aspect
	itb.getSpouse();

	assertTrue(maaif.isMaterialized());

	assertEquals("Around advice must apply", 0, itb.getAge());
	assertEquals("Around advice must apply", 1, itb.getAge());
}
 
Example 17
Source File: AdvisorAdapterRegistrationTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private SimpleBeforeAdviceImpl getAdviceImpl(ITestBean tb) {
	Advised advised = (Advised) tb;
	Advisor advisor = advised.getAdvisors()[0];
	return (SimpleBeforeAdviceImpl) advisor.getAdvice();
}
 
Example 18
Source File: AbstractAspectJAdvisorFactoryTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void testPerTypeWithinAspect() throws SecurityException, NoSuchMethodException {
	TestBean target = new TestBean();
	int realAge = 65;
	target.setAge(realAge);
	PerTypeWithinAspectInstanceFactory aif = new PerTypeWithinAspectInstanceFactory();
	TestBean itb = (TestBean) createProxy(target,
			getFixture().getAdvisors(aif),
			TestBean.class);
	assertEquals("No method calls", 0, aif.getInstantiationCount());
	assertEquals("Around advice must now apply", 0, itb.getAge());

	Advised advised = (Advised) itb;
	// Will be ExposeInvocationInterceptor, synthetic instantiation advisor, 2 method advisors
	assertEquals(4, advised.getAdvisors().length);
	SyntheticInstantiationAdvisor sia = (SyntheticInstantiationAdvisor) advised.getAdvisors()[1];
	assertTrue(sia.getPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
	InstantiationModelAwarePointcutAdvisorImpl imapa = (InstantiationModelAwarePointcutAdvisorImpl) advised.getAdvisors()[2];
	LazySingletonAspectInstanceFactoryDecorator maaif =
			(LazySingletonAspectInstanceFactoryDecorator) imapa.getAspectInstanceFactory();
	assertTrue(maaif.isMaterialized());

	// Check that the perclause pointcut is valid
	assertTrue(maaif.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
	assertNotSame(imapa.getDeclaredPointcut(), imapa.getPointcut());

	// Hit the method in the per clause to instantiate the aspect
	itb.getSpouse();

	assertTrue(maaif.isMaterialized());

	assertTrue(imapa.getDeclaredPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getAge"), null));

	assertEquals("Around advice must still apply", 1, itb.getAge());
	assertEquals("Around advice must still apply", 2, itb.getAge());

	TestBean itb2 = (TestBean) createProxy(target,
			getFixture().getAdvisors(aif),
			TestBean.class);
	assertEquals(1, aif.getInstantiationCount());
	assertEquals("Around advice be independent for second instance", 0, itb2.getAge());
	assertEquals(2, aif.getInstantiationCount());
}
 
Example 19
Source File: AdvisorAdapterRegistrationTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private SimpleBeforeAdviceImpl getAdviceImpl(ITestBean tb) {
	Advised advised = (Advised) tb;
	Advisor advisor = advised.getAdvisors()[0];
	return (SimpleBeforeAdviceImpl) advisor.getAdvice();
}
 
Example 20
Source File: AdvisorAdapterRegistrationTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private SimpleBeforeAdviceImpl getAdviceImpl(ITestBean tb) {
	Advised advised = (Advised) tb;
	Advisor advisor = advised.getAdvisors()[0];
	return (SimpleBeforeAdviceImpl) advisor.getAdvice();
}