Search in sources :

Example 6 with Advisor

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

the class ReflectiveAspectJAdvisorFactory method getAdvisors.

@Override
public List<Advisor> getAdvisors(MetadataAwareAspectInstanceFactory aspectInstanceFactory) {
    Class<?> aspectClass = aspectInstanceFactory.getAspectMetadata().getAspectClass();
    String aspectName = aspectInstanceFactory.getAspectMetadata().getAspectName();
    validate(aspectClass);
    // We need to wrap the MetadataAwareAspectInstanceFactory with a decorator
    // so that it will only instantiate once.
    MetadataAwareAspectInstanceFactory lazySingletonAspectInstanceFactory = new LazySingletonAspectInstanceFactoryDecorator(aspectInstanceFactory);
    List<Advisor> advisors = new LinkedList<>();
    for (Method method : getAdvisorMethods(aspectClass)) {
        Advisor advisor = getAdvisor(method, lazySingletonAspectInstanceFactory, advisors.size(), aspectName);
        if (advisor != null) {
            advisors.add(advisor);
        }
    }
    // If it's a per target aspect, emit the dummy instantiating aspect.
    if (!advisors.isEmpty() && lazySingletonAspectInstanceFactory.getAspectMetadata().isLazilyInstantiated()) {
        Advisor instantiationAdvisor = new SyntheticInstantiationAdvisor(lazySingletonAspectInstanceFactory);
        advisors.add(0, instantiationAdvisor);
    }
    // Find introduction fields.
    for (Field field : aspectClass.getDeclaredFields()) {
        Advisor advisor = getDeclareParentsAdvisor(field);
        if (advisor != null) {
            advisors.add(advisor);
        }
    }
    return advisors;
}
Also used : Field(java.lang.reflect.Field) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) DeclareParentsAdvisor(org.springframework.aop.aspectj.DeclareParentsAdvisor) Advisor(org.springframework.aop.Advisor) Method(java.lang.reflect.Method) LinkedList(java.util.LinkedList)

Example 7 with Advisor

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

the class EnableTransactionManagementIntegrationTests method assertTxProxying.

private void assertTxProxying(AnnotationConfigApplicationContext ctx) {
    FooRepository repo = ctx.getBean(FooRepository.class);
    boolean isTxProxy = false;
    if (AopUtils.isAopProxy(repo)) {
        for (Advisor advisor : ((Advised) repo).getAdvisors()) {
            if (advisor instanceof BeanFactoryTransactionAttributeSourceAdvisor) {
                isTxProxy = true;
                break;
            }
        }
    }
    assertTrue("FooRepository is not a TX proxy", isTxProxy);
    // trigger a transaction
    repo.findAll();
}
Also used : Advised(org.springframework.aop.framework.Advised) Advisor(org.springframework.aop.Advisor) BeanFactoryTransactionAttributeSourceAdvisor(org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor) BeanFactoryTransactionAttributeSourceAdvisor(org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor)

Example 8 with Advisor

use of org.springframework.aop.Advisor in project openmrs-core by openmrs.

the class ServiceContext method removeAddedAdvisors.

/**
 * Removes all the advisors added by ServiceContext.
 *
 * @param cls the class of the cached service to cleanup
 */
private void removeAddedAdvisors(Class cls) {
    Advised advisedService = (Advised) services.get(cls);
    Set<Advisor> advisorsToRemove = addedAdvisors.get(cls);
    if (advisedService != null && advisorsToRemove != null) {
        for (Advisor advisor : advisorsToRemove.toArray(new Advisor[] {})) {
            removeAdvisor(cls, advisor);
        }
    }
}
Also used : Advised(org.springframework.aop.framework.Advised) Advisor(org.springframework.aop.Advisor)

Example 9 with Advisor

use of org.springframework.aop.Advisor in project openmrs-core by openmrs.

the class ServiceContext method moveAddedAOP.

/**
 * Moves advisors and advice added by ServiceContext from the source service to the target one.
 *
 * @param source the existing service
 * @param target the new service
 */
private void moveAddedAOP(Advised source, Advised target) {
    Class serviceClass = source.getClass();
    Set<Advisor> existingAdvisors = getAddedAdvisors(serviceClass);
    for (Advisor advisor : existingAdvisors) {
        target.addAdvisor(advisor);
        source.removeAdvisor(advisor);
    }
    Set<Advice> existingAdvice = getAddedAdvice(serviceClass);
    for (Advice advice : existingAdvice) {
        target.addAdvice(advice);
        source.removeAdvice(advice);
    }
}
Also used : Advisor(org.springframework.aop.Advisor) Advice(org.aopalliance.aop.Advice)

Example 10 with Advisor

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

the class PollerAdviceTests method testMixedAdvice.

@Test
public void testMixedAdvice() throws Exception {
    SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter();
    final List<String> callOrder = new ArrayList<>();
    final AtomicReference<CountDownLatch> latch = new AtomicReference<>(new CountDownLatch(4));
    MessageSource<Object> source = () -> {
        callOrder.add("c");
        latch.get().countDown();
        return null;
    };
    adapter.setSource(source);
    OnlyOnceTrigger trigger = new OnlyOnceTrigger();
    adapter.setTrigger(trigger);
    configure(adapter);
    List<Advice> adviceChain = new ArrayList<>();
    adviceChain.add((MethodInterceptor) invocation -> {
        callOrder.add("a");
        latch.get().countDown();
        return invocation.proceed();
    });
    final AtomicInteger count = new AtomicInteger();
    class TestSourceAdvice extends AbstractMessageSourceAdvice {

        @Override
        public boolean beforeReceive(MessageSource<?> target) {
            count.incrementAndGet();
            callOrder.add("b");
            latch.get().countDown();
            return true;
        }

        @Override
        public Message<?> afterReceive(Message<?> result, MessageSource<?> target) {
            callOrder.add("d");
            latch.get().countDown();
            return result;
        }
    }
    adviceChain.add(new TestSourceAdvice());
    adapter.setAdviceChain(adviceChain);
    adapter.afterPropertiesSet();
    adapter.start();
    assertTrue(latch.get().await(10, TimeUnit.SECONDS));
    // advice + advice + source + advice
    assertThat(callOrder, contains("a", "b", "c", "d"));
    adapter.stop();
    trigger.reset();
    latch.set(new CountDownLatch(4));
    adapter.start();
    assertTrue(latch.get().await(10, TimeUnit.SECONDS));
    adapter.stop();
    assertEquals(2, count.get());
    // Now test when the source is already a proxy.
    ProxyFactory pf = new ProxyFactory(source);
    pf.addAdvice((MethodInterceptor) Joinpoint::proceed);
    adapter.setSource((MessageSource<?>) pf.getProxy());
    trigger.reset();
    latch.set(new CountDownLatch(4));
    count.set(0);
    callOrder.clear();
    adapter.start();
    assertTrue(latch.get().await(10, TimeUnit.SECONDS));
    // advice + advice + source + advice
    assertThat(callOrder, contains("a", "b", "c", "d"));
    adapter.stop();
    trigger.reset();
    latch.set(new CountDownLatch(4));
    adapter.start();
    assertTrue(latch.get().await(10, TimeUnit.SECONDS));
    adapter.stop();
    assertEquals(2, count.get());
    Advisor[] advisors = ((Advised) adapter.getMessageSource()).getAdvisors();
    // make sure we didn't remove the original one
    assertEquals(2, advisors.length);
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) Date(java.util.Date) SimpleActiveIdleMessageSourceAdvice(org.springframework.integration.aop.SimpleActiveIdleMessageSourceAdvice) Autowired(org.springframework.beans.factory.annotation.Autowired) DynamicPeriodicTrigger(org.springframework.integration.util.DynamicPeriodicTrigger) Assert.assertThat(org.junit.Assert.assertThat) OnlyOnceTrigger(org.springframework.integration.test.util.OnlyOnceTrigger) SpringJUnit4ClassRunner(org.springframework.test.context.junit4.SpringJUnit4ClassRunner) NullChannel(org.springframework.integration.channel.NullChannel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Mockito.atLeast(org.mockito.Mockito.atLeast) TriggerContext(org.springframework.scheduling.TriggerContext) Trigger(org.springframework.scheduling.Trigger) CompoundTriggerAdvice(org.springframework.integration.aop.CompoundTriggerAdvice) EnableIntegration(org.springframework.integration.config.EnableIntegration) MessageChannel(org.springframework.messaging.MessageChannel) SimplePollSkipStrategy(org.springframework.integration.scheduling.SimplePollSkipStrategy) Configuration(org.springframework.context.annotation.Configuration) ServiceActivator(org.springframework.integration.annotation.ServiceActivator) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Log4j2LevelAdjuster(org.springframework.integration.test.rule.Log4j2LevelAdjuster) Matchers.contains(org.hamcrest.Matchers.contains) Assert.assertFalse(org.junit.Assert.assertFalse) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) DirectChannel(org.springframework.integration.channel.DirectChannel) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) AbstractMessageSourceAdvice(org.springframework.integration.aop.AbstractMessageSourceAdvice) Advised(org.springframework.aop.framework.Advised) RunWith(org.junit.runner.RunWith) ExpressionControlBusFactoryBean(org.springframework.integration.config.ExpressionControlBusFactoryBean) Mockito.spy(org.mockito.Mockito.spy) TestUtils(org.springframework.integration.test.util.TestUtils) AtomicReference(java.util.concurrent.atomic.AtomicReference) MessageSource(org.springframework.integration.core.MessageSource) ArrayList(java.util.ArrayList) Joinpoint(org.aopalliance.intercept.Joinpoint) Advice(org.aopalliance.aop.Advice) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Message(org.springframework.messaging.Message) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) LinkedList(java.util.LinkedList) Advisor(org.springframework.aop.Advisor) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) PollSkipAdvice(org.springframework.integration.scheduling.PollSkipAdvice) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Rule(org.junit.Rule) BeanFactory(org.springframework.beans.factory.BeanFactory) ContextConfiguration(org.springframework.test.context.ContextConfiguration) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Bean(org.springframework.context.annotation.Bean) CompoundTrigger(org.springframework.integration.util.CompoundTrigger) GenericMessage(org.springframework.messaging.support.GenericMessage) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) AbstractMessageSourceAdvice(org.springframework.integration.aop.AbstractMessageSourceAdvice) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) ProxyFactory(org.springframework.aop.framework.ProxyFactory) ArrayList(java.util.ArrayList) MessageSource(org.springframework.integration.core.MessageSource) Advisor(org.springframework.aop.Advisor) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) OnlyOnceTrigger(org.springframework.integration.test.util.OnlyOnceTrigger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Advised(org.springframework.aop.framework.Advised) SimpleActiveIdleMessageSourceAdvice(org.springframework.integration.aop.SimpleActiveIdleMessageSourceAdvice) CompoundTriggerAdvice(org.springframework.integration.aop.CompoundTriggerAdvice) AbstractMessageSourceAdvice(org.springframework.integration.aop.AbstractMessageSourceAdvice) Advice(org.aopalliance.aop.Advice) PollSkipAdvice(org.springframework.integration.scheduling.PollSkipAdvice) 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