Example usage for org.springframework.aop.framework ProxyFactory addAdvisor

List of usage examples for org.springframework.aop.framework ProxyFactory addAdvisor

Introduction

In this page you can find the example usage for org.springframework.aop.framework ProxyFactory addAdvisor.

Prototype

@Override
    public void addAdvisor(Advisor advisor) 

Source Link

Usage

From source file:prospring3.ch5.beanpostprocessor.DeassociateBeanPostProcessor.java

public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    if (bean instanceof AopInfrastructureBean) {
        // Ignore AOP infrastructure such as scoped proxies.
        return bean;
    }//from  www  .  j  av  a 2 s. c o m
    Class<?> targetClass = AopUtils.getTargetClass(bean);
    if (AopUtils.canApply(this.deassociatePointcutAdvisor, targetClass)) {
        if (bean instanceof Advised) {
            ((Advised) bean).addAdvisor(this.deassociatePointcutAdvisor);
            return bean;
        } else {
            ProxyFactory proxyFactory = new ProxyFactory(bean);
            // Copy our properties (proxyTargetClass etc) inherited from ProxyConfig.
            proxyFactory.copyFrom(this);
            proxyFactory.addAdvisor(this.deassociatePointcutAdvisor);
            return proxyFactory.getProxy(this.classLoader);
        }
    } else {
        // This is not a repository.
        return bean;
    }

}

From source file:org.pshow.ecm.security.MethodAuditPostProcessor.java

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    if (bean instanceof AopInfrastructureBean) {
        // Ignore AOP infrastructure such as scoped proxies.
        return bean;
    }/*w  w  w  .ja  va2s .  c om*/
    Class<?> targetClass = AopUtils.getTargetClass(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 {
        // This is not a repository.
        return bean;
    }
}

From source file:dk.clanie.actor.ActorAnnotationBeanPostProcessor.java

public Object postProcessAfterInitialization(Object bean, String beanName) {
    if (bean instanceof AopInfrastructureBean) {
        // Ignore AOP infrastructure such as scoped proxies.
        return bean;
    }/*from   w ww.  ja v  a 2  s.c  om*/
    Class<?> targetClass = AopUtils.getTargetClass(bean);
    Actor annotation = AnnotationUtils.findAnnotation(targetClass, Actor.class);
    if (annotation != null) {
        //      if (AopUtils.canApply(this.asyncAnnotationAdvisor, targetClass)) {

        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setMaxPoolSize(1);
        executor.setDaemon(true);
        String threadNamePrefix = beanName + ",";
        executor.setThreadNamePrefix(threadNamePrefix);
        executor.initialize();

        ActorAnnotationAdvisor actorAnnotationAdvisor = new ActorAnnotationAdvisor(executor);

        if (bean instanceof Advised) {
            ((Advised) bean).addAdvisor(0, actorAnnotationAdvisor);
            return bean;
        } else {
            ProxyFactory proxyFactory = new ProxyFactory(bean);
            // Copy our properties (proxyTargetClass etc) inherited from ProxyConfig.
            proxyFactory.copyFrom(this);
            proxyFactory.addAdvisor(actorAnnotationAdvisor);
            return proxyFactory.getProxy(this.beanClassLoader);
        }
    } else {
        // No async proxy needed.
        return bean;
    }
}

From source file:com.helpinput.spring.proxy.OptimizeTransactionProxyFactoryBean.java

public void afterPropertiesSet() {
    if (this.target == null) {
        throw new IllegalArgumentException("Property 'target' is required");
    }/*ww w. j av a2  s  .  c o  m*/
    if (this.target instanceof String) {
        throw new IllegalArgumentException("'target' needs to be a bean reference, not a bean name as value");
    }

    ClassLoader proxyClassLoader = target.getClass().getClassLoader();

    ProxyFactory proxyFactory = new ProxyFactory();

    if (this.preInterceptors != null) {
        for (Object interceptor : this.preInterceptors) {
            proxyFactory.addAdvisor(this.advisorAdapterRegistry.wrap(interceptor));
        }
    }

    // Add the main interceptor (typically an Advisor).
    proxyFactory.addAdvisor(this.advisorAdapterRegistry.wrap(createMainInterceptor()));

    if (this.postInterceptors != null) {
        for (Object interceptor : this.postInterceptors) {
            proxyFactory.addAdvisor(this.advisorAdapterRegistry.wrap(interceptor));
        }
    }

    proxyFactory.copyFrom(this);

    TargetSource targetSource = createTargetSource(this.target);
    proxyFactory.setTargetSource(targetSource);

    if (this.proxyInterfaces != null) {
        proxyFactory.setInterfaces(this.proxyInterfaces);
    } else if (!isProxyTargetClass()) {
        // Rely on AOP infrastructure to tell us what interfaces to proxy.
        proxyFactory.setInterfaces(
                ClassUtils.getAllInterfacesForClass(targetSource.getTargetClass(), proxyClassLoader));
    }

    /**
     * use this option to let proxyFactory user cglib to create proxy,otherwise in dynamic script ,this is no dynamic interface
     * ? cglib ??java??
     */
    proxyFactory.setOptimize(true);
    this.proxy = proxyFactory.getProxy(proxyClassLoader);
}

From source file:net.paslavsky.springrest.SpringRestClientFactoryBean.java

@Override
@SuppressWarnings("unchecked")
protected T createInstance() throws Exception {
    ProxyFactory factory = new ProxyFactory();
    if (getObjectType().isInterface()) {
        factory.setInterfaces(getObjectType());
    } else {/*w  w w .j ava2  s  .c  o m*/
        factory.setTargetClass(getObjectType());
    }
    factory.addAdvisor(createRestMethodAdvisor());
    factory.addAdvisor(createToStringPointcutAdvisor());
    return (T) factory.getProxy();
}

From source file:com.newlandframework.avatarmq.netty.MessageEventWrapper.java

public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    super.channelRead(ctx, msg);

    ProxyFactory weaver = new ProxyFactory(wrapper);
    NameMatchMethodPointcutAdvisor advisor = new NameMatchMethodPointcutAdvisor();
    advisor.setMappedName(MessageEventWrapper.proxyMappedName);
    advisor.setAdvice(new MessageEventAdvisor(wrapper, msg));
    weaver.addAdvisor(advisor);

    MessageEventHandler proxyObject = (MessageEventHandler) weaver.getProxy();
    proxyObject.handleMessage(ctx, msg);
}

From source file:org.springmodules.cache.interceptor.proxy.CacheProxyFactoryBean.java

/**
 * Creates the proxy for target object. This method is invoked by a
 * BeanFactory after it has set all bean properties supplied.
 * /*from w ww .ja va2s.  co m*/
 * @throws IllegalStateException
 *           if target is <code>null</code>.
 * @throws AopConfigException
 *           if the proxy interfaces or proxyTargetClass are not set and the
 *           target type is <code>org.springframework.aop.TargetSource</code>.
 */
public void afterPropertiesSet() throws IllegalStateException, AopConfigException {
    cachingInterceptor.afterPropertiesSet();
    flushingInterceptor.afterPropertiesSet();

    if (target == null) {
        throw new IllegalStateException("Property 'target' is required");
    }

    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.addAdvisor(new CachingModelSourceAdvisor(cachingInterceptor));

    if (hasFlushingModels) {
        proxyFactory.addAdvisor(new FlushingModelSourceAdvisor(flushingInterceptor));
    }

    proxyFactory.copyFrom(this);

    TargetSource targetSource = createTargetSource(target);
    proxyFactory.setTargetSource(targetSource);

    if (proxyInterfaces != null) {
        proxyFactory.setInterfaces(proxyInterfaces);
    } else if (!isProxyTargetClass()) {
        if (target instanceof TargetSource) {
            throw new AopConfigException("Either 'proxyInterfaces' or 'proxyTargetClass' is required "
                    + "when using a TargetSource as 'target'");
        }

        // rely on AOP infrastructure to tell us what interfaces to proxy
        proxyFactory.setInterfaces(ClassUtils.getAllInterfaces(target));
    }

    proxy = proxyFactory.getProxy();
}

From source file:org.jdal.remoting.rmi.RmiNativeRemoteReference.java

/**
 * {@inheritDoc}//  w  ww .j  av  a 2 s . c  o m
 */
@SuppressWarnings("unchecked")
public RemoteClient createRemoteClient() {
    ProxyFactory pf = null;
    if (getServiceInterface().isAssignableFrom(remoteService.getClass())) {
        pf = new ProxyFactory(remoteService);
    } else {
        pf = new ProxyFactory(getServiceInterface(),
                new RmiServiceInterceptor((RmiInvocationHandler) remoteService));
    }

    pf.addInterface(RemoteClient.class);
    pf.addAdvisor(new RemoteClientAdvisor(this));

    return (RemoteClient) pf.getProxy();
}

From source file:aop.DeclareMixinAspectJAdvisorFactoryTest.java

protected Object createProxy(Object target, List<Advisor> advisors, Class<?>... interfaces) {
    ProxyFactory pf = new ProxyFactory(target);
    if (interfaces.length > 1 || interfaces[0].isInterface()) {
        pf.setInterfaces(interfaces);/*from   www.ja v  a2s .  co m*/
    } else {
        pf.setProxyTargetClass(true);
    }

    // Required everywhere we use AspectJ proxies
    pf.addAdvice(ExposeInvocationInterceptor.INSTANCE);

    for (Object a : advisors) {
        pf.addAdvisor((Advisor) a);
    }

    pf.setExposeProxy(true);
    return pf.getProxy();
}