Java Code Examples for org.springframework.aop.framework.ProxyFactoryBean

The following examples show how to use org.springframework.aop.framework.ProxyFactoryBean. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: strategy-spring-security-acl   Source File: AclJpaQuery.java    License: Apache License 2.0 6 votes vote down vote up
private AclPredicateTargetSource installAclPredicateTargetSource() {
  synchronized (cachedCriteriaQuery) {
    Predicate restriction = cachedCriteriaQuery.getRestriction();

    if (restriction instanceof Advised) {
      Advised advised = (Advised) restriction;
      if (advised.getTargetSource() instanceof AclPredicateTargetSource) {
        return (AclPredicateTargetSource) advised.getTargetSource();
      }
    }

    AclPredicateTargetSource targetSource = new AclPredicateTargetSource(em.getCriteriaBuilder(), restriction);
    ProxyFactoryBean factoryBean = new ProxyFactoryBean();
    factoryBean.setTargetSource(targetSource);
    factoryBean.setAutodetectInterfaces(true);
    Predicate enhancedPredicate = (Predicate) factoryBean.getObject();
    logger.debug("ACL Jpa Specification target source initialized for criteria {}", cachedCriteriaQuery);

    // install proxy inside criteria
    cachedCriteriaQuery.where(enhancedPredicate);
    return targetSource;
  }
}
 
Example 2
private Environment environmentForImage(int maxWidth, boolean invert) {
	Map<String, Object> specification = new HashMap<>();
	specification.put("banner.image.width", maxWidth);
	specification.put("banner.image.invert", invert);
	ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
	proxyFactoryBean.setInterfaces(Environment.class);
	proxyFactoryBean.addAdvice((MethodInterceptor) invocation -> {
		String containsProperty = "containsProperty";
		String getProperty = "getProperty";
		List<String> toHandle = Arrays.asList(containsProperty, getProperty);
		String methodName = invocation.getMethod().getName();
		if (toHandle.contains(methodName)) {
			String key = String.class.cast(invocation.getArguments()[0]);
			if (methodName.equals(containsProperty)) {
				return (specification.containsKey(key) || this.environment.containsProperty(key));
			}
			if (methodName.equals(getProperty)) {
				return specification.getOrDefault(key, this.environment.getProperty(key));
			}
		}
		return invocation.getMethod().invoke(this.environment, invocation.getArguments());
	});
	return Environment.class.cast(proxyFactoryBean.getObject());
}
 
Example 3
Source Project: spring-analysis-note   Source File: Spr15042Tests.java    License: MIT License 5 votes vote down vote up
@Bean
@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public ProxyFactoryBean myObject() {
	ProxyFactoryBean pfb = new ProxyFactoryBean();
	pfb.setTargetSource(poolTargetSource());
	return pfb;
}
 
Example 4
Source Project: java-technology-stack   Source File: Spr15042Tests.java    License: MIT License 5 votes vote down vote up
@Bean
@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public ProxyFactoryBean myObject() {
	ProxyFactoryBean pfb = new ProxyFactoryBean();
	pfb.setTargetSource(poolTargetSource());
	return pfb;
}
 
Example 5
@Bean
public ProxyFactoryBean transferService(){
	ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
	proxyFactoryBean.setTarget(new TransferServiceImpl());
	proxyFactoryBean.addAdvisor(transferServiceAdvisor());
	proxyFactoryBean.setExposeProxy(true);
	return proxyFactoryBean;
}
 
Example 6
private <T extends Executor> Object proxify(T executor, BiFunction<T, Tracer, T> tracingExecutorProvider, boolean useCglib) {
  ProxyFactoryBean factory = new ProxyFactoryBean();
  factory.setProxyTargetClass(useCglib);
  factory.addAdvice(new ExecutorMethodInterceptor<>(executor, tracingExecutorProvider, tracer));
  factory.setTarget(executor);
  return factory.getObject();
}
 
Example 7
@SuppressWarnings("unchecked")
Object createProxy(Object bean) {
	ProxyFactoryBean factory = new ProxyFactoryBean();
	factory.setProxyTargetClass(true);
	factory.addAdvice(
			new MessageListenerMethodInterceptor(this.kafkaTracing, this.tracer));
	factory.setTarget(bean);
	return factory.getObject();
}
 
Example 8
private Object getProxiedObject(Object bean, boolean cglibProxy, Executor executor,
		Supplier<Executor> supplier) {
	ProxyFactoryBean factory = proxyFactoryBean(bean, cglibProxy, executor, supplier);
	try {
		return getObject(factory);
	}
	catch (Exception ex) {
		if (log.isDebugEnabled()) {
			log.debug(
					"Exception occurred while trying to get a proxy. Will fallback to a different implementation",
					ex);
		}
		try {
			if (bean instanceof ThreadPoolTaskScheduler) {
				if (log.isDebugEnabled()) {
					log.debug(
							"Will wrap ThreadPoolTaskScheduler in its tracing representation due to previous errors");
				}
				return createThreadPoolTaskSchedulerProxy(
						(ThreadPoolTaskScheduler) bean).get();
			}
			else if (bean instanceof ScheduledThreadPoolExecutor) {
				if (log.isDebugEnabled()) {
					log.debug(
							"Will wrap ScheduledThreadPoolExecutor in its tracing representation due to previous errors");
				}
				return createScheduledThreadPoolExecutorProxy(
						(ScheduledThreadPoolExecutor) bean).get();
			}
		}
		catch (Exception ex2) {
			if (log.isDebugEnabled()) {
				log.debug(
						"Fallback for special wrappers failed, will try the tracing representation instead",
						ex2);
			}
		}
		return supplier.get();
	}
}
 
Example 9
@SuppressWarnings("unchecked")
Object createProxy(Object bean, boolean cglibProxy, Advice advice) {
	ProxyFactoryBean factory = new ProxyFactoryBean();
	factory.setProxyTargetClass(cglibProxy);
	factory.addAdvice(advice);
	factory.setTarget(bean);
	return getObject(factory);
}
 
Example 10
@Test
public void should_fallback_to_sleuth_impl_when_it_is_not_possible_to_create_any_proxy_for_ExecutorService()
		throws Exception {
	ExecutorService service = BDDMockito.mock(ExecutorService.class);
	ExecutorBeanPostProcessor bpp = new ExecutorBeanPostProcessor(this.beanFactory) {
		@Override
		Object getObject(ProxyFactoryBean factory) {
			throw new AopConfigException("foo");
		}
	};

	Object o = bpp.postProcessAfterInitialization(service, "foo");

	then(o).isInstanceOf(TraceableExecutorService.class);
}
 
Example 11
Source Project: elasticactors   Source File: ExecutorBeanPostProcessor.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
Object createProxy(Object bean, boolean cglibProxy, Advice advice) {
    ProxyFactoryBean factory = new ProxyFactoryBean();
    factory.setProxyTargetClass(cglibProxy);
    factory.addAdvice(advice);
    factory.setTarget(bean);
    return getObject(factory);
}
 
Example 12
private boolean isProxyFactoryBeanDefinition(BeanDefinition existingDefinition) {
	return ProxyFactoryBean.class.getName().equals(existingDefinition.getBeanClassName());
}
 
Example 13
private boolean isProxyFactoryBeanDefinition(BeanDefinition existingDefinition) {
	return ProxyFactoryBean.class.getName().equals(existingDefinition.getBeanClassName());
}
 
Example 14
private boolean isProxyFactoryBeanDefinition(BeanDefinition existingDefinition) {
	return ProxyFactoryBean.class.getName().equals(existingDefinition.getBeanClassName());
}
 
Example 15
private boolean isProxyFactoryBeanDefinition(BeanDefinition existingDefinition) {
	return ProxyFactoryBean.class.getName().equals(existingDefinition.getBeanClassName());
}
 
Example 16
Source Project: framework   Source File: AutoProxyBeanFactory.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Description: <br>
 * 
 * @author yang.zhipeng <br>
 * @taskId <br>
 * @param beanFactory <br>
 * @throws BeansException <br>
 */
@Override
public void postProcessBeanFactory(final ConfigurableListableBeanFactory beanFactory) throws BeansException {
    logger.info("*************************Dao注入列表***************************");
    SQLHandler sqlHandler = new SQLHandler();
    sqlHandler.setDaoConfig(config);

    ICache cache = CacheHelper.getCache();
    cache.removeNode(CacheConstant.SQL_PARAM_DIR);
    cache.removeNode(CacheConstant.SQL_DIR);

    try {
        for (String pack : packagesToScan) {
            if (StringUtils.isNotEmpty(pack)) {
                Set<Class<?>> clazzSet = BeanUtil.getClasses(pack);
                String className = null;
                for (Class<?> clazz : clazzSet) {
                    if (clazz.isAnnotationPresent(Dao.class)) {
                        className = clazz.getName();
                        String beanName = StringUtils.uncapitalize(clazz.getSimpleName());
                        if (beanFactory.containsBean(beanName)) {
                            beanName = className;
                            if (beanFactory.containsBean(beanName)) {
                                continue;
                            }
                        }

                        // 此处不缓存SQL
                        sqlHandler.invoke(clazz);

                        // 单独加载一个接口的代理类
                        ProxyFactoryBean factoryBean = new ProxyFactoryBean();
                        factoryBean.setBeanFactory(beanFactory);
                        factoryBean.setInterfaces(clazz);
                        factoryBean.setInterceptorNames(interceptors);
                        factoryBean.setTarget(getDaoHandler(clazz));
                        beanFactory.registerSingleton(beanName, factoryBean);
                        logger.info("    success create interface [{0}] with name {1}", className, beanName);
                    }
                }
            }
        }
    }
    catch (Exception e) {
        logger.error("------->自动扫描jar包失败", e);
    }
    logger.info("***********************************************************");
}
 
Example 17
Object getObject(ProxyFactoryBean factory) {
	return factory.getObject();
}
 
Example 18
Source Project: elasticactors   Source File: ExecutorBeanPostProcessor.java    License: Apache License 2.0 4 votes vote down vote up
private Object getProxiedObject(
        Object bean, boolean cglibProxy, Executor executor,
        Supplier<Executor> supplier) {
    ProxyFactoryBean factory = proxyFactoryBean(bean, cglibProxy, executor, supplier);
    try {
        return getObject(factory);
    } catch (Exception ex) {
        if (log.isDebugEnabled()) {
            log.debug(
                    "Exception occurred while trying to get a proxy. Will fallback to a "
                            + "different implementation",
                    ex);
        }
        try {
            if (bean instanceof ThreadPoolTaskScheduler) {
                if (log.isDebugEnabled()) {
                    log.debug(
                            "Will wrap ThreadPoolTaskScheduler in its tracing representation "
                                    + "due to previous errors");
                }
                return createThreadPoolTaskSchedulerProxy(
                        (ThreadPoolTaskScheduler) bean).get();
            } else if (bean instanceof ScheduledThreadPoolExecutor) {
                if (log.isDebugEnabled()) {
                    log.debug(
                            "Will wrap ScheduledThreadPoolExecutor in its tracing "
                                    + "representation due to previous errors");
                }
                return createScheduledThreadPoolExecutorProxy(
                        (ScheduledThreadPoolExecutor) bean).get();
            }
        } catch (Exception ex2) {
            if (log.isDebugEnabled()) {
                log.debug(
                        "Fallback for special wrappers failed, will try the tracing "
                                + "representation instead",
                        ex2);
            }
        }
        return supplier.get();
    }
}
 
Example 19
Source Project: elasticactors   Source File: ExecutorBeanPostProcessor.java    License: Apache License 2.0 4 votes vote down vote up
Object getObject(ProxyFactoryBean factory) {
    return factory.getObject();
}