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;
}
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();
}
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);
}
}
}
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);
}
}
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);
}
Aggregations