Search in sources :

Example 1 with Advisor

use of org.springframework.aop.Advisor in project spring-framework by spring-projects.

the class PersistenceExceptionTranslationPostProcessorTests method checkWillTranslateExceptions.

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");
}
Also used : Advised(org.springframework.aop.framework.Advised) Advisor(org.springframework.aop.Advisor)

Example 2 with Advisor

use of org.springframework.aop.Advisor in project spring-framework by spring-projects.

the class AdvisedSupport method copyConfigurationFrom.

/**
	 * Copy the AOP configuration from the given AdvisedSupport object,
	 * but allow substitution of a fresh TargetSource and a given interceptor chain.
	 * @param other the AdvisedSupport object to take proxy configuration from
	 * @param targetSource the new TargetSource
	 * @param advisors the Advisors for the chain
	 */
protected void copyConfigurationFrom(AdvisedSupport other, TargetSource targetSource, List<Advisor> advisors) {
    copyFrom(other);
    this.targetSource = targetSource;
    this.advisorChainFactory = other.advisorChainFactory;
    this.interfaces = new ArrayList<>(other.interfaces);
    for (Advisor advisor : advisors) {
        if (advisor instanceof IntroductionAdvisor) {
            validateIntroductionAdvisor((IntroductionAdvisor) advisor);
        }
        Assert.notNull(advisor, "Advisor must not be null");
        this.advisors.add(advisor);
    }
    updateAdvisorArray();
    adviceChanged();
}
Also used : IntroductionAdvisor(org.springframework.aop.IntroductionAdvisor) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) IntroductionAdvisor(org.springframework.aop.IntroductionAdvisor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) Advisor(org.springframework.aop.Advisor)

Example 3 with Advisor

use of org.springframework.aop.Advisor in project spring-framework by spring-projects.

the class ProxyFactoryBean method freshAdvisorChain.

/**
	 * Return an independent advisor chain.
	 * We need to do this every time a new prototype instance is returned,
	 * to return distinct instances of prototype Advisors and Advices.
	 */
private List<Advisor> freshAdvisorChain() {
    Advisor[] advisors = getAdvisors();
    List<Advisor> freshAdvisors = new ArrayList<>(advisors.length);
    for (Advisor advisor : advisors) {
        if (advisor instanceof PrototypePlaceholderAdvisor) {
            PrototypePlaceholderAdvisor pa = (PrototypePlaceholderAdvisor) advisor;
            if (logger.isDebugEnabled()) {
                logger.debug("Refreshing bean named '" + pa.getBeanName() + "'");
            }
            // from a getBean() lookup
            if (this.beanFactory == null) {
                throw new IllegalStateException("No BeanFactory available anymore (probably due to serialization) " + "- cannot resolve prototype advisor '" + pa.getBeanName() + "'");
            }
            Object bean = this.beanFactory.getBean(pa.getBeanName());
            Advisor refreshedAdvisor = namedBeanToAdvisor(bean);
            freshAdvisors.add(refreshedAdvisor);
        } else {
            // Add the shared instance.
            freshAdvisors.add(advisor);
        }
    }
    return freshAdvisors;
}
Also used : ArrayList(java.util.ArrayList) Advisor(org.springframework.aop.Advisor)

Example 4 with Advisor

use of org.springframework.aop.Advisor in project spring-framework by spring-projects.

the class PerThisAspect method testIntroductionWithArgumentBinding.

/* prereq AspectJ 1.6.7
	@Test
	public void testIntroductionBasedOnAnnotationMatch_Spr5307() {
		AnnotatedTarget target = new AnnotatedTargetImpl();

		List<Advisor> advisors = getFixture().getAdvisors(
				new SingletonMetadataAwareAspectInstanceFactory(new MakeAnnotatedTypeModifiable(),"someBean"));
		Object proxy = createProxy(target,
				advisors,
				AnnotatedTarget.class);
		System.out.println(advisors.get(1));
		assertTrue(proxy instanceof Lockable);
		Lockable lockable = (Lockable)proxy;
		lockable.locked();
	}
	*/
// TODO: Why does this test fail? It hasn't been run before, so it maybe never actually passed...
@Test
@Ignore
public void testIntroductionWithArgumentBinding() {
    TestBean target = new TestBean();
    List<Advisor> advisors = getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new MakeITestBeanModifiable(), "someBean"));
    advisors.addAll(getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new MakeLockable(), "someBean")));
    Modifiable modifiable = (Modifiable) createProxy(target, advisors, ITestBean.class);
    assertThat(modifiable, instanceOf(Modifiable.class));
    Lockable lockable = (Lockable) modifiable;
    assertFalse(lockable.locked());
    ITestBean itb = (ITestBean) modifiable;
    assertFalse(modifiable.isModified());
    int oldAge = itb.getAge();
    itb.setAge(oldAge + 1);
    assertTrue(modifiable.isModified());
    modifiable.acceptChanges();
    assertFalse(modifiable.isModified());
    itb.setAge(itb.getAge());
    assertFalse("Setting same value does not modify", modifiable.isModified());
    itb.setName("And now for something completely different");
    assertTrue(modifiable.isModified());
    lockable.lock();
    assertTrue(lockable.locked());
    try {
        itb.setName("Else");
        fail("Should be locked");
    } catch (IllegalStateException ex) {
    // Ok
    }
    lockable.unlock();
    itb.setName("Tony");
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) SyntheticInstantiationAdvisor(org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) Advisor(org.springframework.aop.Advisor) Lockable(test.aop.Lockable) DefaultLockable(test.aop.DefaultLockable) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with Advisor

use of org.springframework.aop.Advisor in project spring-framework by spring-projects.

the class PerThisAspect method testMultiplePerTargetAspectsWithOrderAnnotation.

@Test
public void testMultiplePerTargetAspectsWithOrderAnnotation() throws SecurityException, NoSuchMethodException {
    TestBean target = new TestBean();
    int realAge = 65;
    target.setAge(realAge);
    List<Advisor> advisors = new LinkedList<>();
    PerTargetAspectWithOrderAnnotation10 aspect1 = new PerTargetAspectWithOrderAnnotation10();
    aspect1.count = 100;
    advisors.addAll(getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(aspect1, "someBean1")));
    PerTargetAspectWithOrderAnnotation5 aspect2 = new PerTargetAspectWithOrderAnnotation5();
    advisors.addAll(getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(aspect2, "someBean2")));
    Collections.sort(advisors, new OrderComparator());
    TestBean itb = (TestBean) createProxy(target, advisors, TestBean.class);
    assertEquals("Around advice must NOT apply", realAge, itb.getAge());
    // Hit the method in the per clause to instantiate the aspect
    itb.getSpouse();
    assertEquals("Around advice must apply", 0, itb.getAge());
    assertEquals("Around advice must apply", 1, itb.getAge());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) SyntheticInstantiationAdvisor(org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) Advisor(org.springframework.aop.Advisor) OrderComparator(org.springframework.core.OrderComparator) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

Advisor (org.springframework.aop.Advisor)70 Test (org.junit.jupiter.api.Test)33 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)25 Advised (org.springframework.aop.framework.Advised)21 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)19 Test (org.junit.Test)16 TestBean (org.springframework.beans.testfixture.beans.TestBean)16 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)14 AspectJPointcutAdvisor (org.springframework.aop.aspectj.AspectJPointcutAdvisor)11 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)11 ArrayList (java.util.ArrayList)10 JoinPoint (org.aspectj.lang.JoinPoint)8 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)8 Method (java.lang.reflect.Method)7 SyntheticInstantiationAdvisor (org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor)7 StaticMethodMatcherPointcutAdvisor (org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor)7 SerializableNopInterceptor (org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor)7 LockMixinAdvisor (test.mixin.LockMixinAdvisor)7 Advice (org.aopalliance.aop.Advice)6 CountingBeforeAdvice (org.springframework.aop.testfixture.advice.CountingBeforeAdvice)6