org.springframework.aop.framework.ProxyFactoryBean Java Examples

The following examples show how to use org.springframework.aop.framework.ProxyFactoryBean. 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 File: BootifulBannersServiceApplication.java    From bootiful-banners with Apache License 2.0 6 votes vote down vote up
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 #2
Source File: AclJpaQuery.java    From strategy-spring-security-acl with 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 #3
Source File: Spr15042Tests.java    From spring-analysis-note with 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 File: ExecutorBeanPostProcessor.java    From elasticactors with 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 #5
Source File: ExecutorBeanPostProcessorTests.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@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 #6
Source File: ExecutorBeanPostProcessor.java    From spring-cloud-sleuth with 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 #7
Source File: ExecutorBeanPostProcessor.java    From spring-cloud-sleuth with Apache License 2.0 5 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 #8
Source File: TraceMessagingAutoConfiguration.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@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 #9
Source File: ExecutorBeanPostProcessor.java    From java-spring-cloud with Apache License 2.0 5 votes vote down vote up
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 #10
Source File: ProxyFactoryBeanConfig.java    From Hands-On-High-Performance-with-Spring-5 with MIT License 5 votes vote down vote up
@Bean
public ProxyFactoryBean transferService(){
	ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
	proxyFactoryBean.setTarget(new TransferServiceImpl());
	proxyFactoryBean.addAdvisor(transferServiceAdvisor());
	proxyFactoryBean.setExposeProxy(true);
	return proxyFactoryBean;
}
 
Example #11
Source File: Spr15042Tests.java    From java-technology-stack with 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 #12
Source File: AbstractInterceptorDrivenBeanDefinitionDecorator.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private boolean isProxyFactoryBeanDefinition(BeanDefinition existingDefinition) {
	return ProxyFactoryBean.class.getName().equals(existingDefinition.getBeanClassName());
}
 
Example #13
Source File: AutoProxyBeanFactory.java    From framework with 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 #14
Source File: AbstractInterceptorDrivenBeanDefinitionDecorator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private boolean isProxyFactoryBeanDefinition(BeanDefinition existingDefinition) {
	return ProxyFactoryBean.class.getName().equals(existingDefinition.getBeanClassName());
}
 
Example #15
Source File: ExecutorBeanPostProcessor.java    From spring-cloud-sleuth with Apache License 2.0 4 votes vote down vote up
Object getObject(ProxyFactoryBean factory) {
	return factory.getObject();
}
 
Example #16
Source File: AbstractInterceptorDrivenBeanDefinitionDecorator.java    From java-technology-stack with MIT License 4 votes vote down vote up
private boolean isProxyFactoryBeanDefinition(BeanDefinition existingDefinition) {
	return ProxyFactoryBean.class.getName().equals(existingDefinition.getBeanClassName());
}
 
Example #17
Source File: ExecutorBeanPostProcessor.java    From elasticactors with 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 #18
Source File: ExecutorBeanPostProcessor.java    From elasticactors with Apache License 2.0 4 votes vote down vote up
Object getObject(ProxyFactoryBean factory) {
    return factory.getObject();
}
 
Example #19
Source File: AbstractInterceptorDrivenBeanDefinitionDecorator.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private boolean isProxyFactoryBeanDefinition(BeanDefinition existingDefinition) {
	return ProxyFactoryBean.class.getName().equals(existingDefinition.getBeanClassName());
}