Example usage for org.springframework.aop.framework ProxyFactoryBean addAdvice

List of usage examples for org.springframework.aop.framework ProxyFactoryBean addAdvice

Introduction

In this page you can find the example usage for org.springframework.aop.framework ProxyFactoryBean addAdvice.

Prototype

@Override
    public void addAdvice(Advice advice) throws AopConfigException 

Source Link

Usage

From source file:org.codehaus.mojo.hibernate3.HibernateExporterMojo.java

/**
 * Builds a proxy that will respect any configured processor instance if configured. This should only be called on subclasses that end up generated java classes.
 *
 * @param delegate the original Exporter
 * @return an Exporter proxy that will correctly give the processor objects a chance to run after the delegate exporters' start() method's been called.
 *///from   w  ww .  j ava 2  s . co m
protected Exporter buildProcessorAwareExporter(final Exporter delegate) {

    MethodInterceptor interceptor = new MethodInterceptor() {
        @Override
        public Object invoke(MethodInvocation invocation) throws Throwable {
            Method method = invocation.getMethod();
            Object[] objects = invocation.getArguments();

            Object result;
            try {
                result = method.invoke(delegate, objects);
                if (method.getName().contains("start")) {
                    handleComposites();
                    handleProcessor();
                }
                return result;
            } catch (Throwable throwable) {
                getLog().error(throwable);
            }
            return null;

        }
    };

    ProxyFactoryBean bean = new ProxyFactoryBean();
    bean.addAdvice(interceptor);
    bean.setProxyTargetClass(true);
    bean.setBeanClassLoader(delegate.getClass().getClassLoader());
    bean.setAutodetectInterfaces(true);
    bean.setTarget(delegate);
    return (Exporter) bean.getObject();
}

From source file:org.springframework.amqp.rabbit.test.RabbitListenerTestHarness.java

@Override
protected void processListener(MethodRabbitListenerEndpoint endpoint, RabbitListener rabbitListener,
        Object bean, Object adminTarget, String beanName) {
    Object proxy = bean;//from w  w  w.  j  a  va2s .c om
    String id = rabbitListener.id();
    if (StringUtils.hasText(id)) {
        if (this.attributes.getBoolean("spy")) {
            proxy = Mockito.spy(proxy);
            this.listeners.put(id, proxy);
        }
        if (this.attributes.getBoolean("capture")) {
            try {
                ProxyFactoryBean pfb = new ProxyFactoryBean();
                pfb.setProxyTargetClass(true);
                pfb.setTarget(proxy);
                CaptureAdvice advice = new CaptureAdvice();
                pfb.addAdvice(advice);
                proxy = pfb.getObject();
                this.listenerCapture.put(id, advice);
            } catch (Exception e) {
                logger.error("Failed to proxy @RabbitListener with id: " + id);
            }
        }
    } else {
        logger.info("The test harness can only proxy @RabbitListeners with an 'id' attribute");
    }
    super.processListener(endpoint, rabbitListener, proxy, adminTarget, beanName);
}

From source file:org.springframework.cloud.sleuth.instrument.async.ExecutorBeanPostProcessor.java

Object createThreadPoolTaskExecutorProxy(Object bean, boolean cglibProxy, ThreadPoolTaskExecutor executor) {
    ProxyFactoryBean factory = new ProxyFactoryBean();
    factory.setProxyTargetClass(cglibProxy);
    factory.addAdvice(new ExecutorMethodInterceptor<ThreadPoolTaskExecutor>(executor, this.beanFactory) {
        @Override/* w  w w  .j a v  a  2s  .  c  om*/
        Executor executor(BeanFactory beanFactory, ThreadPoolTaskExecutor executor) {
            return new LazyTraceThreadPoolTaskExecutor(beanFactory, executor);
        }
    });
    factory.setTarget(bean);
    return factory.getObject();
}

From source file:org.springframework.cloud.sleuth.instrument.async.ExecutorBeanPostProcessor.java

@SuppressWarnings("unchecked")
Object createProxy(Object bean, boolean cglibProxy, Executor executor) {
    ProxyFactoryBean factory = new ProxyFactoryBean();
    factory.setProxyTargetClass(cglibProxy);
    factory.addAdvice(new ExecutorMethodInterceptor(executor, this.beanFactory));
    factory.setTarget(bean);/*from  w ww.j av  a 2  s. c  om*/
    return factory.getObject();
}

From source file:org.springframework.cloud.sleuth.instrument.messaging.TraceMessagingAutoConfiguration.java

@SuppressWarnings("unchecked")
Object createProxy(Object bean) {
    ProxyFactoryBean factory = new ProxyFactoryBean();
    factory.setProxyTargetClass(true);//from  ww  w  .j  a v  a 2s  .  co  m
    factory.addAdvice(new MessageListenerMethodInterceptor(this.kafkaTracing, this.tracer));
    factory.setTarget(bean);
    return factory.getObject();
}

From source file:org.springframework.integration.smpp.util.TestCurrentExecutingMethodAdvice.java

@Test
public void testLoggingTheCurrentlyExecutingMethodName() throws Throwable {
    ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
    proxyFactoryBean.setProxyTargetClass(true);
    proxyFactoryBean.addAdvice(new CurrentMethodExposingMethodInterceptor());
    proxyFactoryBean.setTarget(new TestClassWithAMethod());

    TestClassWithAMethod testClassWithAMethod = (TestClassWithAMethod) proxyFactoryBean.getObject();
    testClassWithAMethod.testMe();/*from w  w w . j  a va  2  s  .c  o m*/
}