advised.addAdvisor(0, this.advisor); advised.addAdvisor(this.advisor);
/** * @param cls * @param advisor */ public void addAdvisor(Class cls, Advisor advisor) { Advised advisedService = (Advised) services.get(cls); if (advisedService.indexOf(advisor) < 0) { advisedService.addAdvisor(advisor); } addedAdvisors.computeIfAbsent(cls, k -> new HashSet<>()); getAddedAdvisors(cls).add(advisor); }
/** * Check that casting to Advised can't get around advice freeze. */ @Test public void testCannotAddAdvisorWhenFrozenUsingCast() throws Throwable { TestBean target = new TestBean(); target.setAge(21); ProxyFactory pc = new ProxyFactory(target); assertFalse(pc.isFrozen()); pc.addAdvice(new NopInterceptor()); ITestBean proxied = (ITestBean) createProxy(pc); pc.setFrozen(true); Advised advised = (Advised) proxied; assertTrue(pc.isFrozen()); try { advised.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor())); fail("Shouldn't be able to add Advisor when frozen"); } catch (AopConfigException ex) { assertTrue(ex.getMessage().contains("frozen")); } // Check it still works: proxy factory state shouldn't have been corrupted assertEquals(target.getAge(), proxied.getAge()); assertEquals(1, advised.getAdvisors().length); }
@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; }
@Override protected void applyReceiveOnlyAdviceChain(Collection<Advice> chain) { if (!CollectionUtils.isEmpty(chain)) { if (AopUtils.isAopProxy(this.source)) { Advised advised = (Advised) this.source; this.appliedAdvices.forEach(advised::removeAdvice); chain.stream().forEach(advice -> advised.addAdvisor(adviceToReceiveAdvisor(advice))); } else { ProxyFactory proxyFactory = new ProxyFactory(this.source); chain.stream().forEach(advice -> proxyFactory.addAdvisor(adviceToReceiveAdvisor(advice))); this.source = (MessageSource<?>) proxyFactory.getProxy(getBeanClassLoader()); } this.appliedAdvices.clear(); this.appliedAdvices.addAll(chain); if (!(isSyncExecutor()) && logger.isWarnEnabled()) { logger.warn(getComponentName() + ": A task executor is supplied and " + chain.size() + "MessageSourceMutator(s) is/are provided. If an advice mutates the source, such " + "mutations are not thread safe and could cause unexpected results, especially with " + "high frequency pollers. Consider using a downstream ExecutorChannel instead of " + "adding an executor to the poller"); } } }
/** * 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); } }
@Override public Object postProcessAfterInitialization(Object bean, String beanName) { if (bean instanceof AopInfrastructureBean) { return bean; } final Class<?> targetClass = AopUtils.getTargetClass(bean); if (canApply(targetClass)) { final Advice advice = adviceFactory.getAdvice(bean, targetClass); final Advisor advisor = new DefaultPointcutAdvisor(pointcut, advice); if (bean instanceof Advised) { LOG.debug("Bean {} is already proxied, adding Advisor to existing proxy", beanName); ((Advised) bean).addAdvisor(0, advisor); return bean; } else { LOG.debug("Proxying bean {} of type {}", beanName, targetClass.getCanonicalName()); final ProxyFactory proxyFactory = new ProxyFactory(bean); if (proxyConfig != null) { proxyFactory.copyFrom(proxyConfig); } proxyFactory.addAdvisor(advisor); final Object proxy = proxyFactory.getProxy(this.beanClassLoader); return proxy; } } return bean; }
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof AopInfrastructureBean) { // Ignore AOP infrastructure such as scoped proxies. return bean; } Class<?> targetClass = AopUtils.getTargetClass(bean); if (AopUtils.canApply(this.advisor, targetClass)) { if (bean instanceof Advised) { ((Advised) bean).addAdvisor(0, this.advisor); return bean; } else { ProxyFactory proxyFactory = new ProxyFactory(bean); // Copy our properties (proxyTargetClass etc) inherited from ProxyConfig. proxyFactory.copyFrom(this); proxyFactory.addAdvisor(this.advisor); return proxyFactory.getProxy(this.beanClassLoader); } } else { // No async proxy needed. return bean; } }
handlerAdvice.addMethodName("handleMessage"); if (this.handler instanceof Advised) { ((Advised) this.handler).addAdvisor(handlerAdvice);
((Advised) handler).addAdvisor(advisor); handlerAdvice.addMethodName("handleMessage"); if (handler instanceof Advised) { ((Advised) handler).addAdvisor(handlerAdvice);
@Override protected void applyReceiveOnlyAdviceChain(Collection<Advice> chain) { if (!CollectionUtils.isEmpty(chain)) { if (AopUtils.isAopProxy(this.source)) { Advised advised = (Advised) this.source; this.appliedAdvices.forEach(advised::removeAdvice); chain.stream().forEach(advice -> advised.addAdvisor(adviceToReceiveAdvisor(advice))); } else { ProxyFactory proxyFactory = new ProxyFactory(this.source); chain.stream().forEach(advice -> proxyFactory.addAdvisor(adviceToReceiveAdvisor(advice))); this.source = (MessageSource<?>) proxyFactory.getProxy(getBeanClassLoader()); } this.appliedAdvices.clear(); this.appliedAdvices.addAll(chain); if (!(isSyncExecutor()) && logger.isWarnEnabled()) { logger.warn(getComponentName() + ": A task executor is supplied and " + chain.size() + "MessageSourceMutator(s) is/are provided. If an advice mutates the source, such " + "mutations are not thread safe and could cause unexpected results, especially with " + "high frequency pollers. Consider using a downstream ExecutorChannel instead of " + "adding an executor to the poller"); } } }
@Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { Class<?> targetClass = AopUtils.getTargetClass(bean); // the set will hold records of prior class scans and will contain the bean classes that can not // be assigned to the Advisor interface and therefore can be short circuited if (this.nonApplicableCache.contains(targetClass)) { return bean; } if (AopUtils.canApply(this.advisor, targetClass)) { if (bean instanceof Advised) { ((Advised) bean).addAdvisor(this.advisor); return bean; } else { ProxyFactory proxyFactory = new ProxyFactory(bean); // Copy our properties (proxyTargetClass etc) inherited from ProxyConfig. proxyFactory.copyFrom(this); proxyFactory.addAdvisor(this.advisor); return proxyFactory.getProxy(this.beanClassLoader); } } else { // cannot apply advisor this.nonApplicableCache.add(targetClass); return bean; } }
@Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof AopInfrastructureBean) { return bean; } final Class<?> targetClass = AopUtils.getTargetClass(bean); final Pointcut pointcut = getPointcut(); if (AopUtils.canApply(pointcut, targetClass)) { final MethodInterceptor interceptor = getMethodInterceptor(targetClass); final PointcutAdvisor advisor = new DefaultPointcutAdvisor(pointcut, interceptor); if (bean instanceof Advised) { if (log.isDebugEnabled()) { log.debug("Bean " + beanName + " is already proxied, adding Advisor to existing proxy"); } ((Advised) bean).addAdvisor(0, advisor); return bean; } if (log.isDebugEnabled()) { log.debug("Proxying bean " + beanName + " of type " + targetClass.getCanonicalName()); } final ProxyFactory proxyFactory = new ProxyFactory(bean); proxyFactory.copyFrom(this); proxyFactory.addAdvisor(advisor); return proxyFactory.getProxy(this.beanClassLoader); } return bean; }
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof AopInfrastructureBean) { // Ignore AOP infrastructure such as scoped proxies. return bean; } Class<?> targetClass = AopUtils.getTargetClass(bean); if (AopUtils.canApply(this.advisor, targetClass)) { if (bean instanceof Advised) { ((Advised) bean).addAdvisor(0, this.advisor); return bean; } else { ProxyFactory proxyFactory = new ProxyFactory(bean); // Copy our properties (proxyTargetClass etc) inherited from ProxyConfig. proxyFactory.copyFrom(this); proxyFactory.addAdvisor(this.advisor); return proxyFactory.getProxy(this.beanClassLoader); } } else { // No async proxy needed. return bean; } }
handlerAdvice.addMethodName("handleMessage"); if (this.handler instanceof Advised) { ((Advised) this.handler).addAdvisor(handlerAdvice);
advised.addAdvisor(0, this.advisor); advised.addAdvisor(this.advisor);
advised.addAdvisor(0, this.advisor); advised.addAdvisor(this.advisor);
((Advised) handler).addAdvisor(advisor); handlerAdvice.addMethodName("handleMessage"); if (handler instanceof Advised) { ((Advised) handler).addAdvisor(handlerAdvice);